3
3
import discord
4
4
import asyncio
5
5
6
- from discord import Game
6
+ from discord import Game , DMChannel
7
+
7
8
from database import Database
8
9
from datetime import datetime
9
10
from dotenv import load_dotenv
10
11
from os .path import join , dirname
11
12
from input_parser import json_parser
12
13
from discord .ext .commands import Bot
13
14
14
- # This is from rolley
15
15
PREFIX = '>'
16
16
DQ_CHANNEL = 'daily-coding-challenge'
17
17
Q_CHANNEL = 'programming-challenges'
@@ -48,22 +48,49 @@ def update_question():
48
48
question_date = datetime .today ().date ()
49
49
50
50
51
+ def get_embed (base = None ):
52
+ global question
53
+
54
+ if base is None :
55
+ question_text = "[ *{}* ] Asked by **{}**\n \n {}" .format (question [0 ], question [1 ], question [2 ])
56
+
57
+ if len (question ) == 4 :
58
+ question_text + "\n Leetcode link: {}" .format (question [3 ])
59
+ else :
60
+ question_text = "[ *{}* ] Asked by **{}**\n \n {}" .format (base [0 ], base [1 ], base [2 ])
61
+
62
+ return discord .Embed (title = 'Question for **{}**' .format (datetime .today ().date ()), type = 'rich' ,
63
+ description = question_text , color = 0xffd700 )
64
+
65
+
51
66
async def timer_update ():
67
+ global target_channel
68
+
52
69
while True :
53
70
update_question ()
54
71
emb = get_embed ()
55
72
56
73
if target_channel is not None :
57
- await bot . send_message ( target_channel , embed = emb )
74
+ await target_channel . send ( embed = emb )
58
75
59
76
await asyncio .sleep (secs )
60
77
61
78
79
+ def is_mod_or_admin (author , channel , channel_is_private = False ):
80
+ global db
81
+ if not channel_is_private :
82
+ perms = author .permissions_in (channel )
83
+ if perms .manage_roles or perms .administrator :
84
+ return True
85
+
86
+ return db .is_admin (author .id )
87
+
88
+
62
89
# Bot Events
63
90
@bot .event
64
91
async def on_ready ():
65
92
global db
66
- await bot .change_presence (game = Game (name = "Hackerrank" ))
93
+ await bot .change_presence (activity = Game (name = "Hackerrank" ))
67
94
print ("Logged in as" )
68
95
print (bot .user .name )
69
96
print (bot .user .id )
@@ -73,54 +100,43 @@ async def on_ready():
73
100
asyncio .ensure_future (timer_update (), loop = loop )
74
101
75
102
76
- def is_mod_or_admin (author , channel_is_private = False ):
77
- global db
78
- if not channel_is_private :
79
- perms = author .server_permissions
80
- if perms .manage_roles or perms .administrator :
81
- return True
82
-
83
- return db .is_admin (author .id )
84
-
85
-
86
103
@bot .event
87
104
async def on_message (message ):
88
105
global target_channel
89
106
90
- if not message .channel . is_private or message .author .bot :
107
+ if not isinstance ( message .channel , DMChannel ) or message .author .bot :
91
108
# This is a passive check to update the target channel for showing question
92
109
if target_channel is None and message .channel .name == DQ_CHANNEL :
93
110
target_channel = message .channel
94
111
95
112
await bot .process_commands (message )
96
113
return
97
114
98
- # TODO: add check in DB to see if person is allowed to add
99
- if not is_mod_or_admin (message .author , channel_is_private = True ):
115
+ if not is_mod_or_admin (message .author , message .channel , channel_is_private = True ):
100
116
return
101
117
102
118
if len (message .attachments ) == 1 :
103
119
file_url = message .attachments [0 ]['url' ]
104
120
105
121
if file_url .endswith (".json" ):
106
- await bot . send_message ( message .author , "Attempting to add bulk question list" )
122
+ await message .channel . send ( "Attempting to add bulk question list" )
107
123
question_list = json_parser (file_url )
108
124
db .add_multiple_questions (question_list )
109
- await bot . send_message ( message .author , "Added questions in bulk" )
125
+ await message .channel . send ( "Added questions in bulk" )
110
126
return
111
127
else :
112
- await bot . send_message ("I only support JSON files right now" )
128
+ await message . channel . send ("I only support JSON files right now" )
113
129
return
114
130
115
131
if message .author .id in user_cache .keys ():
116
132
if message .content .upper () == 'Y' or message .content .upper () == 'YES' :
117
133
arr = user_cache [message .author .id ]
118
134
if db .add_new_question (arr [1 ], arr [2 ], arr [3 ]) == 1 :
119
- await bot . send_message ( message .author , "Question added." )
135
+ await message .channel . send ( "Question added." )
120
136
else :
121
- await bot . send_message ( message .author , "Question already in database." )
137
+ await message .channel . send ( "Question already in database." )
122
138
else :
123
- await bot . send_message ( message .author , "Question **not** added." )
139
+ await message .channel . send ( "Question **not** added." )
124
140
125
141
del user_cache [message .author .id ]
126
142
@@ -130,22 +146,21 @@ async def on_message(message):
130
146
131
147
if message .author .id in editor_cache .keys ():
132
148
if update_question_details (message , message .author .id ):
133
- await bot . send_message ( message .author , content = "Question has been updated" )
149
+ await message .channel . send ( content = "Question has been updated" )
134
150
else :
135
- await bot . send_message ( message .author , content = "Question could not be updated, try again" )
151
+ await message .channel . send ( content = "Question could not be updated, try again" )
136
152
return
137
153
138
154
if len (lines ) < 3 :
139
- await bot . send_message ( message .author , "Invalid Format. Try Again" )
155
+ await message .channel . send ( "Invalid Format. Try Again" )
140
156
return
141
157
142
158
# index [0] is just a newline
143
159
arr = [1 , lines [1 ], "\n " .join (lines [3 :])]
144
160
145
161
emb = get_embed (base = arr )
146
162
147
- await bot .send_message (message .author , embed = emb ,
148
- content = "Type in _YES_ to keep it this way. _NO_ to try format again" )
163
+ await message .channel .send (embed = emb , content = "Type in _YES_ to keep it this way. _NO_ to try format again" )
149
164
150
165
arr .append (lines [2 ])
151
166
user_cache [message .author .id ] = arr
@@ -173,57 +188,55 @@ def update_question_details(msg, author_id):
173
188
pass_context = True )
174
189
async def add_admin (ctx ):
175
190
if len (ctx .message .mentions ) == 0 :
176
- await bot . send_message ( ctx .message .channel , content = 'Need to supply users to add' )
191
+ await ctx .message .channel . send ( 'Need to supply users to add' )
177
192
return
178
193
179
- if not is_mod_or_admin (ctx .message .author ):
180
- await bot . send_message ( ctx .message .channel , content = 'You have insufficient perms to do this' )
194
+ if not is_mod_or_admin (ctx .message .author , ctx . message . channel ):
195
+ await ctx .message .channel . send ( content = 'You have insufficient perms to do this' )
181
196
return
182
197
183
198
for mentioned in ctx .message .mentions :
184
199
if db .add_admin (mentioned .id ) == 0 :
185
- await bot . send_message ( ctx .message .channel , content = 'User {} not found' .format (mentioned .name ))
200
+ await ctx .message .channel . send ( content = 'User {} not found' .format (mentioned .name ))
186
201
187
- await bot . send_message ( ctx .message .channel , content = 'Users added' )
202
+ await ctx .message .channel . send ( content = 'Users added' )
188
203
189
204
190
205
@bot .command (name = 'remove_admin' , description = 'remove admin from table' , aliases = ['del_admin' , 'da' ],
191
206
brief = 'delete admin' , pass_context = True )
192
207
async def remove_admin (ctx ):
193
208
if len (ctx .message .mentions ) == 0 :
194
- await bot . send_message ( ctx .message .channel , content = 'Need to supply users to add' )
209
+ await ctx .message .channel . send ( content = 'Need to supply users to add' )
195
210
return
196
211
197
- if not is_mod_or_admin (ctx .message .author ):
198
- await bot . send_message ( ctx .message .channel , content = 'You have insufficient perms to do this' )
212
+ if not is_mod_or_admin (ctx .message .author , ctx . message . channel ):
213
+ await ctx .message .channel . send ( content = 'You have insufficient perms to do this' )
199
214
return
200
215
201
216
for mentioned in ctx .message .mentions :
202
217
if db .remove_admin (mentioned .id ) is None :
203
- await bot . send_message ( ctx .message .channel , content = 'User {} not in table' .format (mentioned .name ))
218
+ await ctx .message .channel . send ( content = 'User {} not in table' .format (mentioned .name ))
204
219
205
- await bot . send_message ( ctx .message .channel , content = 'Users removed' )
220
+ await ctx .message .channel . send ( content = 'Users removed' )
206
221
207
222
208
223
@bot .command (name = 'edit_question' , description = 'edits a question' , aliases = ['edit' ], brief = 'update question' ,
209
224
pass_context = True )
210
225
async def edit_question (ctx , * args ):
211
- if not is_mod_or_admin (ctx .message .author ):
212
- await bot . send_message ( ctx .message .channel , content = "You have insufficient perms to do this" )
226
+ if not is_mod_or_admin (ctx .message .author , ctx . message . channel ):
227
+ await ctx .message .channel . send ( content = "You have insufficient perms to do this" )
213
228
return
214
229
215
230
if len (args ) == 0 :
216
- await bot . send_message ( ctx .message .channel , content = "You need to specify a number..." )
231
+ await ctx .message .channel . send ( content = "You need to specify a number..." )
217
232
return
218
233
219
234
try :
220
235
index = int (args [0 ])
221
236
editor_cache [ctx .message .author .id ] = index
222
- await bot .send_message (ctx .message .channel ,
223
- content = "Please enter the question body surrounded by triple backticks" )
237
+ await ctx .message .channel .send (content = "Please enter the question body surrounded by triple backticks" )
224
238
except ValueError :
225
- await bot .send_message (ctx .message .channel ,
226
- content = "A number, y'know like 0, or 1, or 1997. This isn't that hard" )
239
+ await ctx .message .channel .send (content = "A number, y'know like 0, or 1, or 1997. This isn't that hard" )
227
240
228
241
229
242
@bot .command (name = 'sample_json' , description = 'show sample of json for input purposes' , aliases = ['sj' , 'json' ],
@@ -237,7 +250,7 @@ async def sample_json_format(ctx):
237
250
238
251
parsed_to_json = json .dumps (questions )
239
252
240
- await bot . send_message ( ctx .message .author , "```" + parsed_to_json + "```" )
253
+ await ctx .message .channel . send ( "```" + parsed_to_json + "```" )
241
254
242
255
243
256
@bot .command (name = 'show_question' ,
@@ -257,7 +270,7 @@ async def show_question(ctx, *args):
257
270
else :
258
271
emb = get_embed ()
259
272
260
- await bot . send_message ( ctx .message .channel , embed = emb )
273
+ await ctx .message .channel . send ( embed = emb )
261
274
else :
262
275
if ctx .message .channel .name != Q_CHANNEL :
263
276
return
@@ -267,11 +280,11 @@ async def show_question(ctx, *args):
267
280
268
281
if succ is not None :
269
282
emb = get_embed (base = succ )
270
- await bot . send_message ( ctx .message .channel , embed = emb )
283
+ await ctx .message .channel . send ( embed = emb )
271
284
else :
272
- await bot . send_message ( ctx .message .channel , content = "could not find question with supplied index" )
285
+ await ctx .message .channel . send ( content = "could not find question with supplied index" )
273
286
except ValueError :
274
- await bot . send_message ( ctx .message .channel , content = "Please supply **A NUMBER**" )
287
+ await ctx .message .channel . send ( content = "Please supply **A NUMBER**" )
275
288
276
289
277
290
@bot .command (name = 'random_question' , aliases = ['random' , 'rq' ], brief = 'get random question from database' ,
@@ -285,7 +298,7 @@ async def random_question(ctx, *args):
285
298
else :
286
299
emb = get_embed (db .get_random_question (company = args [0 ]))
287
300
288
- await bot . send_message ( ctx .message .channel , embed = emb )
301
+ await ctx .message .channel . send ( embed = emb )
289
302
290
303
291
304
@bot .command (name = 'list_questions' , description = 'lists all questions from index provided' , aliases = ['list' , 'lq' ],
@@ -305,7 +318,7 @@ async def list_questions(ctx, *args):
305
318
306
319
string = '```' + string + '```'
307
320
308
- await bot . send_message ( ctx .message .channel , content = string )
321
+ await ctx .message .channel . send ( content = string )
309
322
310
323
311
324
@bot .command (name = 'list_companies' , description = 'lists all companies in database' , aliases = ['lc' ],
@@ -319,20 +332,20 @@ async def list_companies(ctx, *args):
319
332
string += "{0:15} | {1:3}\n " .format (row .company , row .count )
320
333
321
334
string = '```' + string + '```'
322
- await bot . send_message ( ctx .message .channel , content = string )
335
+ await ctx .message .channel . send ( content = string )
323
336
324
337
325
338
@bot .command (name = 'remove_question' , description = 'deletes question based on index, found by using >list' ,
326
339
aliases = ['del' , 'remove' ],
327
340
brief = 'delete question on index' , pass_context = True )
328
341
async def remove_question (ctx , * args ):
329
- if not is_mod_or_admin (ctx .message .author ):
330
- await bot . send_message ( ctx .message .channel , content = "You have insufficient perms to do this" )
342
+ if not is_mod_or_admin (ctx .message .author , ctx . message . channel ):
343
+ await ctx .message .channel . send ( content = "You have insufficient perms to do this" )
331
344
return
332
345
333
346
global db , question
334
347
if len (args ) == 0 :
335
- await bot . send_message ( ctx .message .channel , content = "Please supply index to delete" )
348
+ await ctx .message .channel . send ( content = "Please supply index to delete" )
336
349
return
337
350
338
351
try :
@@ -341,26 +354,11 @@ async def remove_question(ctx, *args):
341
354
if succ is not None :
342
355
if question == succ :
343
356
question = None
344
- await bot . send_message ( ctx .message .channel , content = "Successfully deleted" )
357
+ await ctx .message .channel . send ( content = "Successfully deleted" )
345
358
else :
346
- await bot . send_message ( ctx .message .channel , content = "could not find question with supplied index" )
359
+ await ctx .message .channel . send ( content = "could not find question with supplied index" )
347
360
except ValueError :
348
- await bot .send_message (ctx .message .channel , content = "Please supply **A NUMBER**" )
349
-
350
-
351
- def get_embed (base = None ):
352
- global question
353
-
354
- if base is None :
355
- question_text = "[ *{}* ] Asked by **{}**\n \n {}" .format (question [0 ], question [1 ], question [2 ])
356
-
357
- if len (question ) == 4 :
358
- question_text + "\n Leetcode link: {}" .format (question [3 ])
359
- else :
360
- question_text = "[ *{}* ] Asked by **{}**\n \n {}" .format (base [0 ], base [1 ], base [2 ])
361
-
362
- return discord .Embed (title = 'Question for **{}**' .format (datetime .today ().date ()), type = 'rich' ,
363
- description = question_text , color = 0xffd700 )
361
+ await ctx .message .channel .send (content = "Please supply **A NUMBER**" )
364
362
365
363
366
364
if __name__ == '__main__' :
0 commit comments