Passed
Branch BonHowi (9f1998)
by Bartosz
01:30
created

SpotStatssCog.update_spot_stats_loop()   A

Complexity

Conditions 1

Size

Total Lines 8
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nop 1
1
import discord
2
from discord.ext import commands, tasks
3
from discord.utils import get
4
from cogs import cogbase
5
from collections import Counter, OrderedDict
6
7
8
class SpotStatssCog(cogbase.BaseCog):
9
    def __init__(self, base):
10
        super().__init__(base)
11
        self.update_spot_stats_loop.start()
12
13
    async def update_spot_stats(self, channel_id: int, channel_type: int):
14
        spot_stats_ch = self.bot.get_channel(self.bot.ch_spotting_stats)
15
        if channel_type == 1:
16
            embed_title = "LEGENDARY"
17
            embed_color = int('%02x%02x%02x' % (163, 140, 21), 16)
18
        elif channel_type == 0:
19
            embed_title = "RARE"
20
            embed_color = int('%02x%02x%02x' % (17, 93, 178), 16)
21
        else:
22
            embed_title = "???"
23
            embed_color = int('%02x%02x%02x' % (1, 1, 1), 16)
24
25
        roles_main_list = await self.get_channel_history(channel_id, channel_type)
26
        roles_event_list = await self.get_channel_history(self.bot.ch_werewolf, channel_type)
27
        roles_joined_list = roles_main_list + roles_event_list
28
        roles_counter = Counter(roles_joined_list)
29
        roles_counter = OrderedDict(roles_counter.most_common())
30
        top_print = []
31
        for key, value in roles_counter.items():
32
            spotting_stats = [f"**{key}**:  {value}"]
33
            top_print.append(spotting_stats)
34
        top_print = ['\n'.join([elem for elem in sublist]) for sublist in top_print]
35
        top_print = "\n".join(top_print)
36
        embed_command = discord.Embed(title=f"{embed_title}", description=top_print, color=embed_color)
37
        await spot_stats_ch.send(embed=embed_command)
38
39
    async def get_channel_history(self, channel_id, channel_type) -> list:
40
        guild = self.bot.get_guild(self.bot.guild[0])
41
        channel = self.bot.get_channel(channel_id)
42
        roles_list = []
43
        messages = await channel.history(limit=None, oldest_first=True).flatten()
44
        for message in messages:
45
            if message.content.startswith("<@&8"):
46
                seq_type = type(message.content)
47
                role_id = int(seq_type().join(filter(seq_type.isdigit, message.content)))
48
                role = get(guild.roles, id=role_id)
49
                if role:
50
                    monster_found = None
51
                    for monster in self.bot.config["commands"]:
52
                        if monster["name"].lower() == role.name.lower() or role.name.lower() in monster["triggers"]:
53
                            monster_found = monster
54
                            break
55
                    if monster_found["type"] == channel_type:
56
                        roles_list.append(role.name)
57
        return roles_list
58
59
    @tasks.loop(hours=12)
60
    async def update_spot_stats_loop(self):
61
        spot_stats_ch = self.bot.get_channel(self.bot.ch_spotting_stats)
62
        await spot_stats_ch.purge()
63
        await self.update_spot_stats(self.bot.ch_legendary_spot, 1)
64
        await self.update_spot_stats(self.bot.ch_rare_spot, 0)
65
        dt_string = self.bot.get_current_time()
66
        print(f"({dt_string})\t[{self.__class__.__name__}]: Spotting stats updated")
67
68
    @update_spot_stats_loop.before_loop
69
    async def before_update_spot_stats_loop(self):
70
        dt_string = self.bot.get_current_time()
71
        print(f'({dt_string})\t[{self.__class__.__name__}]: Waiting until Bot is ready')
72
        await self.bot.wait_until_ready()
73
74
75
def setup(bot: commands.Bot):
76
    bot.add_cog(SpotStatssCog(bot))
77