Failed Conditions
Branch BonHowi (f075ef)
by Bartosz
01:21
created

build.cogs.spotcog.SpotCog.on_message()   B

Complexity

Conditions 6

Size

Total Lines 13
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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