Passed
Branch BonHowi (f3d30b)
by Bartosz
01:51 queued 14s
created

build.cogs.spotcog.SpotCog.__init__()   A

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
"""
2
Cog with role related commands available in the Bot.
3
4
Current commands:
5
/remove_spot
6
7
"""
8
import discord
9
from discord.ext import commands
10
from discord.utils import get
11
import cogs.cogbase as cogbase
12
from cogs.databasecog import DatabaseCog
13
14
monster_type_dict = {0: "rare", 1: "legendary", 2: "event1", 3: "event2", 4: "common"}
15
16
prefix = "/"
17
cords_beginning = ["-", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
18
19
20
class SpotCog(cogbase.BaseCog):
21
    def __init__(self, base):
22
        super().__init__(base)
23
24
    # Ping monster role
25
    @commands.Cog.listener()
26
    async def on_message(self, ctx):
27
        if ctx.author.id == self.bot.user.id:
28
            return
29
30
        # If common spotted
31
        if ctx.channel.id == self.bot.ch_common:
32
            await self.handle_spotted_common(ctx)
33
        elif ctx.channel.category and ctx.channel.category.id == self.bot.cat_spotting:
34
            await self.handle_spotted_monster(ctx)
35
36
    @staticmethod
37
    async def handle_spotted_common(ctx):
38
        if ctx.content[0] in cords_beginning:
39
            await DatabaseCog.db_count_spot(ctx.author.id, "common")
40
            await DatabaseCog.db_save_coords(ctx.content, "common")
41
        else:
42
            await ctx.delete()
43
44
    # TODO: spaghetti code
45
    async def handle_spotted_monster(self, ctx):
46
        peepo_ban_emote = ":peepoban:872502800146382898"
47
        if ctx.content.startswith(prefix):
48
            spotted_monster = self.get_monster(ctx, ctx.content.replace(prefix, ""))
49
            if spotted_monster:
50
                monster_type_str = monster_type_dict[spotted_monster["type"]]
51
                if ctx.channel.id in [self.bot.ch_legendary_spot, self.bot.ch_rare_spot]:
52
                    if ctx.channel.name != monster_type_str:
53
                        channel = discord.utils.get(ctx.guild.channels, name=monster_type_str)
54
                        correct_channel = channel.id
55
                        await ctx.delete()
56
                        await ctx.channel.send(
57
                            f"{ctx.author.mention} you posted on wrong channel! "
58
                            f"Use <#{correct_channel}> instead! <{peepo_ban_emote}>",
59
                            delete_after=8)
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)
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 == self.bot.ch_legendary_spot or ctx.channel.id == self.bot.ch_rare_spot:
76
            await ctx.add_reaction(f"a{peepo_ban_emote}")
77
78
79
def setup(bot: commands.Bot):
80
    bot.add_cog(SpotCog(bot))
81