youtube subscriber count discord bot activity code example
Example: youtube subscriber count discord bot activity
import discord
from discord.ext import commands
from discord.ext.commands import Bot
from discord.utils import get
import urllib.request
import json
import asyncio
intents = discord.Intents.default()
intents.members = True
bot = commands.Bot(intents=intents, command_prefix="!")
ytId = "UCgx3cwTPCFWEe7GAe_X8-1Q"
token = "Your Discord Token"
key = "Your YouTube API Key (you'll need to enable the youtube data api on it)"
data = urllib.request.urlopen("https://www.googleapis.com/youtube/v3/channels?part=statistics&id="+ytId+"&fields=items/statistics/subscriberCount&key="+key).read()
subs = json.loads(data)["items"][0]["statistics"]["subscriberCount"]
message = str(subs) + " Subscribers"
@bot.event
async def status_task():
while True:
data = urllib.request.urlopen("https://www.googleapis.com/youtube/v3/channels?part=statistics&id="+ytId+"&fields=items/statistics/subscriberCount&key="+key).read()
subs = json.loads(data)["items"][0]["statistics"]["subscriberCount"]
message = str(subs) + " Subscribers"
print(message)
await bot.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name=message))
await asyncio.sleep(60)
data = urllib.request.urlopen("https://www.googleapis.com/youtube/v3/channels?part=statistics&id="+ytId+"&fields=items/statistics/subscriberCount&key="+key).read()
subs = json.loads(data)["items"][0]["statistics"]["subscriberCount"]
message = str(subs) + " Subscribers"
print(message)
await bot.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name=message))
await asyncio.sleep(60)
@bot.event
async def on_ready():
print('Logged in as: ' + bot.user.name)
print('Ready!\n')
await bot.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name=message))
bot.loop.create_task(status_task())
bot.run(token)