This repository has been 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
/
Attempt 11
263 lines (244 loc) · 15.3 KB
/
Attempt 11
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
import discord
from discord.ext import commands
from canvasapi import Canvas
import re
import pprint
TOKEN = 'discord token'
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:
@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)
@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)
@client.event
async def on_command_error(ctx, error):
'''if isinstance(error, commands.MissingRequiredArgument):
await ctx.send('Please pass in all required arguments.'
+ f'\n{error}'
+ '\nRefer to **$info** to obtain examples and descriptions of every command.')'''
'''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 *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 __**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)
'''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]```')
@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.')
@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")
elif(title == 'None'):
return await ctx.channel.send("You are missing the title requirement!\nPlease insert an appropriate one word **title**.\nEx: $ce 11111111 Random")
elif(des == 'None'):
return await ctx.channel.send("You are missing the description requirement!\nPlease insert an appropriate one word **description**.\nEx: $ce 11111111 Random Description")
elif(start == 'None'):
return await ctx.channel.send("You are missing the start time requirement!\nPlease insert an appropriate **start time**.\nEx: $ce 11111111 Random Description 12/12/5:00")
elif(end == 'None'):
return await ctx.channel.send("You are missing the end time requirement!\nPlease insert an appropriate **end time**.\nEx: $ce 11111111 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```')
@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```')
@client.command(aliases = ['F'])
async def folder(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_folder(folder = 26147821)
await ctx.channel.send(f'@{ctx.author.name} ```Here is your output!\n```')
await ctx.channel.send(f'**Title**: {list.full_name}\n**Folder URL**: {list.folders_url}\n**Files URL**: {list.files_url}\n**File Count**: {list.files_count}\n**Parent Folder**: {list.parent_folder_id}')
@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]```')
@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")
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
polldict = {28468322:{"question": quest, "description": des}}
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.create_poll(poll = polldict)
await ctx.channel.send(f'@{ctx.author.name} ```Here is your output!\n```')
@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")
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")
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```')
client.run(TOKEN)