This repository was archived by the owner on May 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAttempt 12
More file actions
346 lines (323 loc) · 21.2 KB
/
Attempt 12
File metadata and controls
346 lines (323 loc) · 21.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#Discord library provdies all of the functionality needed for a Discord bot
import discord
from discord.ext import commands
#Canvasapi library provides all of the functionailty needed to connect to a Canvas website
from canvasapi import Canvas
import canvasapi
#The re and pprint libariers are used to eliminate html tags from specific pull request
import re
import pprint
TOKEN = 'discord token' #Unique discord bot token that connects the code to the bot
#Bot configuarations needed to allow all commands to work as intended
intents = discord.Intents.default()
intents.members = True
client = commands.Bot(command_prefix = '$', intents=intents)
#base_url = "https://www.canvasnajar.com"
base_url = "https://canvas.instructure.com/"
dict = {}
class CanvasBot:
#On_ready event that sets up the dictionary needed to store all of the currently connected discord user's information
@client.event
async def on_ready():
global dict
print('Bot is ready')
for guild in client.guilds:
for member in guild.members:
dict[f'{member.id}'] = {"Access": 'None', "Username": f'{member}'}
pprint.pprint(dict)
#On_message event that displays where the user is currently typing while the bot is on
@client.event
async def on_message(message):
if isinstance(message.channel, discord.channel.DMChannel):
print('This is a Private Channel.')
else:
print('This is a Text Channel.')
'''
if message.content.startswith('!members'):
for guild in client.guilds:
for member in guild.members:
dict = {f'{member.id}': 'None'}
print(f"{member}: ", dict)
if message.content.startswith('!update'):
for guild in client.guilds:
for member in guild.members:
dict = {f'{member.id}'}
print(f"{member}: ", dict)
'''
await client.process_commands(message)
#On_command_error event that outputs specific text depending on the type of error captured
@client.event
async def on_command_error(ctx, error):
if isinstance(error, commands.CommandNotFound):
await ctx.send('Error: Command not found!')
elif isinstance(error, commands.MessageNotFound):
await ctx.send('Error: Message not found!')
#Info command that shows all of the current commands for the current version of the code
@client.command(aliases = ['i', 'I'])
async def info(ctx):
message = ("__**Welcome to Canvas Bot.**__"
+ "\n *Setup command*"
+ "\n > Type **$setup**, **$s** or **$S** along with your access token for your Canvas account to establish a connection between your discord and Canvas account."
+ "\n > _Example:_ $s 8~D3hY29OM3QkrPat5aDwNKccolL4WY1jcHt8QEUaIDqEGzqr5AC73u3kpiIisfojgVtp\n"
+ "\n *Courses command*"
+ "\n > Type **$courses**, **$c** or **$C** to display all of your currently enrolled.\n"
+ "\n *Event command*"
+ "\n > Type **$events**, **$e** or **$E** to display all of your current events based on your user id.\n"
+ "\n *Announcment command*"
+ "\n > Type **$announcements**, **$a** or **$A** along with one of your course's ids to display all of your current announcements for that specific course."
+ "\n > __Example:__ $a 2421114\n"
+ "\n *Create Event command*"
+ "\n > Type **$create_event**, **$ce** or **$CE** along with your user id, title, description, start and end time for the event."
+ "\n > __Example:__ $ce 28468322 Test Test 2/12/5:00 2/13/6:35\n"
+ "\n *File command*"
+ "\n > Type **$file** or **$f** along with one of your file's ids to display all of the information for that specific file."
+ "\n > __Example:__ $f 123456789\n"
+ "\n *Folder command*"
+ "\n > Type **$folder** or **$F** along with one of your folder's ids to display all of the information for that specific folder."
+ "\n > __Example:__ $F 123456789\n")
message2 = ("\n-------- End of Page 1 --------\n"
+ "\n*User command*"
+ "\n > Type **$user**, **$u** or **$U** along with one of your course's ids and your first and last name to display your user id."
+ "\n > __Example:__ $u 2421114 Nickolas Rodriguez\n"
+ "\n *Polls command*"
+ "\n > Type **$polls**, **$p** or **$P** to display all of your current polls based on your account.\n"
+ "\n *Create poll command* (WIP)"
+ "\n > Type **$create_poll**, **$cp** or **$CP** along with a one-word question and one-word description to create a poll for your account."
+ "\n > __Example__: $cp Test Test\n"
+ "\n *Course Files command*"
+ "\n > Type **$course_files** or **$cf** along with one of your course's ids to display all of your current files for that specific course."
+ "\n > __Example__: $cf 2421114\n"
+ "\n *Course Folders command*"
+ "\n > Type **$course_folders** or **$CF** along with one of your course's ids to display all of your current folders for that specific course."
+ "\n > __Example__: $CF 2421114\n"
+ "\n__**Please make sure for all commands, after all other input parameters needed, you are inserting your correct access token associated with your Canvas account!**__")
await ctx.channel.send(message)
await ctx.channel.send(message2)
#Course command that shows all of the current courses for a specific user
@client.command(aliases = ['c', 'C'])
async def courses(ctx):
global dict
if (dict[f'{ctx.author.id}']["Access"] == 'None'):
await ctx.channel.send("**Uh-oh!** \nPlease insert check to see if you inserted the proper parameters. \nCheck **$info** for help.")
else:
Dict = 'j '.join(dict[f'{ctx.author.id}']["Access"])
access_token = (Dict)
canvas = Canvas(base_url, access_token)
list = canvas.get_courses()
await ctx.channel.send(f'@{ctx.author.name} ```Here is your output!\n```')
for courses in list:
await ctx.channel.send(f'*Name:* **{courses.name}** \n*Course ID:* {courses.id} \n*Course Code:* _{courses.course_code}_')
await ctx.channel.send('__**Here are your current courses!**__'
+ "```\nOutputs include the course's: "
+ '\nName: [course name] \nCourse ID: [id] \nCourse Code: [code]```')
#Event command that shows all of the current events for a specific user
@client.command(aliases = ['e','E'])
async def events(ctx):
global dict
if (dict[f'{ctx.author.id}']["Access"] == 'None'):
await ctx.channel.send("**Uh-oh!** \nPlease insert check to see if you inserted the proper parameters. \nCheck **$info** for help.")
else:
Dict = 'j '.join(dict[f'{ctx.author.id}']["Access"])
access_token = (Dict)
canvas = Canvas(base_url, access_token)
list = canvas.get_calendar_events(all_events = "true")
await ctx.channel.send(f'@{ctx.author.name} ```Here is your output!\n```')
for event in list:
cleanr = re.compile('<.*?>')
description = re.sub(cleanr, '', f'{event.description}')
await ctx.channel.send(f'*Title:* **{event.title}** \n *Start:* {event.start_at} \n *End:* {event.end_at} \n *Description:* {description}')
await ctx.channel.send('__**Here are all of your events!**__'
+ "```\nOutputs include the event's: "
+ '\nTitle: [title] \nStart: [start date] \nEnd: [end date] \nDescription: [description]```')
#Announcement command that shows all of the current announcements for a specific class the user inputs
@client.command(aliases = ['a', 'A'])
async def announcements(ctx, course="None"):
if(course == "None"):
return await ctx.channel.send("You are missing the course requirement!\nPlease insert an appropriate **course id.**\nEx: $a **2421114**")
global dict
input = "course_" + course
if (dict[f'{ctx.author.id}']["Access"] == 'None'):
await ctx.channel.send("**Uh-oh!** \nPlease insert check to see if you inserted the proper parameters. \nCheck **$info** for help.")
else:
Dict = 'j '.join(dict[f'{ctx.author.id}']["Access"])
access_token = (Dict)
canvas = Canvas(base_url, access_token)
list = canvas.get_announcements(context_codes = input)
await ctx.channel.send(f'@{ctx.author.name} ```Here is your output!\n```')
for announce in list:
cleanr = re.compile('<.*?>')
message = re.sub(cleanr, '', f'{announce.message}')
await ctx.channel.send(f'*Title:* **{announce.title}** \n *Message:* {message} \n *Posted:* {announce.posted_at}')
await ctx.channel.send('__**Here are your current announcements!**__'
+ "```\nOutputs include the announcement's: "
+ '\nTitle: [title] \nMessage: [message] \nPosted: [posted date]```')
#Setup command that connects the user's Canvas and discord account together
@client.command(aliases = ['s', 'S'])
async def setup(ctx, access='None'):
if(access == 'None'):
return await ctx.channel.send("You are missing the access token requirement!\nPlease insert an appropriate **access token**.\nEx: $s 8~D3hY29OM3QkrPat5aDwNKccolL4WY1jcHt8QEUaIDqEGzqr5AC73u3kpiIisfojgVtp")
global access_token
global dict
dict[f'{ctx.author.id}']["Access"] = {f'{access}'}
pprint.pprint(dict)
await ctx.channel.send(f'Your access token has been recieved!\n You are now ready to start using the Canvas Bot!\n Type **$info** for a command list.')
#Create event command that creates a user event based on 5 parameters
@client.command(aliases = ['ce', 'CE'])
async def create_event(ctx, id='None', title='None', des='None', start='None', end='None'):
if(id == 'None'):
return await ctx.channel.send("You are missing the user id requirement!\nPlease insert an appropriate **user id**.\nEx: $ce **111111111** Random Description 12/12/5:00 12/13/6:54")
elif(title == 'None'):
return await ctx.channel.send("You are missing the title requirement!\nPlease insert an appropriate one word **title**.\nEx: $ce 111111111 **Random** Description 12/12/5:00 12/13/6:54")
elif(des == 'None'):
return await ctx.channel.send("You are missing the description requirement!\nPlease insert an appropriate one word **description**.\nEx: $ce 111111111 Random **Description** 12/12/5:00 12/13/6:54")
elif(start == 'None'):
return await ctx.channel.send("You are missing the start time requirement!\nPlease insert an appropriate **start time**.\nEx: $ce 111111111 Random Description **12/12/5:00** 12/13/6:54")
elif(end == 'None'):
return await ctx.channel.send("You are missing the end time requirement!\nPlease insert an appropriate **end time**.\nEx: $ce 111111111 Random Description 12/12/5:00 **12/13/6:54**")
global dict
context_code = 'user_'+ id
calendar_event = {'context_code': context_code, 'title': title, 'description': des, 'start_at': start, 'end_at': end}
if (dict[f'{ctx.author.id}']["Access"] == 'None'):
await ctx.channel.send("**Uh-oh!** \nPlease insert check to see if you inserted the proper parameters. \nCheck **$info** for help.")
else:
Dict = 'j '.join(dict[f'{ctx.author.id}']["Access"])
access_token = (Dict)
canvas = Canvas(base_url, access_token)
canvas.create_calendar_event(calendar_event)
await ctx.channel.send(f'@{ctx.author.name} ```Check your calendar for the new event!\n```')
#File command that shows all the information from a specific file id inputted
@client.command(aliases = ['f'])
async def file(ctx, id='None'):
if(id == 'None'):
return await ctx.channel.send("You are missing the file id requirement!\nPlease insert an appropriate **file id**.\nEx: $f **123456789**")
global dict
if (dict[f'{ctx.author.id}']["Access"] == 'None'):
await ctx.channel.send("**Uh-oh!** \nPlease insert check to see if you inserted the proper parameters. \nCheck **$info** for help.")
else:
Dict = 'j '.join(dict[f'{ctx.author.id}']["Access"])
access_token = (Dict)
canvas = Canvas(base_url, access_token)
file = canvas.get_file(file = id)
await ctx.channel.send(f'@{ctx.author.name} ```Here is your output!\n```')
await ctx.channel.send(f'Name: **{file.display_name}** \n URL: **{file.url}** \n')
await ctx.channel.send('__**Here is your current file!**__'
+ "```\nOutputs include the file's: "
+ '\nName: [name] \nURL: [url] \n```')
#Folder command that shows all the information from a specific folder id inputted
@client.command(aliases = ['F'])
async def folder(ctx, id = 'None'):
if(id == 'None'):
return await ctx.channel.send("You are missing the folder id requirement!\nPlease insert an appropriate **folder id**.\nEx: $f **123456789**")
global dict
if (dict[f'{ctx.author.id}']["Access"] == 'None'):
await ctx.channel.send("**Uh-oh!** \nPlease insert check to see if you inserted the proper parameters. \nCheck **$info** for help.")
else:
Dict = 'j '.join(dict[f'{ctx.author.id}']["Access"])
access_token = (Dict)
canvas = Canvas(base_url, access_token)
list = canvas.get_folder(folder = id)
await ctx.channel.send(f'@{ctx.author.name} ```Here is your output!\n```')
folders_url = f'{list.folders_url}'
files_url = f'{list.files_url}'
await ctx.channel.send(f'**Title**: {list.full_name}\n**Folder Details**: {folders_url}\n**File(s) Details**: {files_url}'
+f'\n**File Count**: {list.files_count}\n**Parent Folder**: {list.parent_folder_id}\n**Folder ID**: {list.id}'
+f'\n**Found in Course ID**: {list.context_id}\n**At Position**: {list.position}')
#Polls command that shows all of the current polls for a specific user
@client.command(aliases = ['p', 'P'])
async def polls(ctx):
global dict
if (dict[f'{ctx.author.id}']["Access"] == 'None'):
await ctx.channel.send("**Uh-oh!** \nPlease insert check to see if you inserted the proper parameters. \nCheck **$info** for help.")
else:
Dict = 'j '.join(dict[f'{ctx.author.id}']["Access"])
access_token = (Dict)
canvas = Canvas(base_url, access_token)
list = canvas.get_polls()
await ctx.channel.send(f'@{ctx.author.name} ```Here is your output!\n```')
for poll in list:
cleanr = re.compile('<.*?>')
message = re.sub(cleanr, '', f'{poll.description}')
await ctx.channel.send(f'*Question:* **{poll.question}** \n *Description* {message} \n *Posted:* {poll.created_at}')
await ctx.channel.send('__**Here are your current polls!**__'
+ "```\nOutputs include the poll's: "
+ '\nQuestion: [question] \nDescription: [description] \nPosted: [posted date]```')
#Create poll command that creates a user poll based on 2 parameters
@client.command(aliases = ['cp', 'CP'])
async def create_poll(ctx, quest='None', des='None'):
if(quest == 'None'):
return await ctx.channel.send("You are missing the question requirement!\nPlease insert an appropriate one word **question**.\nEx: $cp **Color** Pick")
elif(des == 'None'):
return await ctx.channel.send("You are missing the description requirement!\nPlease insert an appropriate one word **description**.\nEx: $cp Color **Pick**")
global dict
if (dict[f'{ctx.author.id}']["Access"] == 'None'):
await ctx.channel.send("**Uh-oh!** \nPlease insert check to see if you inserted the proper parameters. \nCheck **$info** for help.")
else:
Dict = 'j '.join(dict[f'{ctx.author.id}']["Access"])
access_token = (Dict)
canvas = Canvas(base_url, access_token)
polls = canvas.create_poll(poll = [{"question": quest, "description": des}])
await ctx.channel.send(f'@{ctx.author.name} ```Here is your output!\n```')
#User command that shows all the user's id from Canvas based on 3 parameters
@client.command(aliases = ['u','U'])
async def user(ctx, id='None', first='None', last='None'):
if(id == 'None'):
return await ctx.channel.send("You are missing the course id requirement!\nPlease insert an appropriate **course id**.\nEx: $u **2421114** Nickolas Rodriguez")
elif(first == 'None'):
return await ctx.channel.send("You are missing the first name requirement!\nPlease insert an appropriate **first name**.\nEx: $u 2421114 **Nickolas** Rodriguez")
elif(last == 'None'):
return await ctx.channel.send("You are missing the last name requirement!\nPlease insert an appropriate **last name**.\nEx: $u 2421114 Nickolas **Rodriguez**")
global dict
name = first + ' ' + last
if (dict[f'{ctx.author.id}']["Access"] == 'None'):
await ctx.channel.send("**Uh-oh!** \nPlease check to see if you inserted the proper parameters. \nCheck **$info** for help.")
else:
Dict = 'j '.join(dict[f'{ctx.author.id}']["Access"])
access_token = (Dict)
canvas = Canvas(base_url,access_token)
course = canvas.get_course(id)
user = course.get_users(search_term = name)
await ctx.channel.send(user[0])
await ctx.channel.send('__**Here is your Canvas Account!**__'
+ "```\nOutputs include the accounts's: "
+ '\n[Firstname Lastname] (Canvas ID) \n```')
#Course files command that shows all of the current files within a specific course
@client.command(aliases = ['cf'])
async def course_files(ctx, id = 'None'):
if(id == 'None'):
return await ctx.channel.send("You are missing the course id requirement!\nPlease insert an appropriate **course id**.\nEx: $cf **2421114**")
global dict
if (dict[f'{ctx.author.id}']["Access"] == 'None'):
await ctx.channel.send("**Uh-oh!** \nPlease check to see if you inserted the proper parameters. \nCheck **$info** for help.")
else:
Dict = 'j '.join(dict[f'{ctx.author.id}']["Access"])
access_token = (Dict)
canvas = Canvas(base_url,access_token)
course = canvas.get_course(id)
list = course.get_files()
await ctx.channel.send(f'@{ctx.author.name} ```Here is your output!\n```')
for file in list:
await ctx.channel.send(f'**Title**: {file.display_name}\n**File ID**: {file.id}\n**URL**: {file.url}\n**Created on**: {file.created_at}\n**Size**: {file.size} bytes\n')
await ctx.channel.send('__**Here are your course files!**__'
+ "```\nOutputs include the file's: "
+ '\nTitle: [title]\nFile ID: [id]\nURL: [download link]\nCreated on: [time]\nSize: [size]\n```')
#Course folders command that shows all of the current folders within a specific course
@client.command(aliases = ['CF'])
async def course_folders(ctx, id = 'None'):
if(id == 'None'):
return await ctx.channel.send("You are missing the course id requirement!\nPlease insert an appropriate **course id**.\nEx: $cf **2421114**")
global dict
if (dict[f'{ctx.author.id}']["Access"] == 'None'):
await ctx.channel.send("**Uh-oh!** \nPlease check to see if you inserted the proper parameters. \nCheck **$info** for help.")
else:
Dict = 'j '.join(dict[f'{ctx.author.id}']["Access"])
access_token = (Dict)
canvas = Canvas(base_url,access_token)
course = canvas.get_course(id)
list = course.get_folders()
await ctx.channel.send(f'@{ctx.author.name} ```Here is your output!\n```')
for folder in list:
await ctx.channel.send(f'**Title**: {folder.name}\n**Folder ID**: {folder.id}\n**Created on**: {folder.created_at}\n**File count**: {folder.files_count}\n')
await ctx.channel.send('__**Here are your course folders!**__'
+ "```\nOutputs include the folder's: "
+ '\nTitle: [title]\nFile ID: [id]\nURL: [download link]\nCreated on: [time]\nSize: [size]\n```')
client.run(TOKEN)