Passed
Push — main ( c507f0...00b29a )
by Bartosz
02:41 queued 01:13
created

build.cogs.commandscog.CommandsCog.get_stats_own()   A

Complexity

Conditions 1

Size

Total Lines 6
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nop 2
1
"""
2
Cog with general commands available in the Bot.
3
"""
4
5
import discord
6
from discord.utils import get
7
from googletrans import Translator
8
import cogs.cogbase as cogbase
9
from discord.ext import commands
10
from discord_slash import cog_ext, SlashContext
11
from cogs.databasecog import DatabaseCog
12
from modules.utils import get_dominant_color
13
14
15
class CommandsCog(cogbase.BaseCog):
16
    def __init__(self, base):
17
        super().__init__(base)
18
19
    # Get own spotting stats
20
    @cog_ext.cog_slash(name="myStats", guild_ids=cogbase.GUILD_IDS,
21
                       description="Get your spotting stats",
22
                       default_permission=True)
23
    async def get_stats_own(self, ctx: SlashContext) -> None:
24
        embed = await self.get_stats(ctx.author)
25
        await ctx.send(embed=embed, hidden=True)
26
27
    # Get and show own spotting stats
28
    @cog_ext.cog_slash(name="myStatsShow", guild_ids=cogbase.GUILD_IDS,
29
                       description="Get your spotting stats and show it to other members",
30
                       default_permission=True)
31
    async def show_stats_own(self, ctx: SlashContext) -> None:
32
        embed = await self.get_stats(ctx.author)
33
        await ctx.send(embed=embed)
34
35
    # Get member's spotting stats
36
    @cog_ext.cog_slash(name="memberStats", guild_ids=cogbase.GUILD_IDS,
37
                       description="Get member's spotting stats",
38
                       default_permission=False,
39
                       permissions=cogbase.PERMISSION_MODS)
40
    async def get_stats_member(self, ctx: SlashContext, member: discord.Member) -> None:
41
        embed = await self.get_stats(member)
42
        await ctx.send(embed=embed, hidden=True)
43
44
    async def get_stats(self, member: discord.Member) -> discord.Embed:
45
        spot_roles = self.bot.config["total_milestones"][0]
46
        guild = self.bot.get_guild(self.bot.guild[0])
47
        spots_df = await DatabaseCog.db_get_member_stats(member.id)
48
        spots_df["total"] = spots_df["legendary"] * self.legend_multiplier + spots_df["rare"]
49
50
        role_next = ""
51
        spots_for_new = ""
52
        roles_list = [key for (key, value) in spot_roles.items() if spots_df.at[0, "total"] < value]
53
        values_list = [value for (key, value) in spot_roles.items() if spots_df.at[0, "total"] < value]
54
        if roles_list:
55
            role_next = get(guild.roles, name=roles_list[0])
56
            spots_for_new = values_list[0]
57
58
        message: str = f"**Legends**: {spots_df.at[0, 'legendary']}\n" \
59
                       f"**Rares**: {spots_df.at[0, 'rare']}\n" \
60
                       f"**Commons**: {spots_df.at[0, 'common']}\n\n" \
61
                       f"**Total points**: {spots_df.at[0, 'total']}\n" \
62
                       f"**Progress**: {spots_df.at[0, 'total']}/{spots_for_new}\n" \
63
                       f"**Next role**: {role_next.name}"
64
65
        member_color = get_dominant_color(member.avatar_url)
66
        embed = discord.Embed(title=f"{member} spotting stats", description=message,
67
                              color=member_color)
68
        embed.set_thumbnail(url=f'{member.avatar_url}')
69
        return embed
70
71
    # Pool
72
    @cog_ext.cog_slash(name="poll", guild_ids=cogbase.GUILD_IDS,
73
                       description="Create pool",
74
                       default_permission=False,
75
                       permissions=cogbase.PERMISSION_MODS)
76
    async def poll(self, ctx: SlashContext, *, title: str, content: str = "") -> None:
77
        author_color = get_dominant_color(ctx.author.avatar_url)
78
        emb = (discord.Embed(title=f"{title}", description=content, color=author_color))
79
        try:
80
            poll_message = await ctx.send(embed=emb)
81
            await poll_message.add_reaction("\N{THUMBS UP SIGN}")
82
            await poll_message.add_reaction("\N{THUMBS DOWN SIGN}")
83
        except Exception as e:
84
            await ctx.send(f"Oops, I couldn't react to the poll. Check that I have permission to add reactions! "
85
                           f"```py\n{e}```")
86
87
    # Translate
88
    @cog_ext.cog_slash(name="translate", guild_ids=cogbase.GUILD_IDS,
89
                       description="Translate message",
90
                       default_permission=False,
91
                       permissions=cogbase.PERMISSION_MODS)
92
    async def translate(self, ctx: SlashContext, message: str) -> None:
93
        # Translates the language and converts it to English
94
        translator = Translator()
95
        translated_message = translator.translate(message)
96
        await ctx.send(f"`{message}` -> `{translated_message.text}`", hidden=True)
97
98
99
def setup(bot: commands.Bot) -> None:
100
    bot.add_cog(CommandsCog(bot))
101