Exemple #1
0
 def on_message_delete(self, event):
     Message.update(deleted=True).where(Message.id == event.id).execute()
Exemple #2
0
 def on_message_delete_bulk(self, event):
     Message.update(deleted=True).where((Message.id << event.ids)).execute()
Exemple #3
0
    def on_message_create(self, event):
        """
        This monstrosity of a function handles the parsing and dispatching of
        commands.
        """
        # Ignore messages sent by bots
        if event.message.author.bot:
            return

        if rdb.sismember('ignored_channels', event.message.channel_id):
            return

        # If this is message for a guild, grab the guild object
        if hasattr(event, 'guild') and event.guild:
            guild_id = event.guild.id
        elif hasattr(event, 'guild_id') and event.guild_id:
            guild_id = event.guild_id
        else:
            guild_id = None

        guild = self.guilds.get(event.guild.id) if guild_id else None
        config = guild and guild.get_config()

        # If the guild has configuration, use that (otherwise use defaults)
        if config and config.commands:
            commands = list(
                self.bot.get_commands_for_message(config.commands.mention, {},
                                                  config.commands.prefix,
                                                  event.message))
        elif guild_id:
            # Otherwise, default to requiring mentions
            commands = list(
                self.bot.get_commands_for_message(True, {}, '', event.message))
        else:
            if ENV != 'prod':
                if not event.message.content.startswith(ENV + '!'):
                    return
                event.message.content = event.message.content[len(ENV) + 1:]

            # DM's just use the commands (no prefix/mention)
            commands = list(
                self.bot.get_commands_for_message(False, {}, '',
                                                  event.message))

        # If we didn't find any matching commands, return
        if not len(commands):
            return

        event.user_level = self.get_level(event.guild,
                                          event.author) if event.guild else 0

        # Grab whether this user is a global admin
        # TODO: cache this
        global_admin = rdb.sismember('global_admins', event.author.id)

        # Iterate over commands and find a match
        for command, match in commands:
            if command.level == -1 and not global_admin:
                continue

            level = command.level

            if guild and not config and command.triggers[0] != 'setup':
                continue
            elif config and config.commands and command.plugin != self:
                overrides = {}
                for obj in config.commands.get_command_override(command):
                    overrides.update(obj)

                if overrides.get('disabled'):
                    continue

                level = overrides.get('level', level)

            if not global_admin and event.user_level < level:
                continue

            with timed('rowboat.command.duration',
                       tags={
                           'plugin': command.plugin.name,
                           'command': command.name
                       }):
                try:
                    command.plugin.execute(
                        CommandEvent(command, event.message, match))
                except CommandResponse as e:
                    return event.reply(e.response)
                except:
                    event.reply(
                        '<:{}> something went wrong, perhaps try again later'.
                        format(RED_TICK_EMOJI))
                    self.log.exception('Command error:')

            Message.update(command=command.plugin.name + ':' +
                           command.name).where(
                               (Message.id == event.message.id)).execute()

            # Dispatch the command used modlog event
            if config:
                event.config.set(getattr(config.plugins, 'modlog', None))
                if not event.config:
                    return

                plugin = self.bot.plugins.get('ModLogPlugin')
                if plugin:
                    plugin.log_action(Actions.COMMAND_USED, event)

            return