You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
3.2KB

  1. from discord import ActivityType
  2. from discord.ext import commands
  3. from Roxbot import checks
  4. from Roxbot.settings import guild_settings
  5. def blacklisted(user):
  6. with open("settings/blacklist.txt", "r") as fp:
  7. for line in fp.readlines():
  8. if user.id+"\n" == line:
  9. return True
  10. return False
  11. class Twitch():
  12. """
  13. A cog that handles posting when users go live on Twitch
  14. """
  15. def __init__(self, bot_client):
  16. self.bot = bot_client
  17. async def on_member_update(self, member_b, member_a):
  18. """Twitch Shilling Part"""
  19. twitch = guild_settings.get(member_b.guild).twitch
  20. if blacklisted(member_b) or not twitch["enabled"]:
  21. return
  22. if member_a.activitiy:
  23. if member_a.activity.type == ActivityType.streaming and member_b.activity.type != ActivityType.streaming:
  24. if not twitch["whitelist"]["enabled"] or member_a.id in twitch["whitelist"]["list"]:
  25. channel = self.bot.get_channel(twitch["channel"])
  26. return await channel.send(":video_game:** {} is live!** :video_game:\n{}\n{}".format(
  27. member_a.name, member_a.game.name, member_a.game.url))
  28. @commands.group()
  29. @checks.is_admin_or_mod()
  30. async def whitelist(self, ctx):
  31. """Command group that handles the twitch cog's whitelist."""
  32. if ctx.invoked_subcommand is None:
  33. return await ctx.send('Missing Argument')
  34. @whitelist.command()
  35. async def enable(self, ctx):
  36. """Enables the twitch shilling whitelist. Repeat the command to disable.
  37. Usage:
  38. ;whitelist enable"""
  39. settings = guild_settings.get(ctx.guild)
  40. if not settings.twitch["whitelist"]["enabled"]:
  41. settings.twitch["whitelist"]["enabled"] = 1
  42. settings.update(settings.twitch, "twitch")
  43. return await ctx.send("Whitelist for Twitch shilling has been enabled.")
  44. else:
  45. settings.twitch["whitelist"]["enabled"] = 0
  46. settings.update(settings.twitch, "twitch")
  47. return await ctx.send("Whitelist for Twitch shilling has been disabled.")
  48. @whitelist.command()
  49. async def edit(self, ctx, option, mentions = None):
  50. """Adds or removes users to the whitelist. Exactly the same as the blacklist command in usage."""
  51. whitelist_count = 0
  52. settings = guild_settings.get(ctx.guild)
  53. if not ctx.message.mentions and option != 'list':
  54. return await ctx.send("You haven't mentioned anyone to whitelist.")
  55. if option not in ['+', '-', 'add', 'remove', 'list']:
  56. return await ctx.send('Invalid option "%s" specified, use +, -, add, or remove' % option, expire_in=20)
  57. if option in ['+', 'add']:
  58. for user in ctx.message.mentions:
  59. settings.twitch["whitelist"]["list"].append(user.id)
  60. whitelist_count += 1
  61. settings.update(settings.twitch, "twitch")
  62. return await ctx.send('{} user(s) have been added to the whitelist'.format(whitelist_count))
  63. elif option in ['-', 'remove']:
  64. for user in ctx.message.mentions:
  65. if user.id in settings.twitch["whitelist"]["list"]:
  66. settings.twitch["whitelist"]["list"].remove(user.id)
  67. whitelist_count += 1
  68. settings.update(settings.twitch, "twitch")
  69. return await ctx.send('{} user(s) have been removed to the whitelist'.format(whitelist_count))
  70. elif option == 'list':
  71. return await ctx.send(settings.twitch["whitelist"]["list"])
  72. def setup(bot_client):
  73. bot_client.add_cog(Twitch(bot_client))