30
30
#####################################
31
31
32
32
33
- # Load bot token from .env
34
- load_dotenv ()
35
- TOKEN = os .getenv ('DISCORD_TOKEN' )
33
+ #################################
34
+ #
35
+ POINTS_PER_MESSAGE = 2 #
36
+ POINTS_PER_MINUTE_TALKING = 1 #
37
+ POINT_DECAY_PER_HOUR = 1 #
38
+ #
39
+ #################################
40
+
36
41
37
42
# Initialize bot object to use the COMMAND_PREFIX defined above
38
43
bot = commands .Bot (command_prefix = COMMAND_PREFIX )
39
44
40
- # Generate timestamp of startup
41
- timestamp = time .strftime ('%Y%m%d-%H%M%S' )
42
-
43
- # Configure logging
44
- logging .basicConfig (
45
- level = LOG_LEVEL ,
46
- format = '%(asctime)s: [%(levelname)s] - %(message)s' ,
47
- datefmt = '%Y-%m-%d %H:%M:%S' ,
48
- handlers = [
49
- logging .FileHandler (f"logs/{ timestamp } .log" , mode = "w" ),
50
- logging .StreamHandler ()
51
- ]
52
- )
53
45
54
46
@bot .event
55
47
async def on_connect ():
@@ -75,10 +67,10 @@ async def on_ready():
75
67
label = guild .name + " (" + str (guild .id ) + ")"
76
68
logging .info (label )
77
69
78
- activity .readDatabase ()
70
+ activity .read_database ()
79
71
80
72
for guild in bot .guilds :
81
- addAllUsersFromGuildToDatabase (guild )
73
+ add_all_users_from_guild_to_database (guild )
82
74
83
75
84
76
@bot .event
@@ -89,6 +81,7 @@ async def on_disconnect():
89
81
90
82
logging .warning ('Lost connection to Discord.' )
91
83
84
+
92
85
@bot .event
93
86
async def on_guild_join (guild ):
94
87
"""
@@ -119,10 +112,9 @@ async def on_member_join(member):
119
112
else :
120
113
welcome_message = f"Welcome to { member .guild .name } , { member .name } !"
121
114
122
- await member .dm_channel .send (welcome_message )
123
-
124
-
115
+ add_user_to_database (member .id , member .name )
125
116
117
+ await member .dm_channel .send (welcome_message )
126
118
127
119
128
120
@bot .event
@@ -143,14 +135,17 @@ async def on_message(message):
143
135
Allows the bot to respond to user messages rather than commands
144
136
"""
145
137
146
- if message .author == bot . user :
138
+ if message .author . bot :
147
139
"""
148
140
Tells the bot to ignore its own messages
149
141
"""
150
142
151
- return
143
+ return
152
144
153
- elif 'happy birthday' in message .content .lower ():
145
+ else :
146
+ change_user_score (message .author .id , POINTS_PER_MESSAGE )
147
+
148
+ if 'happy birthday' in message .content .lower ():
154
149
"""
155
150
Lets the bot say happy birthday whenever a user says it
156
151
"""
@@ -184,38 +179,111 @@ async def on_message(message):
184
179
await bot .process_commands (message )
185
180
186
181
187
- async def run_once_per_day ():
182
+ @bot .command (name = "score" , help = "Displays your current server score." )
183
+ async def check_user_score (ctx ):
184
+ uuid = ctx .message .author .id
185
+ score = get_user_score (uuid )
186
+
187
+ # Take away the points the user gets for running the command
188
+ change_user_score (uuid , - POINTS_PER_MESSAGE )
189
+
190
+ await ctx .message .channel .send (f"Score for <@{ uuid } >: { score } " )
191
+
192
+
193
+ async def run_once_every_day ():
188
194
"""
189
195
Runs a block of code every day sometime between 00:00 and 01:00 local time.
190
196
"""
191
197
192
- await bot .wait_until_ready ()
193
-
194
198
if (int (time .strftime ('%H' , time .localtime ())) < 1 ):
195
199
# This code will run if it is the correct time
196
200
logging .info ("Running nightly operations." )
197
201
else :
198
202
logging .debug ("Attempted to run daily event out of defined hours." )
199
203
200
- # Check every hour
204
+
205
+ async def run_once_every_minute ():
206
+ """
207
+ Runs a block of code every minute
208
+ """
209
+
210
+ await asyncio .sleep (60 )
211
+
212
+ # Give every user in a voice channel points
213
+ for guild in bot .guilds :
214
+ for channel in guild .voice_channels :
215
+ for user in channel .members :
216
+ change_user_score (user .id , POINTS_PER_MINUTE_TALKING )
217
+
218
+
219
+ async def run_once_every_hour ():
220
+ """
221
+ Runs a block of code every hour
222
+ """
223
+
201
224
await asyncio .sleep (3600 )
202
225
226
+ # Call the once-each-day function so it can do its check
227
+ await run_once_every_day ()
228
+
229
+ activity .change_all_scores (- POINT_DECAY_PER_HOUR )
230
+
231
+
232
+ def change_user_score (uuid , delta ):
233
+ if (uuid in activity .USERS .keys ()):
234
+ activity .USERS [uuid ].change_score (delta )
235
+ else :
236
+ logging .error (f"Attempted to change score of user { uuid } when user is not in database." )
237
+
203
238
204
- def addAllUsersFromGuildToDatabase (guild ):
239
+ def get_user_score (uuid ):
240
+ if (uuid in activity .USERS .keys ()):
241
+ return activity .USERS [uuid ].score
242
+ else :
243
+ logging .error (f"Attempted to get score of user { uuid } when user is not in database." )
244
+
245
+
246
+ def add_all_users_from_guild_to_database (guild ):
205
247
for user in guild .members :
206
248
if (not user .bot ):
207
- addUserToDatabase (user .id , user .name )
249
+ add_user_to_database (user .id , user .name )
208
250
209
- activity .writeDatabase ()
251
+ activity .write_database ()
210
252
211
253
212
- def addUserToDatabase (uuid , name , score = 0 , allowmoderator = True , rankexempt = False ):
254
+ def add_user_to_database (uuid , name , score = 0 , allowmoderator = True , rankexempt = False ):
213
255
if (not uuid in activity .USERS .keys ()):
214
- activity .addUser (uuid , name , score , allowmoderator , rankexempt )
256
+ activity .add_user (uuid , name , score , allowmoderator , rankexempt )
215
257
logging .info (f"Registered new user { name } ({ uuid } ) to database." )
216
258
217
- activity .writeDatabase ()
259
+ activity .write_database ()
260
+
261
+
262
+
263
+
264
+ def main ():
265
+ # Load bot token from .env
266
+ load_dotenv ()
267
+ TOKEN = os .getenv ('DISCORD_TOKEN' )
268
+
269
+ # Generate timestamp of startup
270
+ timestamp = time .strftime ('%Y%m%d-%H%M%S' )
271
+
272
+ # Configure logging
273
+ logging .basicConfig (
274
+ level = LOG_LEVEL ,
275
+ format = '%(asctime)s: [%(levelname)s] - %(message)s' ,
276
+ datefmt = '%Y-%m-%d %H:%M:%S' ,
277
+ handlers = [
278
+ logging .FileHandler (f"logs/{ timestamp } .log" , mode = "w" ),
279
+ logging .StreamHandler ()
280
+ ]
281
+ )
282
+
283
+ bot .loop .create_task (run_once_every_minute ())
284
+ bot .loop .create_task (run_once_every_hour ())
218
285
286
+ bot .run (TOKEN )
219
287
220
- bot . loop . create_task ( run_once_per_day ())
221
- bot . run ( TOKEN )
288
+ if __name__ == "__main__" :
289
+ main ( )
0 commit comments