Passed
Branch BonHowi (187c5b)
by Bartosz
01:31
created

SpotCog.clear_nemeton_channels_loop()   A

Complexity

Conditions 1

Size

Total Lines 7
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nop 1
1
"""
2
Cog with role related commands available in the Bot.
3
4
Current commands:
5
/remove_spot
6
7
"""
8
from datetime import datetime, timedelta
9
10
import discord
11
from discord.ext import commands, tasks
12
from discord.utils import get
13
import cogs.cogbase as cogbase
14
from cogs.databasecog import DatabaseCog
15
16
monster_type_dict = {0: "rare", 1: "legendary", 2: "event1", 3: "event2", 4: "common"}
17
18
prefix = "/"
19
cords_beginning = ["-", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
20
21
22
class SpotCog(cogbase.BaseCog):
23
    def __init__(self, base):
24
        super().__init__(base)
25
        self.peepo_ban_emote = ":peepoban:872502800146382898"
26
        self.spotting_channels = [self.bot.ch_legendary_spot, self.bot.ch_rare_spot,
27
                                  self.bot.ch_legendary_nemeton, self.bot.ch_rare_nemeton]
28
29
        self.clear_nemeton_channels_loop.start()
30
31
    # Ping monster role
32
    @commands.Cog.listener()
33
    async def on_message(self, ctx):
34
        if ctx.author.id == self.bot.user.id:
35
            return
36
37
        # If common spotted
38
        try:
39
            if ctx.channel.id == self.bot.ch_common:
40
                await self.handle_spotted_common(ctx)
41
            elif ctx.channel.category and ctx.channel.category.id == self.bot.cat_spotting:
42
                await self.handle_spotted_monster(ctx)
43
        except AttributeError:
44
            pass
45
46
    @staticmethod
47
    async def handle_spotted_common(ctx):
48
        if ctx.content[0] in cords_beginning:
49
            await DatabaseCog.db_count_spot(ctx.author.id, "common", "")
50
            await DatabaseCog.db_save_coords(ctx.content, "common")
51
        else:
52
            await ctx.delete()
53
54
    async def handle_spotted_monster(self, ctx):
55
        if ctx.content.startswith(prefix):
56
            spotted_monster = self.get_monster(ctx, ctx.content.replace(prefix, ""))
57
            if spotted_monster:
58
                monster_type_str = monster_type_dict[spotted_monster["type"]]
59
                if await self.wrong_channel(ctx, spotted_monster, monster_type_str):
60
                    return
61
                role = get(ctx.guild.roles, name=spotted_monster["name"])
62
                await ctx.delete()
63
                await ctx.channel.send(f"{role.mention}")
64
                await DatabaseCog.db_count_spot(ctx.author.id,
65
                                                monster_type_str, spotted_monster["name"])
66
                logs_ch = self.bot.get_channel(self.bot.ch_logs)
67
                await logs_ch.send(f"[PingLog] {ctx.author} ({ctx.author.id}) "
68
                                   f"requested ping for **{spotted_monster['name']}**")
69
            else:
70
                await ctx.delete()
71
                await ctx.channel.send(
72
                    f"{ctx.author.mention} monster not found - are you sure that the name is correct?", delete_after=5)
73
        elif len(ctx.content) > 0 and ctx.content[0] in cords_beginning:
74
            await DatabaseCog.db_save_coords(ctx.content, ctx.channel.name)
75
        elif ctx.channel.id in self.spotting_channels:
76
            await ctx.add_reaction(f"a{self.peepo_ban_emote}")
77
78
    async def wrong_channel(self, ctx, spotted_monster, monster_type_str):
79
        if ctx.channel.id in self.spotting_channels:
80
            if monster_type_str not in ctx.channel.name:
81
                channel_wild = discord.utils.get(ctx.guild.channels, name=monster_type_str)
82
                correct_channel_wild = channel_wild.id
83
                channel_nemeton = discord.utils.get(ctx.guild.channels, name=f"{monster_type_str}-nemeton")
84
                correct_channel_nemeton = channel_nemeton.id
85
                await ctx.delete()
86
                await ctx.channel.send(
87
                    f"{ctx.author.mention} you posted {spotted_monster['name']} on wrong channel! "
88
                    f"Use <#{correct_channel_wild}> or <#{correct_channel_nemeton}> instead! <{self.peepo_ban_emote}>",
89
                    delete_after=8)
90
                return True
91
            return False
92
93
    @staticmethod
94
    async def clear_channel_messages(channel):
95
        messages = []
96
        today = datetime.utcnow()
97
        today = today.replace(hour=0, minute=0, second=0, microsecond=0)
98
        async for message in channel.history(before=today):
99
            messages.append(message)
100
        await channel.delete_messages(messages)
101
102
    @tasks.loop(hours=3)
103
    async def clear_nemeton_channels_loop(self):
104
        lege_nemeton = self.bot.get_channel(self.bot.ch_legendary_nemeton)
105
        rare_nemeton = self.bot.get_channel(self.bot.ch_rare_nemeton)
106
        await self.clear_channel_messages(lege_nemeton)
107
        await self.clear_channel_messages(rare_nemeton)
108
        self.create_log_msg("Wiped Nemeton channels")
109
110
    @clear_nemeton_channels_loop.before_loop
111
    async def before_update_leaderboards_loop(self):
112
        self.create_log_msg(f"Waiting until Bot is ready")
113
        await self.bot.wait_until_ready()
114
115
116
def setup(bot: commands.Bot):
117
    bot.add_cog(SpotCog(bot))
118