SHARE
TWEET

Untitled

a guest Jan 18th, 2019 88 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import copy
  2. import discord
  3. from redbot.core import Config, commands, checks
  4. from redbot.core.utils.chat_formatting import pagify
  5.  
  6.  
  7. BaseCog = getattr(commands, "Cog", object)
  8.  
  9. class SmartReact(BaseCog):
  10.     """Create automatic reactions when trigger words are typed in chat"""
  11.  
  12.     default_guild_settings = {
  13.         "reactions": {}
  14.     }
  15.  
  16.     def __init__(self, bot):
  17.         self.bot = bot
  18.         self.conf = Config.get_conf(self, identifier=964952632)
  19.         self.conf.register_guild(
  20.             **self.default_guild_settings
  21.         )
  22.  
  23.     @checks.mod_or_permissions(administrator=True)
  24.     @commands.guild_only()
  25.     @commands.command(name="addreact")
  26.     async def addreact(self, ctx, word, emoji):
  27.         """Add an auto reaction to a word"""
  28.         guild = ctx.message.guild
  29.         message = ctx.message
  30.         emoji = self.fix_custom_emoji(emoji)
  31.         await self.create_smart_reaction(guild, word, emoji, message)
  32.  
  33.     @checks.mod_or_permissions(administrator=True)
  34.     @commands.guild_only()
  35.     @commands.command(name="delreact")
  36.     async def delreact(self, ctx, word, emoji):
  37.         """Delete an auto reaction to a word"""
  38.         guild = ctx.message.guild
  39.         message = ctx.message
  40.         emoji = self.fix_custom_emoji(emoji)
  41.         await self.remove_smart_reaction(guild, word, emoji, message)
  42.  
  43.     def fix_custom_emoji(self, emoji):
  44.         if emoji[:2] != "<:":
  45.             return emoji
  46.         for guild in self.bot.guilds:
  47.             for e in guild.emojis:
  48.                 if str(e.id) == emoji.split(':')[2][:-1]:
  49.                     return e
  50.         return None
  51.  
  52.     @checks.mod_or_permissions(administrator=True)
  53.     @commands.guild_only()
  54.     @commands.command(name="listreact")
  55.     async def listreact(self, ctx):
  56.         """List reactions for this server"""
  57.         emojis = await self.conf.guild(ctx.guild).reactions()
  58.         msg = f"Smart Reactions for {ctx.guild.name}:\n"
  59.         for emoji in emojis:
  60.             for command in emojis[emoji]:
  61.                 msg += f"{emoji}: {command}\n"
  62.         for page in pagify(msg, delims=["\n"]):
  63.             await ctx.send(msg)
  64.  
  65.     async def create_smart_reaction(self, guild, word, emoji, message):
  66.         try:
  67.             # Use the reaction to see if it's valid
  68.             await message.add_reaction(emoji)
  69.             emoji = str(emoji)
  70.             reactions = await self.conf.guild(guild).reactions()
  71.             if emoji in reactions:
  72.                 if word.lower() in reactions[emoji]:
  73.                     await message.channel.send("This smart reaction already exists.")
  74.                     return
  75.                 reactions[emoji].append(word.lower())
  76.             else:
  77.                 reactions[emoji] = [word.lower()]
  78.             await self.conf.guild(guild).reactions.set(reactions)
  79.             await message.channel.send("Successfully added this reaction.")
  80.  
  81.         except (discord.errors.HTTPException, discord.errors.InvalidArgument):
  82.             await message.channel.send("That's not an emoji I recognize. "
  83.                                        "(might be custom!)")
  84.  
  85.     async def remove_smart_reaction(self, guild, word, emoji, message):
  86.         try:
  87.             # Use the reaction to see if it's valid
  88.             await message.add_reaction(emoji)
  89.             emoji = str(emoji)
  90.             reactions = await self.conf.guild(guild).reactions()
  91.             if emoji in reactions:
  92.                 if word.lower() in reactions[emoji]:
  93.                     reactions[emoji].remove(word.lower())
  94.                     await self.conf.guild(guild).reactions.set(reactions)
  95.                     await message.channel.send("Removed this smart reaction.")
  96.                 else:
  97.                     await message.channel.send("That emoji is not used as a reaction "
  98.                                                "for that word.")
  99.             else:
  100.                 await message.channel.send("There are no smart reactions which use "
  101.                                            "this emoji.")
  102.  
  103.         except (discord.errors.HTTPException, discord.errors.InvalidArgument):
  104.             await message.channel.send("That's not an emoji I recognize. "
  105.                                "(might be custom!)")
  106.  
  107.     # Thanks irdumb#1229 for the help making this "more Pythonic"
  108.     async def on_message(self, message):
  109.         if message.author == self.bot.user:
  110.             return
  111.         guild = message.guild
  112.         reacts = copy.deepcopy(await self.conf.guild(guild).reactions())
  113.         if reacts is None:
  114.             return
  115.         words = message.content.lower().split()
  116.         for emoji in reacts:
  117.             if set(w.lower() for w in reacts[emoji]).intersection(words):
  118.                 emoji = self.fix_custom_emoji(emoji)
  119.                 try:
  120.                     await message.add_reaction(emoji)
  121.                 except discord.errors.Forbidden:
  122.                     pass
  123.                 except discord.errors.InvalidArgument:
  124.                     pass
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top