Exemplo n.º 1
0
    async def message(self, ctx, *messages: MessageConv):
        """
        Finds and prints the contents of the messages with the given IDs.
        """

        logger.info("Displaying %d messages", len(messages))

        if not mod_perm(ctx) and len(messages) > 3:
            messages = islice(messages, 0, 3)
            await ctx.send(
                content="Too many messages requested, stopping at 3...")

        def make_embed(message):
            embed = discord.Embed(colour=message.author.colour)
            embed.description = message.content or None
            embed.timestamp = message.created_at
            embed.url = message.jump_url
            embed.set_author(
                name=f"{message.author.name}#{message.author.discriminator}")
            embed.set_thumbnail(url=message.author.avatar_url)
            embed.add_field(name="Sent by", value=message.author.mention)

            if ctx.guild is not None:
                embed.add_field(name="Channel", value=message.channel.mention)

            embed.add_field(name="Permalink", value=message.jump_url)

            if message.edited_at is not None:
                delta = fancy_timedelta(message.edited_at - message.created_at)
                embed.add_field(
                    name="Edited at",
                    value=f"`{message.edited_at}` ({delta} afterwords)",
                )

            if message.attachments:
                embed.add_field(
                    name="Attachments",
                    value="\n".join(attach.url
                                    for attach in message.attachments),
                )

            if message.embeds:
                embed.add_field(name="Embeds", value=str(len(message.embeds)))

            if message.reactions:
                emojis = Counter()
                for reaction in message.reactions:
                    emojis[str(reaction.emoji)] += 1

                embed.add_field(
                    name="Reactions",
                    value="\n".join((f"{count}: {emoji}"
                                     for emoji, count in emojis.items())),
                )

            return embed

        for message in messages:
            await ctx.send(embed=make_embed(message))
Exemplo n.º 2
0
    async def pinghelpers(self, ctx):
        """Pings helpers if used in the respective channel"""

        cooldown_time = self.bot.config.helper_ping_cooldown

        logger.info(
            "User '%s' (%d) is pinging the helper role in channel '%s' in guild '%s' (%d)",
            ctx.author,
            ctx.author.mention,
            ctx.channel,
            ctx.guild,
            ctx.guild.id,
        )
        pingable_channels = self.bot.sql.roles.get_pingable_role_channels(
            ctx.guild)
        # this will return an empty list if there is nothing.
        channel_role = [(channel, role) for channel, role in pingable_channels
                        if channel == ctx.channel]

        if not channel_role:
            embed = discord.Embed(colour=discord.Colour.red())
            embed.set_author(name="Failed to ping helper role.")
            embed.description = f"There is no helper role set for this channel."
            raise CommandFailed(embed=embed)

        channel_user = (ctx.channel.id, ctx.author.id)
        cooldown = self.cooldowns.get(channel_user)

        if mod_perm(ctx) or not cooldown or cooldown <= datetime.now():
            self.cooldowns[channel_user] = datetime.now() + timedelta(
                seconds=cooldown_time)

            # This will loop over the dictionary and remove expired entries.
            key_list = list(self.cooldowns.keys())
            for k in key_list:
                if self.cooldowns[k] < datetime.now():
                    del self.cooldowns[k]

            # channel[0] will be the first tuple in the list. there will only be one, since the
            # channel's id is a primary key (tb_pingable_role_channel in roles.py). channel[0][1] is the role.
            await ctx.send(
                f"{channel_role[0][1].mention}, {ctx.author.mention} needs help."
            )

        elif cooldown > datetime.now():
            # convert deltatime into string: Hh, Mm, Ss
            time_remaining = cooldown - datetime.now()

            embed = discord.Embed(colour=discord.Colour.red())
            embed.set_author(name="Failed to ping helper role.")
            embed.description = f"You can ping the helper role for this channel again in {fancy_timedelta(time_remaining)}"
            raise CommandFailed(embed=embed)
Exemplo n.º 3
0
    async def raw_message(self, ctx, messages):
        logger.info("Outputting raw message contents for %d messages",
                    len(messages))

        if not mod_perm(ctx) and len(messages) > 4:
            messages = islice(messages, 0, 4)
            await ctx.send(
                content="Too many messages requested, stopping at 5...")

        for message in messages:
            content = (
                f"{message.author.name}#{message.author.discriminator} sent:\n"
                f"```\n{escape_backticks(message.content)}\n```")
            await ctx.send(content=content)
Exemplo n.º 4
0
    async def list_emojis(self, ctx, all_guilds=False):
        contents = []
        content = StringBuilder()

        if all_guilds:
            if not mod_perm(ctx):
                raise ManualCheckFailure(
                    content="Only moderators can do this.")

            guild_emojis = (guild.emojis for guild in self.bot.guilds)
            emojis = chain(*guild_emojis)
        else:
            emojis = ctx.guild.emojis

        logger.info("Listing all emojis within the guild")
        for emoji in emojis:
            managed = "M" if emoji.managed else ""
            content.writeln(
                f"- [{emoji}]({emoji.url}) id: `{emoji.id}`, name: `{emoji.name}` {managed}"
            )

            if len(content) > 1900:
                # Too long, break into new embed
                contents.append(str(content))

                # Start content over
                content.clear()

        if content:
            contents.append(str(content))

        for i, content in enumerate(contents):
            embed = discord.Embed(description=content,
                                  colour=discord.Colour.dark_teal())
            embed.set_footer(text=f"Page {i + 1}/{len(contents)}")

            if i == 0:
                if all_guilds:
                    embed.set_author(name="Emojis in all guilds")
                else:
                    embed.set_author(name=f"Emojis within {ctx.guild.name}")

            await ctx.send(embed=embed)
Exemplo n.º 5
0
    async def mentionable_prefix(self, ctx, value: int = None):
        """
        Gets the current number of typeable characters required for your mention.
        If you're a moderator, you can change this value. (Set to 0 to disable)
        """

        if value is None:
            # Get prefix
            value = self.bot.sql.settings.get_mentionable_name_prefix(
                ctx.guild)
            embed = discord.Embed(colour=discord.Colour.dark_teal())
            embed.description = (
                f"Names must begin with at least {value} typeable character{plural(value)}"
                if value else "No guild requirement for mentionable names")
        elif not mod_perm(ctx):
            # Lacking authority to set mentionable prefix
            embed = discord.Embed(colour=discord.Colour.red())
            embed.description = (
                "You do not have permission to set the mentionable name prefix"
            )
            raise ManualCheckFailure(embed=embed)
        else:
            # Set prefix
            if value < 0 or value > 32:
                embed = discord.Embed()
                embed.colour = discord.Colour.red()
                embed.description = "Prefix lengths must be between `0` and `32`."
                raise CommandFailed(embed=embed)

            with self.bot.sql.transaction():
                self.bot.sql.settings.set_mentionable_name_prefix(
                    ctx.guild, value)

            embed = discord.Embed(colour=discord.Colour.dark_teal())
            embed.description = (
                f"Set mentionable prefix to {value} character{plural(value)}"
                if value else "Disabled mentionable prefix requirement")

        await ctx.send(embed=embed)
Exemplo n.º 6
0
    async def prefix(self, ctx, *, prefix: str = None):
        """
        Gets the current prefix. If you're a moderator, you can set it too.
        A trailing underscore is converted into spaces. A single '_' unsets
        the bot's prefix, and uses the default one.
        """

        if prefix is None:
            # Get prefix
            bot_prefix = self.bot.prefix(ctx.guild)
            embed = discord.Embed(colour=discord.Colour.dark_teal())
            if ctx.guild is None:
                embed.description = "No command prefix, all messages are commands"
            else:
                embed.description = f"Prefix for {ctx.guild.name} is `{bot_prefix}`"
        elif ctx.guild is None and prefix is not None:
            # Attempt to set prefix outside of guild
            embed = discord.Embed(colour=discord.Colour.red())
            embed.description = "Cannot set a command prefix outside of a server!"
            raise CommandFailed(embed=embed)
        elif not mod_perm(ctx):
            # Lacking authority to set prefix
            embed = discord.Embed(colour=discord.Colour.red())
            embed.description = "You do not have permission to set the prefix"
            raise ManualCheckFailure(embed=embed)
        elif prefix == "_":
            # Unset prefix
            with self.bot.sql.transaction():
                self.bot.sql.settings.set_prefix(ctx.guild, None)
                bot_prefix = self.bot.prefix(ctx.guild)

            embed = discord.Embed(colour=discord.Colour.dark_teal())
            embed.description = (
                f"Unset prefix for {ctx.guild.name}. (Default prefix: `{bot_prefix}`)"
            )
            self.journal.send(
                "prefix",
                ctx.guild,
                "Unset bot command prefix",
                icon="settings",
                prefix=None,
                default_prefix=self.bot.config.default_prefix,
            )
        else:
            # Set prefix
            bot_prefix = re.sub(r"_$", " ", prefix)
            with self.bot.sql.transaction():
                self.bot.sql.settings.set_prefix(ctx.guild, bot_prefix)

            embed = discord.Embed(colour=discord.Colour.dark_teal())
            embed.description = f"Set prefix for {ctx.guild.name} to `{bot_prefix}`"
            self.journal.send(
                "prefix",
                ctx.guild,
                "Unset bot command prefix",
                icon="settings",
                prefix=bot_prefix,
                default_prefix=self.bot.config.default_prefix,
            )

        await ctx.send(embed=embed)