Skip to content

Commit 5ccadc9

Browse files
authored
Implement cogs (#14)
1 parent 87459c2 commit 5ccadc9

File tree

2 files changed

+117
-84
lines changed

2 files changed

+117
-84
lines changed

bot/client.py

+24-84
Original file line numberDiff line numberDiff line change
@@ -1,98 +1,38 @@
1+
import os
2+
13
import discord
24
from discord.ext import commands
3-
from enum import Enum, unique
45

56
from bot import VERSION
67

7-
client = commands.Bot(command_prefix=commands.when_mentioned_or('$'),
8-
help_command=None,
9-
intents=discord.Intents.all(),
10-
case_insensitive=True)
11-
12-
13-
@unique
14-
class Cinturoes(Enum):
15-
Branco = 1
16-
Amarelo = 2
17-
Azul = 3
18-
Verde = 4
19-
Laranja = 5
20-
Vermelho = 6
21-
Roxo = 7
22-
Preto = 8
23-
24-
25-
class Ninja_data():
26-
def __init__(self, guild, member):
27-
self.guild = guild
28-
self.member = member
29-
30-
def current_belt(self):
31-
for belt in self.member.roles:
32-
for cinturao in Cinturoes:
33-
if cinturao.name == belt.name:
34-
return belt
35-
36-
def next_belt(self):
37-
for belt in self.member.roles:
38-
for cinturao in Cinturoes:
39-
if cinturao.name == belt.name:
40-
next_cinturao_value = cinturao.value + 1
41-
return get_role_from_name(
42-
self.guild,
43-
Cinturoes(next_cinturao_value).name)
44-
45-
46-
def get_role_from_name(guild, belt_name):
47-
for role in guild.roles:
48-
if role.name == belt_name:
49-
return role
50-
8+
client = commands.Bot(
9+
command_prefix = commands.when_mentioned_or('$'),
10+
help_command = None,
11+
intents = discord.Intents.all(),
12+
case_insensitive = True
13+
)
5114

5215
@client.event
5316
async def on_ready():
5417
print(f'We have logged in as {client.user}')
5518

56-
5719
@client.command()
58-
@commands.has_any_role("🛡️ Admin", "🏆 Champion", "🧑‍🏫 Mentores")
59-
async def promove(ctx, user, belt):
60-
await ctx.send(f"<@{ctx.message.author.id}> quer promover {user} a {belt}")
61-
62-
mentions = ctx.message.raw_mentions #List of id mentions in the command
63-
guild = ctx.guild #Guild of the command
64-
member = guild.get_member(
65-
mentions[0]) #gets the member of the mentioned id
66-
67-
ninja = Ninja_data(guild, member)
68-
69-
if belt == "Branco":
70-
71-
cinturao = get_role_from_name(guild, "Branco").id
72-
await member.add_roles(guild.get_role(cinturao),
73-
reason=None,
74-
atomic=True)
75-
76-
await ctx.send(f'{user} agora és cinturão {belt}')
20+
async def load(ctx, extension):
21+
client.load_extension(f'bot.cogs.{extension}')
22+
23+
await ctx.send(
24+
f'O cog {extension} foi ativado.'
25+
)
7726

78-
elif belt == ninja.current_belt().name:
79-
80-
await ctx.send(
81-
f'<@{ctx.message.author.id}> esse já é o cinturão atual do ninja {user}'
82-
)
83-
84-
elif ninja.next_belt().name == belt:
85-
86-
await member.add_roles(guild.get_role(ninja.next_belt().id),
87-
reason=None,
88-
atomic=True)
89-
await member.remove_roles(guild.get_role(ninja.current_belt().id),
90-
reason=None,
91-
atomic=True)
27+
@client.command()
28+
async def unload(ctx, extension):
29+
client.unload_extension(f'bot.cogs.{extension}')
30+
31+
await ctx.send(
32+
f'O cog {extension} foi desativado.'
33+
)
9234

93-
await ctx.send(f'{user} agora és cinturão {belt}')
35+
for filename in os.listdir('bot/cogs'):
36+
if filename.endswith('.py'):
37+
client.load_extension(f'bot.cogs.{filename[:-3]}')
9438

95-
else:
96-
await ctx.send(
97-
f'<@{ctx.message.author.id}> esse cinturão não é valido de se ser atribuido'
98-
)

bot/cogs/belts.py

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
from enum import Enum, unique
2+
3+
import discord
4+
from discord.ext import commands
5+
6+
@unique
7+
class Belts(Enum):
8+
Branco = 1
9+
Amarelo = 2
10+
Azul = 3
11+
Verde = 4
12+
Laranja = 5
13+
Vermelho = 6
14+
Roxo = 7
15+
Preto = 8
16+
17+
class Ninja():
18+
def __init__(self, guild, member):
19+
self.guild = guild
20+
self.member = member
21+
self.roles = [role for role in member.roles]
22+
23+
def current_belt(self):
24+
highest_belt = None
25+
for role in self.roles:
26+
for belt in Belts:
27+
if belt.name == role.name:
28+
highest_belt = belt
29+
30+
return highest_belt
31+
32+
def next_belt(self):
33+
# Check if the maximum range has been exceeded
34+
value = self.current_belt().value + 1 if self.current_belt().value < 8 else 8
35+
36+
return Belts(value)
37+
38+
@staticmethod
39+
def get_role_from_name(guild, belt):
40+
for role in guild.roles:
41+
if role.name == belt:
42+
return role
43+
44+
class BeltsAttributions(commands.Cog):
45+
def __init__(self, client):
46+
self.client = client
47+
48+
@commands.command(name = 'promove')
49+
@commands.has_any_role("🛡️ Admin", "🏆 Champion", "🧑‍🏫 Mentores")
50+
async def promove(self, ctx, user, belt):
51+
52+
mentions = ctx.message.raw_mentions
53+
guild = ctx.guild
54+
member = guild.get_member(mentions[0])
55+
ninja = Ninja(guild, member)
56+
57+
if belt == "Branco" and ninja.current_belt() == None:
58+
role = Ninja.get_role_from_name(guild, belt)
59+
60+
await member.add_roles(
61+
guild.get_role(role.id),
62+
reason = None,
63+
atomic = True
64+
)
65+
66+
await ctx.send(
67+
f'{user} agora és cinturão {belt} :tada:'
68+
)
69+
70+
elif belt == ninja.current_belt().name:
71+
await ctx.reply(
72+
f"Esse já é o cinturão do ninja {user}!"
73+
)
74+
75+
elif belt == ninja.next_belt().name:
76+
role = Ninja.get_role_from_name(guild, belt)
77+
await member.add_roles(
78+
guild.get_role(role.id),
79+
reason = None,
80+
atomic = True
81+
)
82+
83+
await ctx.send(
84+
f'{user} agora és cinturão {belt} :tada:'
85+
)
86+
87+
elif belt != ninja.next_belt().name:
88+
await ctx.send(
89+
f'{user} esse cinturão não é valido de se ser atribuido.'
90+
)
91+
92+
def setup(client):
93+
client.add_cog(BeltsAttributions(client))

0 commit comments

Comments
 (0)