Пример #1
0
 async def set_cooldown_type(self, context, type: str):
     valid_types = ['global', 'perpasta']
     if type not in valid_types:
         return
     guild = GuildModelInterface.get_or_none(guild_id=context.guild.id)
     if guild is None:
         return
     if type == 'global':
         guild.cooldown_type = guild.GLOBAL
     else:
         guild.cooldown_type = guild.PER_RESPONSE
     GuildModelInterface.save_instance(guild)
     await context.channel.send(f'Guild cooldown type changed to {type}')
     print(
         f'Guild {guild.guild_id} ({guild.guild_name}) triggered response '
         f'cooldown type updated to {type}')
Пример #2
0
 async def _set_cooldown(context, cooldown_type: int, cooldown: int):
     guild = GuildModelInterface.get_or_none(guild_id=context.guild.id)
     if guild is None:
         return
     if cooldown_type == TriggeredResponseModelInterface.TEXT:
         old_cooldown = guild.triggered_text_cooldown
         guild.triggered_text_cooldown = cooldown
     else:
         old_cooldown = guild.triggered_image_cooldown
         guild.triggered_image_cooldown = cooldown
     GuildModelInterface.save_instance(guild)
     await context.channel.send(f'Guild cooldown changed from '
                                f'{old_cooldown} to {cooldown}')
     print(
         f'Guild {guild.guild_id} ({guild.guild_name}) triggered response '
         f'cooldown (type {cooldown_type}) updated from {old_cooldown} to '
         f'{cooldown}')
Пример #3
0
async def on_message(message):
    # We don't want the bot replying to itself.
    if message.author == bot.user:
        return
    print(f'Message received, author: {message.author}, '
          f'content: {message.content}, '
          f'cleaned content: {message.clean_content}')
    profile, profile_created = DiscordProfileModelInterface.get_or_create(
        id=message.author.id, defaults={'display_name': message.author.name})
    guild, guild_created = GuildModelInterface.get_or_create(
        guild_id=message.guild.id, defaults={'guild_name': message.guild.name})
    if not guild_created and guild.guild_name != message.guild.name:
        print(f'Guild {guild.guild_id} has had its name updated and is being '
              f'updated in the database.')
        guild.guild_name = message.guild.name
        GuildModelInterface.save_instance(guild)
    if profile_created:
        print(f'Discord profile {message.author.id} has been added '
              f'to the database.')
    elif profile.display_name != message.author.name:
        print(f'User {message.author.id} updated their username and is being'
              f'updated in the database.')
        profile.display_name = message.author.name
        DiscordProfileModelInterface.save_instance(profile)
    words_in_message = message.content.lower().split(' ')
    first_word = words_in_message[0]
    punctuation_removal_translation = str.maketrans('', '', string.punctuation)
    if not (first_word.startswith('$')
            and first_word[1:] in Environment.instance().BOT_COMMANDS):
        # Local cache of words so we don't have to hit the database for
        # repeated words, like if a message is "bot bot bot bot bot dead"
        # it won't do a query for "bot" 5 times.
        checked_words = []
        for word in words_in_message:
            # Remove all punctuation and symbols.
            word = word.translate(punctuation_removal_translation)
            if word in checked_words:
                continue
            # Delete messages if they contain banned words.
            banned_word = BannedWordModelInterface.get_or_none(guild=guild,
                                                               word=word)
            if banned_word is not None:
                await message.delete()
                # Send a warning DM to the sender.
                await message.author.send(f'Don\'t be saying that stuff.')
                break
            checked_words.append(word)
            response = TriggeredResponseModelInterface.get_allowed_or_none(
                user=profile.user,
                guild=guild,
                trigger=word,
            )
            if response is not None:
                print(f'{message.author.id} triggered the "{word}" '
                      f'triggered response (type {response.type}).')
                if response.type == TriggeredResponseModelInterface.TEXT:
                    await message.channel.send(response.response)
                else:
                    data = io.BytesIO(response.image)
                    await message.channel.send(
                        file=discord.File(data, 'image.jpg'))
                # Only 1 triggered response per message.
                break
    await bot.process_commands(message)