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_werewolf_list = await self.get_channel_history(self.bot.ch_werewolf, channel_type) |
27
|
|
|
roles_wraith_list = await self.get_channel_history(self.bot.ch_wraiths, channel_type) |
28
|
|
|
roles_joined_list = roles_main_list + roles_werewolf_list + roles_wraith_list |
29
|
|
|
roles_counter = Counter(roles_joined_list) |
30
|
|
|
roles_counter = OrderedDict(roles_counter.most_common()) |
31
|
|
|
top_print = [] |
32
|
|
|
for key, value in roles_counter.items(): |
33
|
|
|
spotting_stats = [f"**{key}**: {value}"] |
34
|
|
|
top_print.append(spotting_stats) |
35
|
|
|
top_print = ['\n'.join([elem for elem in sublist]) for sublist in top_print] |
36
|
|
|
top_print = "\n".join(top_print) |
37
|
|
|
embed_command = discord.Embed(title=f"{embed_title}", description=top_print, color=embed_color) |
38
|
|
|
dt_string = self.bot.get_current_time() |
39
|
|
|
embed_command.set_footer(text=f"Last updated: {dt_string}") |
40
|
|
|
await spot_stats_ch.send(embed=embed_command) |
41
|
|
|
|
42
|
|
|
channel = self.bot.get_channel(channel_id) |
43
|
|
|
print(f"({dt_string})\t[{self.__class__.__name__}]: Spotting stats updated - {channel.name}") |
44
|
|
|
|
45
|
|
|
async def get_channel_history(self, channel_id, channel_type) -> list: |
46
|
|
|
guild = self.bot.get_guild(self.bot.guild[0]) |
47
|
|
|
channel = self.bot.get_channel(channel_id) |
48
|
|
|
roles_list = [] |
49
|
|
|
messages = await channel.history(limit=None, oldest_first=True).flatten() |
50
|
|
|
for message in messages: |
51
|
|
|
if message.content.startswith("<@&8"): # If message is a ping for a role |
52
|
|
|
seq_type = type(message.content) |
53
|
|
|
role_id = int(seq_type().join(filter(seq_type.isdigit, message.content))) |
54
|
|
|
role = get(guild.roles, id=role_id) |
55
|
|
|
if role: |
56
|
|
|
roles_list.append(self.create_spotted_list(role, channel_type)) |
57
|
|
|
roles_list = list(filter(None, roles_list)) |
58
|
|
|
return roles_list |
59
|
|
|
|
60
|
|
|
def create_spotted_list(self, role, channel_type): |
61
|
|
|
monster_found = None |
62
|
|
|
for monster in self.bot.config["commands"]: |
63
|
|
|
if monster["name"].lower() == role.name.lower() or role.name.lower() in monster["triggers"]: |
64
|
|
|
monster_found = monster |
65
|
|
|
break |
66
|
|
|
if monster_found["type"] == channel_type: |
67
|
|
|
return role.name |
68
|
|
|
return |
69
|
|
|
|
70
|
|
|
@tasks.loop(hours=12) |
71
|
|
|
async def update_spot_stats_loop(self): |
72
|
|
|
spot_stats_ch = self.bot.get_channel(self.bot.ch_spotting_stats) |
73
|
|
|
await spot_stats_ch.purge() |
74
|
|
|
await self.update_spot_stats(self.bot.ch_legendary_spot, 1) |
75
|
|
|
await self.update_spot_stats(self.bot.ch_rare_spot, 0) |
76
|
|
|
dt_string = self.bot.get_current_time() |
77
|
|
|
print(f"({dt_string})\t[{self.__class__.__name__}]: All spotting stats updated") |
78
|
|
|
|
79
|
|
|
@update_spot_stats_loop.before_loop |
80
|
|
|
async def before_update_spot_stats_loop(self): |
81
|
|
|
dt_string = self.bot.get_current_time() |
82
|
|
|
print(f'({dt_string})\t[{self.__class__.__name__}]: Waiting until Bot is ready') |
83
|
|
|
await self.bot.wait_until_ready() |
84
|
|
|
|
85
|
|
|
|
86
|
|
|
def setup(bot: commands.Bot): |
87
|
|
|
bot.add_cog(SpotStatssCog(bot)) |
88
|
|
|
|