aboutsummaryrefslogtreecommitdiffstats
path: root/bot/exts/evergreen/emoji_count.py
blob: 95e4c92709b82e9851c86c2ffce3a8cddcf227a7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import datetime
import logging
import random
from typing import Dict, Optional

import discord
from discord.ext import commands

from bot.constants import Colours

log = logging.getLogger(__name__)


class EmojiCount(commands.Cog):
    """Command that give random emoji based on category."""

    def __init__(self, bot: commands.Bot):
        self.bot = bot

    def embed_builder(self, emoji: dict) -> discord.Embed:
        """Genrates embed with emoji name and count."""
        embed = discord.Embed()
        embed.color = Colours.orange
        embed.title = "Emoji Count"
        embed.timestamp = datetime.datetime.utcnow()
        if len(emoji) == 1:
            for key, value in emoji.items():
                print(key)
                embed.description = f"There are **{len(value)}** emojis in the **{key}** category"
                embed.set_thumbnail(url=random.choice(value).url)
        else:
            msg = ''
            for key, value in emoji.items():
                emoji_choice = random.choice(value)
                emoji_info = f'There are **{len(value)}** emojis in the **{key}** category\n'
                msg += f'<:{emoji_choice.name}:{emoji_choice.id}> {emoji_info}'
            embed.description = msg
        log.trace('Emoji embed sent')
        return embed

    @staticmethod
    def generate_invalid_embed(ctx: commands.Context) -> discord.Embed:
        """Genrates error embed."""
        embed = discord.Embed()
        embed.color = Colours.soft_red
        embed.title = "Invalid Input"
        emoji_dict = {}
        for emoji in ctx.guild.emojis:
            emoji_dict.update({emoji.name.split("_")[0]: []})
        error_comp = ', '.join(key for key in emoji_dict.keys())
        embed.description = f"These are the valid categories\n```{error_comp}```"
        log.trace("Error embed sent")
        return embed

    def emoji_list(self, ctx: commands.Context, emojis: dict) -> Dict:
        """Genrates dictionary of emojis given by the user."""
        for emoji in ctx.guild.emojis:
            for key, value in emojis.items():
                if emoji.name.split("_")[0] == key:
                    value.append(emoji)
        log.trace("Emoji dict sent")
        return emojis

    @commands.command(name="emoji_count", aliases=["ec"])
    async def ec(self, ctx: commands.Context, *, emoji: str = None) -> Optional[str]:
        """Returns embed with emoji category and info given by user."""
        emoji_dict = {}
        for a in ctx.guild.emojis:
            if emoji is None:
                log.trace("Emoji Category doesn't provided by the user")
                emoji_dict.update({a.name.split("_")[0]: []})
            elif a.name.split("_")[0] in emoji:
                log.trace("Emoji Category provided by the user")
                emoji_dict.update({a.name.split("_")[0]: []})
        emoji_dict = self.emoji_list(ctx, emoji_dict)
        if len(emoji_dict) == 0:
            embed = self.generate_invalid_embed(ctx)
            log.trace("Error embed received")
        else:
            embed = self.embed_builder(emoji_dict)
            log.trace("Emoji embed received")
        await ctx.send(embed=embed)
        log.trace("Embed sent")


def setup(bot: commands.Bot) -> None:
    """Emoji Count Cog load."""
    bot.add_cog(EmojiCount(bot))