1import discord
2from discord.ext import commands
3from discord.ext.commands import Bot
4from discord.utils import get
5
6import urllib.request
7import json
8import asyncio
9
10intents = discord.Intents.default()
11intents.members = True
12bot = commands.Bot(intents=intents, command_prefix="!")
13
14ytId = "UCgx3cwTPCFWEe7GAe_X8-1Q"
15token = "Your Discord Token"
16key = "Your YouTube API Key (you'll need to enable the youtube data api on it)"
17
18data = urllib.request.urlopen("https://www.googleapis.com/youtube/v3/channels?part=statistics&id="+ytId+"&fields=items/statistics/subscriberCount&key="+key).read()
19subs = json.loads(data)["items"][0]["statistics"]["subscriberCount"]
20message = str(subs) + " Subscribers"
21
22@bot.event
23async def status_task():
24 while True:
25 data = urllib.request.urlopen("https://www.googleapis.com/youtube/v3/channels?part=statistics&id="+ytId+"&fields=items/statistics/subscriberCount&key="+key).read()
26 subs = json.loads(data)["items"][0]["statistics"]["subscriberCount"]
27 message = str(subs) + " Subscribers"
28 print(message)
29 await bot.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name=message))
30 await asyncio.sleep(60)
31 data = urllib.request.urlopen("https://www.googleapis.com/youtube/v3/channels?part=statistics&id="+ytId+"&fields=items/statistics/subscriberCount&key="+key).read()
32 subs = json.loads(data)["items"][0]["statistics"]["subscriberCount"]
33 message = str(subs) + " Subscribers"
34 print(message)
35 await bot.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name=message))
36 await asyncio.sleep(60)
37
38
39@bot.event
40async def on_ready():
41 print('Logged in as: ' + bot.user.name)
42 print('Ready!\n')
43 await bot.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name=message))
44 bot.loop.create_task(status_task())
45
46
47
48
49bot.run(token)