build.cogs.coordscog.CoordsCog.__init__()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 2
rs 10
c 0
b 0
f 0
cc 1
nop 2
1
from discord.ext import commands
2
from discord_slash import cog_ext, SlashContext
3
import pandas as pd
4
from discord.ext import commands
5
from discord_slash import cog_ext, SlashContext
6
7
from cogs import cogbase
8
9
10
class CoordsCog(cogbase.BaseCog):
11
    def __init__(self, base):
12
        super().__init__(base)
13
14
    @cog_ext.cog_slash(name="saveCoordinates", guild_ids=cogbase.GUILD_IDS,
15
                       description="Save spotting coordinates from channels history",
16
                       default_permission=False,
17
                       permissions=cogbase.PERMISSION_ADMINS)
18
    async def save_coordinates(self, ctx: SlashContext) -> None:
19
        await ctx.send('Coords are being saved', hidden=True)
20
        legendary_coords = await self.get_channel_history(self.bot.ch_legendary_spot)
21
        rare_coords = await self.get_channel_history(self.bot.ch_rare_spot)
22
        # werewolf_coords = await self.get_channel_history(self.bot.ch_werewolf)
23
        # wraith_coords = await self.get_channel_history(self.bot.ch_wraiths)
24
        common_coords = await self.get_channel_history(self.bot.ch_common)
25
        # TODO: why does concat create list instead of df???
26
        # coords_list = [legendary_coords, rare_coords, common_coords]
27
        # result = pd.concat(coords_list, ignore_index=True)
28
        coords_df = legendary_coords.append(rare_coords).append(common_coords)
29
        path_coords = r"server_files/coords_history.xlsx"
30
        coords_df.to_excel(path_coords, index=False)
31
        self.create_log_msg(f"Coords saved to {path_coords}")
32
33
    async def get_channel_history(self, channel_id: int) -> pd.DataFrame:
34
        channel = self.bot.get_channel(channel_id)
35
        coords_filter_list = ["0.", "1.", "2.", "3.", "4.", "5.", "6.", "7.", "8.", "9."]
36
        coords_list = []
37
        async for message in channel.history(limit=None, oldest_first=True):
38
            is_coord = [ele for ele in coords_filter_list if (ele in message.content)]
39
            if is_coord:
40
                coords_list.append(message.content)
41
        coords_list = [i.split('\n') for i in coords_list]
42
        coords_list = [item for sublist in coords_list for item in sublist]
43
        coords_list = list(filter(None, coords_list))
44
        coords_df = pd.DataFrame(coords_list, columns=["coords"])
45
        coords_df["type"] = "common" if "common" in channel.name else channel.name
46
        return coords_df
47
48
49
def setup(bot: commands.Bot):
50
    bot.add_cog(CoordsCog(bot))
51