1
1
import json
2
+ import time
3
+ from datetime import date
2
4
from enum import Enum , unique
3
5
4
6
import discord
5
7
from discord .ext import commands
8
+ from sqlalchemy import create_engine
9
+ from sqlalchemy .orm import sessionmaker
10
+
11
+ from bot .cogs .constants import Belts , get_role_from_name , translator_to_emoji
12
+ from bot .cogs .logs import AttributionLogs , Base , log_attribution
13
+
14
+ # sqlalchemy setup
15
+ engine = create_engine ("sqlite:///daily_logs.db" )
16
+
17
+ Base .metadata .bind = engine
18
+
19
+ DBSession = sessionmaker (bind = engine )
20
+
21
+ session = DBSession ()
6
22
7
23
8
24
class FileHandler :
25
+ """
26
+ This is a class to handle a json file.
27
+
28
+ Attributes:
29
+ file (string): The path to the json file being handled.
30
+ """
31
+
9
32
file = "bot/cogs/belts.json"
10
33
11
- def __init__ (self , belt ):
34
+ def __init__ (self : str , belt : str ):
35
+ """
36
+ The constructor for the FileHandler class.
37
+
38
+ Parameters:
39
+ color (int): Color code to be displayed in discord embed.
40
+ """
12
41
self .belt = belt
13
42
self .msg = self .get_info ()[0 ]
14
43
self .color = self .get_info ()[1 ]
15
44
16
- def get_info (self ):
45
+ def get_info (self ) -> tuple :
46
+ """
47
+ The function to get the info from the belts.json file.
48
+
49
+ Returns:
50
+ msg (string): Variable that contains the message of the respective belt.
51
+ color (int): Color code to be displayed in discord embed.
52
+ """
17
53
with open (self .file ) as json_file :
18
54
data = json .load (json_file )
19
55
msg = f"Subiste para { self .belt } :clap:\n \n Próximos objetivos:"
20
56
color = int (data [self .belt ]["color" ], 16 )
21
- for param in data [self .belt ]["objectives " ]:
57
+ for param in data [self .belt ]["goals " ]:
22
58
msg += "\n " + param
23
59
24
60
return (msg , color )
25
61
26
62
27
- translator_to_emoji = {
28
- "Branco" : ":white_circle:" ,
29
- "Amarelo" : ":yellow_circle:" ,
30
- "Azul" : ":blue_circle:" ,
31
- "Verde" : ":green_circle:" ,
32
- "Laranja" : ":orange_circle:" ,
33
- "Vermelho" : ":red_circle:" ,
34
- "Roxo" : ":purple_circle:" ,
35
- "Preto" : ":black_circle:" ,
36
- }
37
-
38
-
39
- @unique
40
- class Belts (Enum ):
41
- Branco = 1
42
- Amarelo = 2
43
- Azul = 3
44
- Verde = 4
45
- Laranja = 5
46
- Vermelho = 6
47
- Roxo = 7
48
- Preto = 8
49
-
50
-
51
63
class Ninja :
52
- def __init__ (self , guild , member ):
64
+ """This is a class to get information about a specific ninja."""
65
+
66
+ def __init__ (self , guild : discord .Guild , member : discord .Member ):
53
67
self .guild = guild
54
68
self .member = member
55
69
self .roles = [role for role in member .roles ]
56
70
57
- def current_belt (self ):
71
+ def current_belt (self ) -> Belts :
72
+ """This function returns the current belt of the ninja."""
73
+
58
74
highest_belt = None
59
75
for role in self .roles :
60
76
for belt in Belts :
@@ -63,34 +79,34 @@ def current_belt(self):
63
79
64
80
return highest_belt
65
81
66
- def next_belt (self ):
67
- # Check if the maximum range has been exceeded
82
+ def next_belt (self ) -> Belts :
83
+ """This function returns the next belt of the ninja."""
84
+
68
85
value = self .current_belt ().value + 1 if self .current_belt ().value < 8 else 8
69
86
70
87
return Belts (value )
71
88
72
- @staticmethod
73
- def get_role_from_name (guild , belt ):
74
- for role in guild .roles :
75
- if role .name == belt :
76
- return role
77
-
78
89
79
90
class BeltsAttributions (commands .Cog ):
80
- def __init__ (self , client ):
91
+ """This is a class to handle the attribution of belts."""
92
+
93
+ def __init__ (self , client : commands .Bot ):
81
94
self .client = client
82
95
83
96
@commands .command (name = "promove" )
84
97
@commands .has_any_role ("🛡️ Admin" , "🏆 Champion" , "🧑🏫 Mentores" )
85
- async def promove (self , ctx , user , belt ):
98
+ async def promove (
99
+ self , ctx : discord .ext .commands .Context , user : str , belt : str
100
+ ) -> None :
101
+ """This function promotes a user to the next belt."""
86
102
87
103
mentions = ctx .message .raw_mentions
88
104
guild = ctx .guild
89
105
member = guild .get_member (mentions [0 ])
90
106
ninja = Ninja (guild , member )
91
107
92
108
if belt == "Branco" and ninja .current_belt () == None :
93
- role = Ninja . get_role_from_name (guild , belt )
109
+ role = get_role_from_name (guild , belt )
94
110
95
111
await member .add_roles (guild .get_role (role .id ), reason = None , atomic = True )
96
112
@@ -109,11 +125,22 @@ async def promove(self, ctx, user, belt):
109
125
110
126
await user .send (embed = embed )
111
127
128
+ # Adding the log to the database
129
+ new_log = AttributionLogs (
130
+ ninja_id = str (member ),
131
+ mentor_id = str (ctx .author ),
132
+ belt_attributed = belt ,
133
+ timestamp = int (time .time ()),
134
+ )
135
+
136
+ session .add (new_log )
137
+ session .commit ()
138
+
112
139
elif belt == ninja .current_belt ().name :
113
140
await ctx .reply (f"Esse já é o cinturão do ninja { user } !" )
114
141
115
142
elif belt == ninja .next_belt ().name :
116
- role = Ninja . get_role_from_name (guild , belt )
143
+ role = get_role_from_name (guild , belt )
117
144
await member .add_roles (guild .get_role (role .id ), reason = None , atomic = True )
118
145
119
146
# Public message
@@ -131,9 +158,20 @@ async def promove(self, ctx, user, belt):
131
158
132
159
await user .send (embed = embed )
133
160
161
+ # Adding the log to the database
162
+ new_log = AttributionLogs (
163
+ ninja_id = str (member ),
164
+ mentor_id = str (ctx .author ),
165
+ belt_attributed = belt ,
166
+ timestamp = int (time .time ()),
167
+ )
168
+
169
+ session .add (new_log )
170
+ session .commit ()
171
+
134
172
elif belt != ninja .next_belt ().name :
135
173
await ctx .send (f"{ user } esse cinturão não é valido de se ser atribuido." )
136
174
137
175
138
- def setup (client ) :
176
+ def setup (client : commands . Bot ) -> None :
139
177
client .add_cog (BeltsAttributions (client ))
0 commit comments