Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import discord, sqlite3, twitch, asyncio, os
- from discord.ext import commands
- TOKEN = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
- conn = sqlite3.connect("info.db")
- bot = commands.Bot(command_prefix="!")
- COLOR = discord.Color(6570404)
- def get_notification_channel(guild_id: int):
- cur = conn.cursor()
- cur.execute(f"SELECT channel_id FROM notification_channels WHERE guild_id='{guild_id}'")
- results = cur.fetchall()
- guild = bot.get_guild(guild_id)
- if guild:
- for result in results:
- channel = guild.get_channel(int(result[0]))
- if channel:
- return channel
- async def background_task():
- await bot.wait_until_ready()
- stream_messages = {}
- token = twitch.get_token()
- game_id = twitch.get_game_id_by_name("Let It Die", token=token)
- while True:
- try:
- print("Background task, checking...")
- token = twitch.get_token()
- current_streams = twitch.get_game_streams(game_id, token=token)
- for guild in bot.guilds:
- blacklist = []
- cur = conn.cursor()
- cur.execute(f"SELECT user_name FROM blacklist WHERE guild_id='{guild.id}'")
- results = cur.fetchall()
- for result in results:
- blacklist.append(result[0])
- cur.close()
- channel = get_notification_channel(guild.id)
- if channel:
- for streamer_id in current_streams:
- if current_streams[streamer_id].user_name not in blacklist:
- stream = current_streams[streamer_id]
- embed = discord.Embed(title=stream.title, description=f"{stream.user_name} is streaming on **Let It Die** on Twitch!", color=COLOR)
- embed.add_field(name="Viewer Count", value=stream.viewer_count, inline=False)
- embed.add_field(name="URL", value=f"https://www.twitch.tv/{stream.user_name}", inline=False)
- img_url = stream.thumbnail_url.replace("{width}", "500").replace("{height}", "300")
- embed.set_image(url=img_url)
- if streamer_id in stream_messages:
- print("Updating")
- try:
- await stream_messages[streamer_id].edit(embed=embed)
- except Exception as e:
- print(e)
- message = await channel.send(embed=embed)
- stream_messages[streamer_id] = message
- else:
- print("New")
- message = await channel.send(embed=embed)
- stream_messages[streamer_id] = message
- one = set(current_streams.keys())
- two = set(stream_messages.keys())
- diff = list(one-two)
- for streamer_id in diff:
- print("Deleting")
- await stream_messages[streamer_id].delete()
- del(stream_messages[streamer_id])
- await asyncio.sleep(50)
- except Exception as e:
- print(e)
- @bot.event
- async def on_ready():
- print("Logged in")
- for guild in bot.guilds:
- channel = get_notification_channel(guild.id)
- await channel.send("Online!")
- @bot.command()
- async def notificationchannel(ctx, channel: discord.TextChannel=None):
- if ctx.author.guild_permissions.administrator or ctx.author.id == 328786339673800709:
- cur = conn.cursor()
- cur.execute(f"DELETE FROM notification_channels WHERE guild_id='{ctx.guild.id}'")
- cur.execute(f"INSERT INTO notification_channels VALUES ('{channel.id}', '{ctx.guild.id}')")
- cur.close()
- conn.commit()
- await ctx.send("Channel updated.")
- @bot.command()
- async def addblacklist(ctx, user_name):
- if ctx.author.guild_permissions.administrator or ctx.author.id == 328786339673800709:
- cur = conn.cursor()
- cur.execute(f"INSERT INTO blacklist VALUES ('{user_name}', '{ctx.guild.id}')")
- cur.close()
- conn.commit()
- await ctx.send(f"{user_name} added to the blacklist.")
- @bot.command()
- async def removeblacklist(ctx, user_name):
- if ctx.author.guild_permissions.administrator or ctx.author.id == 328786339673800709:
- cur = conn.cursor()
- cur.execute(f"DELETE FROM blacklist WHERE user_name='{user_name}' AND guild_id='{ctx.guild.id}'")
- cur.close()
- conn.commit()
- await ctx.send(f"{user_name} removed from the blacklist.")
- bot.loop.create_task(background_task())
- bot.run(TOKEN)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement