Passed
Push — main ( 530a31...c507f0 )
by Bartosz
02:42 queued 01:19
created

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

Complexity

Conditions 3

Size

Total Lines 11
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 11
nop 4
dl 0
loc 11
rs 9.85
c 0
b 0
f 0
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
        self.peepo_ban_emote = ":peepoban:872502800146382898"
24
25
    # Ping monster role
26
    @commands.Cog.listener()
27
    async def on_message(self, ctx):
28
        if ctx.author.id == self.bot.user.id:
29
            return
30
31
        # If common spotted
32
        if ctx.channel.id == self.bot.ch_common:
33
            await self.handle_spotted_common(ctx)
34
        elif ctx.channel.category and ctx.channel.category.id == self.bot.cat_spotting:
35
            await self.handle_spotted_monster(ctx)
36
37
    @staticmethod
38
    async def handle_spotted_common(ctx):
39
        if ctx.content[0] in cords_beginning:
40
            await DatabaseCog.db_count_spot(ctx.author.id, "common")
41
            await DatabaseCog.db_save_coords(ctx.content, "common")
42
        else:
43
            await ctx.delete()
44
45
    # TODO: spaghetti code
46
    async def handle_spotted_monster(self, ctx):
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 await self.wrong_channel(ctx, spotted_monster, monster_type_str):
52
                    return
53
                role = get(ctx.guild.roles, name=spotted_monster["name"])
54
                await ctx.delete()
55
                await ctx.channel.send(f"{role.mention}")
56
                await DatabaseCog.db_count_spot(ctx.author.id,
57
                                                monster_type_str)
58
                logs_ch = self.bot.get_channel(self.bot.ch_logs)
59
                await logs_ch.send(f"[PingLog] {ctx.author} ({ctx.author.id}) "
60
                                   f"requested ping for **{spotted_monster['name']}**")
61
            else:
62
                await ctx.delete()
63
                await ctx.channel.send(
64
                    f"{ctx.author.mention} monster not found - are you sure that the name is correct?", delete_after=5)
65
        elif len(ctx.content) > 0 and ctx.content[0] in cords_beginning:
66
            await DatabaseCog.db_save_coords(ctx.content, ctx.channel.name)
67
        elif ctx.channel.id == self.bot.ch_legendary_spot or ctx.channel.id == self.bot.ch_rare_spot:
68
            await ctx.add_reaction(f"a{self.peepo_ban_emote}")
69
70
    async def wrong_channel(self, ctx, spotted_monster, monster_type_str):
71
        if ctx.channel.id in [self.bot.ch_legendary_spot, self.bot.ch_rare_spot]:
72
            if ctx.channel.name != monster_type_str:
73
                channel = discord.utils.get(ctx.guild.channels, name=monster_type_str)
74
                correct_channel = channel.id
75
                await ctx.delete()
76
                await ctx.channel.send(
77
                    f"{ctx.author.mention} you posted {spotted_monster['name']} on wrong channel! "
78
                    f"Use <#{correct_channel}> instead! <{self.peepo_ban_emote}>", delete_after=8)
79
                return True
80
            return False
81
82
83
def setup(bot: commands.Bot):
84
    bot.add_cog(SpotCog(bot))
85