Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2019
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.83 KB | None | 0 0
  1. import discord, sqlite3, twitch, asyncio, os
  2. from discord.ext import commands
  3.  
  4. TOKEN = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
  5.  
  6. conn = sqlite3.connect("info.db")
  7.  
  8. bot = commands.Bot(command_prefix="!")
  9.  
  10. COLOR = discord.Color(6570404)
  11.  
  12. def get_notification_channel(guild_id: int):
  13. cur = conn.cursor()
  14. cur.execute(f"SELECT channel_id FROM notification_channels WHERE guild_id='{guild_id}'")
  15. results = cur.fetchall()
  16. guild = bot.get_guild(guild_id)
  17. if guild:
  18. for result in results:
  19. channel = guild.get_channel(int(result[0]))
  20. if channel:
  21. return channel
  22.  
  23. async def background_task():
  24. await bot.wait_until_ready()
  25. stream_messages = {}
  26. token = twitch.get_token()
  27. game_id = twitch.get_game_id_by_name("Let It Die", token=token)
  28. while True:
  29. try:
  30. print("Background task, checking...")
  31. token = twitch.get_token()
  32. current_streams = twitch.get_game_streams(game_id, token=token)
  33. for guild in bot.guilds:
  34. blacklist = []
  35. cur = conn.cursor()
  36. cur.execute(f"SELECT user_name FROM blacklist WHERE guild_id='{guild.id}'")
  37. results = cur.fetchall()
  38. for result in results:
  39. blacklist.append(result[0])
  40. cur.close()
  41. channel = get_notification_channel(guild.id)
  42. if channel:
  43. for streamer_id in current_streams:
  44. if current_streams[streamer_id].user_name not in blacklist:
  45. stream = current_streams[streamer_id]
  46. embed = discord.Embed(title=stream.title, description=f"{stream.user_name} is streaming on **Let It Die** on Twitch!", color=COLOR)
  47. embed.add_field(name="Viewer Count", value=stream.viewer_count, inline=False)
  48. embed.add_field(name="URL", value=f"https://www.twitch.tv/{stream.user_name}", inline=False)
  49. img_url = stream.thumbnail_url.replace("{width}", "500").replace("{height}", "300")
  50. embed.set_image(url=img_url)
  51. if streamer_id in stream_messages:
  52. print("Updating")
  53. try:
  54. await stream_messages[streamer_id].edit(embed=embed)
  55. except Exception as e:
  56. print(e)
  57. message = await channel.send(embed=embed)
  58. stream_messages[streamer_id] = message
  59. else:
  60. print("New")
  61. message = await channel.send(embed=embed)
  62. stream_messages[streamer_id] = message
  63. one = set(current_streams.keys())
  64. two = set(stream_messages.keys())
  65. diff = list(one-two)
  66. for streamer_id in diff:
  67. print("Deleting")
  68. await stream_messages[streamer_id].delete()
  69. del(stream_messages[streamer_id])
  70. await asyncio.sleep(50)
  71. except Exception as e:
  72. print(e)
  73.  
  74. @bot.event
  75. async def on_ready():
  76. print("Logged in")
  77. for guild in bot.guilds:
  78. channel = get_notification_channel(guild.id)
  79. await channel.send("Online!")
  80.  
  81. @bot.command()
  82. async def notificationchannel(ctx, channel: discord.TextChannel=None):
  83. if ctx.author.guild_permissions.administrator or ctx.author.id == 328786339673800709:
  84. cur = conn.cursor()
  85. cur.execute(f"DELETE FROM notification_channels WHERE guild_id='{ctx.guild.id}'")
  86. cur.execute(f"INSERT INTO notification_channels VALUES ('{channel.id}', '{ctx.guild.id}')")
  87. cur.close()
  88. conn.commit()
  89. await ctx.send("Channel updated.")
  90.  
  91. @bot.command()
  92. async def addblacklist(ctx, user_name):
  93. if ctx.author.guild_permissions.administrator or ctx.author.id == 328786339673800709:
  94. cur = conn.cursor()
  95. cur.execute(f"INSERT INTO blacklist VALUES ('{user_name}', '{ctx.guild.id}')")
  96. cur.close()
  97. conn.commit()
  98. await ctx.send(f"{user_name} added to the blacklist.")
  99.  
  100. @bot.command()
  101. async def removeblacklist(ctx, user_name):
  102. if ctx.author.guild_permissions.administrator or ctx.author.id == 328786339673800709:
  103. cur = conn.cursor()
  104. cur.execute(f"DELETE FROM blacklist WHERE user_name='{user_name}' AND guild_id='{ctx.guild.id}'")
  105. cur.close()
  106. conn.commit()
  107. await ctx.send(f"{user_name} removed from the blacklist.")
  108.  
  109.  
  110. bot.loop.create_task(background_task())
  111. bot.run(TOKEN)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement