-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcs221bot.py
124 lines (92 loc) · 4.8 KB
/
cs221bot.py
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
import argparse
import asyncio
import os
import random
import traceback
from io import BytesIO
from os.path import isfile, join
import discord
from discord.ext import commands
from dotenv import load_dotenv
from util.badargs import BadArgs
CANVAS_COLOR = 0xe13f2b
CANVAS_THUMBNAIL_URL = "https://lh3.googleusercontent.com/2_M-EEPXb2xTMQSTZpSUefHR3TjgOCsawM3pjVG47jI-BrHoXGhKBpdEHeLElT95060B=s180"
POLL_FILE = "data/poll.json"
GUILD_ID = 974449980947464214
load_dotenv()
CS221BOT_KEY = os.getenv("CS221BOT_KEY")
bot = commands.Bot(command_prefix="!", help_command=None, intents=discord.Intents.all())
parser = argparse.ArgumentParser(description="Run CS221Bot")
parser.add_argument("--cnu", dest="notify_unpublished", action="store_true",
help="Allow the bot to send notifications about unpublished Canvas modules (if you have access) as well as published ones.")
args = parser.parse_args()
async def status_task() -> None:
await bot.wait_until_ready()
while not bot.is_closed():
online_members = {member for guild in bot.guilds for member in guild.members if not member.bot and member.status != discord.Status.offline}
play = ["with the \"help\" command", " ", "with your mind", "ƃuᴉʎɐlԀ", "...something?",
"a game? Or am I?", "¯\_(ツ)_/¯", f"with {len(online_members)} people", "with image manipulation"]
listen = ["smart music", "... wait I can't hear anything",
"rush 🅱", "C++ short course"]
watch = ["TV", "YouTube vids", "over you",
"how to make a bot", "C++ tutorials", "I, Robot"]
rng = random.randrange(0, 3)
if rng == 0:
await bot.change_presence(activity=discord.Activity(type=discord.ActivityType.playing, name=random.choice(play)))
elif rng == 1:
await bot.change_presence(activity=discord.Activity(type=discord.ActivityType.listening, name=random.choice(listen)))
else:
await bot.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name=random.choice(watch)))
await asyncio.sleep(30)
def startup() -> None:
bot.get_cog("Canvas").canvas_init()
bot.get_cog("Piazza").piazza_start()
@bot.event
async def on_ready() -> None:
startup()
print("Logged in successfully")
bot.loop.create_task(status_task())
bot.loop.create_task(bot.get_cog("Piazza").send_pupdate())
bot.loop.create_task(bot.get_cog("Canvas").stream_tracking())
bot.loop.create_task(bot.get_cog("Canvas").assignment_reminder())
bot.loop.create_task(bot.get_cog("Canvas").update_modules())
@bot.event
async def on_message_edit(before: discord.Message, after: discord.Message) -> None:
await bot.process_commands(after)
@bot.event
async def on_message(message: discord.Message) -> None:
if isinstance(message.channel, discord.abc.PrivateChannel):
return
if not message.author.bot and message.guild.id == GUILD_ID:
# debugging
# with open("messages.txt", "a") as f:
# print(f"{message.guild.name}: {message.channel.name}: {message.author.name}: \"{message.content}\" @ {str(datetime.datetime.now())} \r\n", file = f)
# print(message.content)
await bot.process_commands(message)
if __name__ == "__main__":
# True if the bot should send notifications about new *unpublished* modules on Canvas; False otherwise.
# This only matters if the host of the bot has access to unpublished modules. If the host does
# not have access, then the bot won't know about any unpublished modules and won't send any info
# about them anyway.
bot.notify_unpublished = args.notify_unpublished
bot.guild_id = GUILD_ID
if bot.notify_unpublished:
print("Warning: bot will send notifications about unpublished modules (if you have access).")
for extension in filter(lambda f: isfile(join("cogs", f)) and f != "__init__.py", os.listdir("cogs")):
bot.load_extension(f"cogs.{extension[:-3]}")
print(f"{extension} module loaded")
@bot.event
async def on_command_error(ctx: commands.Context, error: commands.CommandError):
if isinstance(error, commands.CommandNotFound) or isinstance(error, discord.HTTPException):
pass
elif isinstance(error, BadArgs):
await error.print(ctx)
elif isinstance(error, commands.CommandOnCooldown):
await ctx.send(str(error), delete_after=error.retry_after)
elif isinstance(error, commands.MissingPermissions) or isinstance(error, commands.BotMissingPermissions) or isinstance(error, commands.BadArgument):
await ctx.send(str(error), delete_after=5)
else:
etype = type(error)
trace = error.__traceback__
await ctx.send(file=discord.File(BytesIO(bytes("".join(traceback.format_exception(etype, error, trace)), "utf-8")), filename="error.txt"))
bot.run(CS221BOT_KEY)