Passed
Branch BonHowi (8c9655)
by Bartosz
02:00
created

build.cogs.spotstatscog   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 15
eloc 63
dl 0
loc 70
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
C SpotStatssCog.update_spot_stats() 0 38 11
A SpotStatssCog.__init__() 0 3 1
A SpotStatssCog.before_update_spot_stats_loop() 0 5 1
A SpotStatssCog.update_spot_stats_loop() 0 8 1

1 Function

Rating   Name   Duplication   Size   Complexity  
A setup() 0 2 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
        guild = self.bot.get_guild(self.bot.guild[0])
15
        spot_stats_ch = self.bot.get_channel(self.bot.ch_spotting_stats)
16
        channel = self.bot.get_channel(channel_id)
17
        if channel_type == 1:
18
            embed_title = "LEGENDARY"
19
            embed_color = int('%02x%02x%02x' % (163, 140, 21), 16)
20
        elif channel_type == 0:
21
            embed_title = "RARE"
22
            embed_color = int('%02x%02x%02x' % (17, 93, 178), 16)
23
        else:
24
            embed_title = "???"
25
            embed_color = int('%02x%02x%02x' % (1, 1, 1), 16)
26
        roles_list = []
27
        messages = await channel.history(limit=None, oldest_first=True).flatten()
28
        for message in messages:
29
            if message.content.startswith("<@&8"):
30
                seq_type = type(message.content)
31
                role_id = int(seq_type().join(filter(seq_type.isdigit, message.content)))
32
                role = get(guild.roles, id=role_id)
33
                if role:
34
                    monster_found = None
35
                    for monster in self.bot.config["commands"]:
36
                        if monster["name"].lower() == role.name.lower() or role.name.lower() in monster["triggers"]:
37
                            monster_found = monster
38
                            break
39
                    if monster_found["type"] == channel_type:
40
                        roles_list.append(role.name)
41
        roles_counter = Counter(roles_list)
42
        roles_counter = OrderedDict(roles_counter.most_common())
43
        top_print = []
44
        for key, value in roles_counter.items():
45
            spotting_stats = [f"**{key}**:  {value}"]
46
            top_print.append(spotting_stats)
47
        top_print = ['\n'.join([elem for elem in sublist]) for sublist in top_print]
48
        top_print = "\n".join(top_print)
49
        embed_command = discord.Embed(title=f"{embed_title}", description=top_print, color=embed_color)
50
        await spot_stats_ch.send(embed=embed_command)
51
52
    @tasks.loop(hours=12)
53
    async def update_spot_stats_loop(self):
54
        spot_stats_ch = self.bot.get_channel(self.bot.ch_spotting_stats)
55
        await spot_stats_ch.purge()
56
        await self.update_spot_stats(self.bot.ch_legendary_spot, 1)
57
        await self.update_spot_stats(self.bot.ch_rare_spot, 0)
58
        dt_string = self.bot.get_current_time()
59
        print(f"({dt_string})\t[{self.__class__.__name__}]: Spotting stats updated")
60
61
    @update_spot_stats_loop.before_loop
62
    async def before_update_spot_stats_loop(self):
63
        dt_string = self.bot.get_current_time()
64
        print(f'({dt_string})\t[{self.__class__.__name__}]: Waiting until Bot is ready')
65
        await self.bot.wait_until_ready()
66
67
68
def setup(bot: commands.Bot):
69
    bot.add_cog(SpotStatssCog(bot))
70