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 8
127 lines (115 loc) · 6.28 KB
/
Attempt 8
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
import discord
from discord.ext import commands
from canvasapi import Canvas
import re
import pprint
TOKEN = 'enter token'
intents = discord.Intents.default()
intents.members = True
client = commands.Bot(command_prefix = '$', intents=intents)
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)
'''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 *Courses command*"
+ "\n Type **$courses**, **$c** or **$C** to display all of your currently enrolled."
+ "\n *Event command*"
+ "\n Type **$events**, **$e** or **$E** to display all of your current events."
+ "\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 __**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):
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):
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.run(TOKEN)