Passed
Push — main ( d12a1e...942891 )
by Bartosz
02:45 queued 01:23
created

build.cogs.spotcog   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 16
eloc 45
dl 0
loc 69
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A SpotCog.__init__() 0 2 1
A SpotCog.handle_spotted_common() 0 7 2
A SpotCog.on_message() 0 10 5
B SpotCog.handle_spotted_monster() 0 20 7

1 Function

Rating   Name   Duplication   Size   Complexity  
A setup() 0 2 1
1
"""
2
Cog with role related commands available in the Bot.
3
4
Current commands:
5
/remove_spot
6
7
"""
8
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, monster_type_dict[4])
40
            await DatabaseCog.db_save_coords(ctx.content, monster_type_dict[4])
41
        else:
42
            await ctx.delete()
43
44
    # TODO: spaghetti code
45
    async def handle_spotted_monster(self, ctx):
46
        if ctx.content.startswith(prefix):
47
            spotted_monster = self.get_monster(ctx, ctx.content.replace(prefix, ""))
48
            if spotted_monster:
49
                role = get(ctx.guild.roles, name=spotted_monster["name"])
50
                await ctx.delete()
51
                await ctx.channel.send(f"{role.mention}")
52
                await DatabaseCog.db_count_spot(ctx.author.id,
53
                                                monster_type_dict[spotted_monster["type"]])
54
                logs_ch = self.bot.get_channel(self.bot.ch_logs)
55
                await logs_ch.send(f"[PingLog] {ctx.author} ({ctx.author.id}) "
56
                                   f"requested ping for **{spotted_monster['name']}** role")
57
            else:
58
                await ctx.delete()
59
                await ctx.channel.send(
60
                    f"{ctx.author.mention} monster not found - are you sure that the name is correct?", delete_after=5)
61
        elif len(ctx.content) > 0 and ctx.content[0] in cords_beginning:
62
            await DatabaseCog.db_save_coords(ctx.content, ctx.channel.name)
63
        elif ctx.channel.id != self.bot.ch_werewolf and ctx.channel.id != self.bot.ch_nemeton:
64
            await ctx.add_reaction("a:peepoban:872502800146382898")
65
66
67
def setup(bot: commands.Bot):
68
    bot.add_cog(SpotCog(bot))
69