college-bot-v1/cogs/assignments.py

602 lines
28 KiB
Python
Raw Normal View History

2022-10-09 19:27:02 +01:00
import datetime
import sqlite3
import textwrap
from typing import Optional
import discord
from discord.ext import commands, tasks
import config
2022-10-24 11:54:06 +01:00
from utils import Assignments, Tutors, simple_embed_paginator, get_or_none, Student, hyperlink, console
2022-10-09 19:27:02 +01:00
2022-10-30 16:31:38 +00:00
BOOL_EMOJI = {True: "\N{white heavy check mark}", False: "\N{cross mark}"}
2022-10-09 19:27:02 +01:00
TUTOR_OPTION = discord.Option(
str,
"The tutor who assigned the project",
default=None,
2022-10-30 16:31:38 +00:00
choices=[x.title() for x in dir(Tutors) if not x.startswith("__") and x not in ("name", "value")],
2022-10-09 19:27:02 +01:00
)
2022-10-10 18:11:10 +01:00
__MARK_AS_OPTION_OPTIONS = ("unfinished", "finished", "unsubmitted", "submitted")
MARK_AS_OPTION = discord.Option(
int,
name="status",
choices=[
discord.OptionChoice(
name="{}{}".format(BOOL_EMOJI[not x.startswith("un")], x),
value=__MARK_AS_OPTION_OPTIONS.index(x),
)
for x in __MARK_AS_OPTION_OPTIONS
2022-10-30 16:31:38 +00:00
],
2022-10-10 18:11:10 +01:00
)
2022-10-09 19:27:02 +01:00
class TutorSelector(discord.ui.View):
value: Optional[Tutors] = None
@discord.ui.select(
placeholder="Select a tutor name",
options=[
2022-10-30 16:31:38 +00:00
discord.SelectOption(label=x.title(), value=x.upper()) for x in [y.name for y in TUTOR_OPTION.choices]
],
2022-10-09 19:27:02 +01:00
)
async def select_tutor(self, select: discord.ui.Select, interaction2: discord.Interaction):
await interaction2.response.defer(invisible=True)
self.value = getattr(Tutors, select.values[0].upper())
self.stop()
async def assignment_autocomplete(ctx: discord.AutocompleteContext) -> list[str]:
if not ctx.value:
results: list[Assignments] = await Assignments.objects.order_by("-due_by").limit(7).all()
else:
2022-10-30 16:31:38 +00:00
results: list[Assignments] = (
await Assignments.objects.filter(title__icontains=ctx.value).limit(30).order_by("-entry_id").all()
)
return [textwrap.shorten(f"{x.entry_id}: {x.title}", 100, placeholder="...") for x in results]
2022-10-09 19:27:02 +01:00
# noinspection DuplicatedCode
class AssignmentsCog(commands.Cog):
def __init__(self, bot: commands.Bot):
self.bot = bot
self.reminder_loop.start()
def cog_unload(self):
self.reminder_loop.stop()
@tasks.loop(minutes=10)
async def reminder_loop(self):
if not self.bot.is_ready():
await self.bot.wait_until_ready()
2022-10-14 21:27:37 +01:00
try:
view_command = "</{0.name} view:{0.id}>".format(
2022-10-30 16:31:38 +00:00
self.bot.get_application_command("assignments", type=discord.SlashCommandGroup)
2022-10-14 21:27:37 +01:00
)
edit_command = "</{0.name} edit:{0.id}>".format(
2022-10-30 16:31:38 +00:00
self.bot.get_application_command("assignments", type=discord.SlashCommandGroup)
2022-10-14 21:27:37 +01:00
)
except AttributeError:
view_command = "`/assignments view`"
edit_command = "`/assignments edit`"
2022-10-09 19:27:02 +01:00
allowed_mentions = discord.AllowedMentions(everyone=True) if not config.dev else discord.AllowedMentions.none()
guild = self.bot.get_guild(config.guilds[0])
general = discord.utils.get(guild.text_channels, name="general")
if not general.can_send():
return
2022-10-30 16:31:38 +00:00
msg_format = (
"@everyone {reminder_name} reminder for project {project_title} for **{project_tutor}**!\n"
"Run '%s {project_title}' to view information on the assignment.\n"
"*You can mark this assignment as complete with '%s {project_title}', which will prevent"
" further reminders.*" % (view_command, edit_command)
)
2022-10-09 19:27:02 +01:00
now = datetime.datetime.now()
assignments: list[Assignments] = await Assignments.objects.filter(submitted=False).all()
for assignment in assignments:
due = datetime.datetime.fromtimestamp(assignment.due_by)
for reminder_name, reminder_time in config.reminders.items():
if reminder_name in assignment.reminders:
# already sent
continue
elif reminder_time != 3600 * 3 and assignment.finished is True:
continue
elif isinstance(reminder_time, int) and reminder_time >= (assignment.due_by - assignment.created_at):
await assignment.update(reminders=assignment.reminders + [reminder_name])
else:
if isinstance(reminder_time, datetime.time):
2022-10-10 18:11:10 +01:00
if now.date() == due.date():
2022-10-09 19:27:02 +01:00
if now.time().hour == reminder_time.hour:
try:
await general.send(
msg_format.format(
reminder_name=reminder_name,
project_title=textwrap.shorten(assignment.title, 100, placeholder="..."),
2022-10-30 16:31:38 +00:00
project_tutor=assignment.tutor.name.title(),
2022-10-09 19:27:02 +01:00
),
2022-10-30 16:31:38 +00:00
allowed_mentions=allowed_mentions,
2022-10-09 19:27:02 +01:00
)
except discord.HTTPException:
pass
else:
await assignment.update(reminders=assignment.reminders + [reminder_name])
else:
time = due - datetime.timedelta(seconds=reminder_time)
if time <= now:
try:
await general.send(
msg_format.format(
reminder_name=reminder_name,
project_title=textwrap.shorten(assignment.title, 100, placeholder="..."),
2022-10-30 16:31:38 +00:00
project_tutor=assignment.tutor.name.title(),
2022-10-09 19:27:02 +01:00
),
2022-10-30 16:31:38 +00:00
allowed_mentions=allowed_mentions,
2022-10-09 19:27:02 +01:00
)
except discord.HTTPException:
pass
else:
await assignment.update(reminders=assignment.reminders + [reminder_name])
@staticmethod
def generate_assignment_embed(assignment: Assignments) -> discord.Embed:
embed = discord.Embed(
title=f"Assignment #{assignment.entry_id}",
description=f"**Title:**\n>>> {assignment.title}",
2022-10-30 16:31:38 +00:00
colour=discord.Colour.random(),
2022-10-09 19:27:02 +01:00
)
if assignment.classroom:
classroom = hyperlink(assignment.classroom, max_length=1024)
else:
classroom = "No classroom link."
if assignment.shared_doc:
shared_doc = hyperlink(assignment.shared_doc, max_length=1024)
else:
shared_doc = "No shared document."
embed.add_field(name="Classroom URL:", value=classroom, inline=False),
embed.add_field(name="Shared Document URL:", value=shared_doc)
embed.add_field(name="Tutor:", value=assignment.tutor.name.title(), inline=False)
2022-10-30 16:31:38 +00:00
user_id = getattr(assignment.created_by, "user_id", assignment.entry_id)
embed.add_field(name="Created:", value=f"<t:{assignment.created_at:.0f}:R> by <@{user_id}>", inline=False)
2022-10-09 19:27:02 +01:00
embed.add_field(
name="Due:",
value=f"<t:{assignment.due_by:.0f}:R> "
2022-10-30 16:31:38 +00:00
f"(finished: {BOOL_EMOJI[assignment.finished]} | Submitted: {BOOL_EMOJI[assignment.submitted]})",
inline=False,
2022-10-09 19:27:02 +01:00
)
if assignment.reminders:
embed.set_footer(text="Reminders sent: " + ", ".join(assignment.reminders))
return embed
2022-10-30 16:31:38 +00:00
assignments_command = discord.SlashCommandGroup("assignments", "Assignment/project management", guild_only=True)
2022-10-09 19:27:02 +01:00
@assignments_command.command(name="list")
async def list_assignments(
2022-10-30 16:31:38 +00:00
self,
ctx: discord.ApplicationContext,
limit: int = 20,
upcoming_only: bool = True,
tutor_name: TUTOR_OPTION = None,
unfinished_only: bool = False,
unsubmitted_only: bool = False,
2022-10-09 19:27:02 +01:00
):
"""Lists assignments."""
tutor_name: Optional[str]
query = Assignments.objects.limit(limit).order_by("-due_by")
if upcoming_only is True:
now = datetime.datetime.now().timestamp()
query = query.filter(due_by__gte=now)
if tutor_name is not None:
query = query.filter(tutor=getattr(Tutors, tutor_name.upper()))
if unfinished_only is True:
query = query.filter(finished=False)
if unsubmitted_only:
query = query.filter(submitted=False)
await ctx.defer()
lines = []
for assignment in await query.all():
assignment: Assignments
due_by = datetime.datetime.fromtimestamp(assignment.due_by)
lines.append(
f"#{assignment.entry_id!s}: Set by **{assignment.tutor.name.title()}**, "
f"due {discord.utils.format_dt(due_by, 'R')}"
)
embeds = simple_embed_paginator(lines, assert_ten=True, colour=ctx.author.colour)
2022-10-30 16:31:38 +00:00
embeds = embeds or [discord.Embed(description="No projects match the provided criteria.")]
2022-10-09 19:27:02 +01:00
2022-10-30 16:31:38 +00:00
return await ctx.respond(embeds=embeds)
2022-10-09 19:27:02 +01:00
@assignments_command.command(name="add")
async def create_assignment(self, ctx: discord.ApplicationContext):
"""Adds/creates an assignment."""
author = await get_or_none(Student, user_id=ctx.author.id)
if author is None:
return await ctx.respond("\N{cross mark} You must have verified to use this command.", ephemeral=True)
class AddModal(discord.ui.Modal):
def __init__(self, kwargs: dict = None):
self.msg: Optional[discord.WebhookMessage] = None
self.create_kwargs = kwargs or {
"created_by": author,
"title": None,
"classroom": None,
"shared_doc": None,
"due_by": None,
2022-10-30 16:31:38 +00:00
"tutor": None,
2022-10-09 19:27:02 +01:00
}
super().__init__(
discord.ui.InputText(
custom_id="title",
label="Assignment Title",
min_length=2,
max_length=2000,
2022-10-30 16:31:38 +00:00
value=self.create_kwargs["title"],
2022-10-09 19:27:02 +01:00
),
discord.ui.InputText(
custom_id="classroom",
label="Google Classroom Link",
max_length=4000,
required=False,
placeholder="Optional, can be added later.",
2022-10-30 16:31:38 +00:00
value=self.create_kwargs["classroom"],
2022-10-09 19:27:02 +01:00
),
discord.ui.InputText(
custom_id="shared_doc",
label="Shared Document Link",
max_length=4000,
required=False,
placeholder="Google docs, slides, powerpoint, etc. Optional.",
2022-10-30 16:31:38 +00:00
value=self.create_kwargs["shared_doc"],
2022-10-09 19:27:02 +01:00
),
discord.ui.InputText(
custom_id="due_by",
label="Due by",
max_length=16,
min_length=14,
placeholder="dd/mm/yy hh:mm".upper(),
value=(
self.create_kwargs["due_by"].strftime("%d/%m/%y %H:%M")
2022-10-30 16:31:38 +00:00
if self.create_kwargs["due_by"]
else None
),
2022-10-09 19:27:02 +01:00
),
title="Add an assignment",
2022-10-30 16:31:38 +00:00
timeout=300,
2022-10-09 19:27:02 +01:00
)
async def callback(self, interaction: discord.Interaction):
await interaction.response.defer()
self.create_kwargs["title"] = self.children[0].value
self.create_kwargs["classroom"] = self.children[1].value or None
self.create_kwargs["shared_doc"] = self.children[2].value or None
try:
self.create_kwargs["due_by"] = datetime.datetime.strptime(
self.children[3].value,
2022-10-30 16:31:38 +00:00
"%d/%m/%y %H:%M" if len(self.children[3].value) == 14 else "%d/%m/%Y %H:%M",
2022-10-09 19:27:02 +01:00
)
except ValueError:
2022-10-30 16:31:38 +00:00
2022-10-09 19:27:02 +01:00
class TryAgainView(discord.ui.View):
def __init__(self, kw):
self._mod = None
self.kw = kw
super().__init__(timeout=330)
@property
def modal(self) -> Optional[AddModal]:
return self._mod
@discord.ui.button(label="Try again", style=discord.ButtonStyle.primary)
async def try_again(self, _, interaction2: discord.Interaction):
self.disable_all_items()
self._mod = AddModal(self.kw)
await interaction2.response.send_modal(self._mod)
await interaction2.edit_original_response(view=self)
await self._mod.wait()
self.stop()
v = TryAgainView(self.create_kwargs)
2022-10-30 16:31:38 +00:00
msg = await interaction.followup.send("\N{cross mark} Failed to parse date - try again?", view=v)
2022-10-09 19:27:02 +01:00
await v.wait()
if v.modal:
self.create_kwargs = v.modal.create_kwargs
else:
return
else:
view = TutorSelector()
2022-10-30 16:31:38 +00:00
msg = await interaction.followup.send("Which tutor assigned this project?", view=view)
2022-10-09 19:27:02 +01:00
await view.wait()
self.create_kwargs["tutor"] = view.value
self.msg = msg
self.stop()
modal = AddModal()
await ctx.send_modal(modal)
await modal.wait()
if not modal.msg:
return
2022-10-30 16:31:38 +00:00
await modal.msg.edit(content="Creating assignment...", view=None)
2022-10-09 19:27:02 +01:00
try:
modal.create_kwargs["due_by"] = modal.create_kwargs["due_by"].timestamp()
await Assignments.objects.create(**modal.create_kwargs)
except sqlite3.Error as e:
return await modal.msg.edit(content="SQL Error: %s.\nAssignment not saved." % e)
else:
2022-10-30 16:31:38 +00:00
return await modal.msg.edit(content=f"\N{white heavy check mark} Created assignment!")
2022-10-09 19:27:02 +01:00
@assignments_command.command(name="view")
async def get_assignment(
2022-10-30 16:31:38 +00:00
self, ctx: discord.ApplicationContext, title: discord.Option(str, autocomplete=assignment_autocomplete)
2022-10-09 19:27:02 +01:00
):
"""Views an assignment's details"""
try:
entry_id = int(title.split(":", 1)[0])
2022-10-09 19:27:02 +01:00
except ValueError:
return await ctx.respond("\N{cross mark} Invalid Input.")
assignment: Assignments = await get_or_none(Assignments, entry_id=int(entry_id))
if not assignment:
return await ctx.respond("\N{cross mark} Unknown assignment.")
2022-10-24 11:54:06 +01:00
try:
await assignment.created_by.load()
except AttributeError:
console.log(f"[red]Failed to load created_by row for assignment {assignment.entry_id}")
2022-10-09 19:27:02 +01:00
return await ctx.respond(embed=self.generate_assignment_embed(assignment))
@assignments_command.command(name="edit")
async def edit_assignment(
2022-10-30 16:31:38 +00:00
self, ctx: discord.ApplicationContext, title: discord.Option(str, autocomplete=assignment_autocomplete)
2022-10-09 19:27:02 +01:00
):
"""Edits an assignment"""
try:
entry_id = int(title.split(":", 1)[0])
2022-10-09 19:27:02 +01:00
except ValueError:
return await ctx.respond("\N{cross mark} Invalid Input.")
assignment: Assignments = await get_or_none(Assignments, entry_id=int(entry_id))
if not assignment:
return await ctx.respond("\N{cross mark} Unknown assignment.")
await assignment.created_by.load()
class EditAssignmentView(discord.ui.View):
def __init__(self):
super().__init__(timeout=300)
async def interaction_check(self, interaction: discord.Interaction) -> bool:
return interaction.user == ctx.author
async def on_timeout(self) -> None:
await self.message.delete(delay=0.1)
2022-10-11 14:11:23 +01:00
async def update_display(self, interaction: discord.Interaction):
complete_d = "incomplete" if assignment.finished else "complete"
submitted_d = "unsubmitted" if assignment.submitted else "submitted"
self.get_item("complete").label = "Mark as " + complete_d
self.get_item("submitted").label = "Mark as " + submitted_d
await interaction.edit_original_response(view=self)
2022-10-09 19:27:02 +01:00
@discord.ui.button(label="Update title")
async def update_title(self, _, interaction: discord.Interaction):
class UpdateTitleModal(discord.ui.Modal):
def __init__(self):
super().__init__(
discord.ui.InputText(
style=discord.InputTextStyle.long,
label="New title",
value=assignment.title,
min_length=2,
max_length=4000,
),
2022-10-30 16:31:38 +00:00
title="Update assignment title",
2022-10-09 19:27:02 +01:00
)
async def callback(self, _interaction: discord.Interaction):
await _interaction.response.defer()
await assignment.update(title=self.children[0].value)
await _interaction.followup.send(
2022-10-30 16:31:38 +00:00
"\N{white heavy check mark} Changed assignment title!", delete_after=5
2022-10-09 19:27:02 +01:00
)
self.stop()
modal = UpdateTitleModal()
await interaction.response.send_modal(modal)
2022-10-11 14:11:23 +01:00
await self.update_display(interaction)
2022-10-09 19:27:02 +01:00
@discord.ui.button(label="Update classroom URL")
async def update_classroom_url(self, _, interaction: discord.Interaction):
class UpdateClassroomURL(discord.ui.Modal):
def __init__(self):
super().__init__(
discord.ui.InputText(
style=discord.InputTextStyle.long,
label="New Classroom URL",
value=assignment.classroom,
required=False,
max_length=4000,
),
2022-10-30 16:31:38 +00:00
title="Update Classroom url",
2022-10-09 19:27:02 +01:00
)
async def callback(self, _interaction: discord.Interaction):
await _interaction.response.defer()
try:
await assignment.update(classroom=self.children[0].value)
await _interaction.followup.send(
2022-10-30 16:31:38 +00:00
"\N{white heavy check mark} Changed classroom URL!", delete_after=5
2022-10-09 19:27:02 +01:00
)
except sqlite3.Error:
await _interaction.followup.send(
"\N{cross mark} Failed to apply changes - are you sure you put a valid URL in?"
)
finally:
self.stop()
modal = UpdateClassroomURL()
await interaction.response.send_modal(modal)
2022-10-11 14:11:23 +01:00
await self.update_display(interaction)
2022-10-09 19:27:02 +01:00
@discord.ui.button(label="Update shared document url")
async def update_shared_document_url(self, _, interaction: discord.Interaction):
class UpdateSharedDocumentModal(discord.ui.Modal):
def __init__(self):
super().__init__(
discord.ui.InputText(
style=discord.InputTextStyle.long,
label="New shared document URL",
value=assignment.shared_doc,
required=False,
max_length=4000,
),
2022-10-30 16:31:38 +00:00
title="Update shared document url",
2022-10-09 19:27:02 +01:00
)
async def callback(self, _interaction: discord.Interaction):
await _interaction.response.defer()
try:
await assignment.update(shared_doc=self.children[0].value)
await _interaction.followup.send(
2022-10-30 16:31:38 +00:00
"\N{white heavy check mark} Changed shared doc URL!", delete_after=5
2022-10-09 19:27:02 +01:00
)
except sqlite3.Error:
await _interaction.followup.send(
"\N{cross mark} Failed to apply changes - are you sure you put a valid URL in?"
)
finally:
self.stop()
modal = UpdateSharedDocumentModal()
await interaction.response.send_modal(modal)
2022-10-11 14:11:23 +01:00
await self.update_display(interaction)
2022-10-09 19:27:02 +01:00
@discord.ui.button(label="Update tutor")
async def update_tutor(self, _, interaction: discord.Interaction):
await interaction.response.defer()
view = TutorSelector()
msg: discord.WebhookMessage = await interaction.followup.send(
2022-10-30 16:31:38 +00:00
"Which tutor assigned this project?", view=view
2022-10-09 19:27:02 +01:00
)
await view.wait()
await assignment.update(tutor=view.value)
await msg.edit(
2022-10-30 16:31:38 +00:00
content=f"\N{white heavy check mark} Changed tutor to {view.value.name.title()}", view=None
2022-10-09 19:27:02 +01:00
)
await msg.delete(delay=5)
2022-10-11 14:11:23 +01:00
await self.update_display(interaction)
2022-10-09 19:27:02 +01:00
2022-10-11 14:11:23 +01:00
@discord.ui.button(label="Update due date")
2022-10-09 19:27:02 +01:00
async def update_due(self, _, interaction: discord.Interaction):
2022-10-11 14:11:23 +01:00
class UpdateDateModal(discord.ui.Modal):
def __init__(self):
self.date = datetime.datetime.fromtimestamp(assignment.due_by)
super().__init__(
discord.ui.InputText(
label="New due by date",
placeholder=self.date.strftime("%d/%m/%y %H:%M"),
value=self.date.strftime("%d/%m/%y %H:%M"),
min_length=14,
2022-10-30 16:31:38 +00:00
max_length=16,
2022-10-11 14:11:23 +01:00
),
2022-10-30 16:31:38 +00:00
title="Change due by date",
2022-10-11 14:11:23 +01:00
)
async def callback(self, _interaction: discord.Interaction):
await _interaction.response.defer()
try:
new = datetime.datetime.strptime(
self.children[1].value,
2022-10-30 16:31:38 +00:00
"%d/%m/%y %H:%M" if len(self.children[1].value) == 14 else "%d/%m/%Y %H:%M",
2022-10-11 14:11:23 +01:00
)
except ValueError:
await _interaction.followup.send(
"\N{cross mark} Failed to parse URL. Make sure you passed in dd/mm/yy hh:mm"
" (e.g. {})".format(datetime.datetime.now().strftime("%d/%m/%y %H:%M"))
)
self.stop()
else:
try:
await assignment.update(due_by=new.timestamp(), reminders=[])
await _interaction.followup.send(
2022-10-30 16:31:38 +00:00
"\N{white heavy check mark} Changed due by date & reset reminders.", delete_after=5
2022-10-11 14:11:23 +01:00
)
except sqlite3.Error:
2022-10-30 16:31:38 +00:00
await _interaction.followup.send("\N{cross mark} Failed to apply changes.")
2022-10-11 14:11:23 +01:00
finally:
self.stop()
2022-10-30 16:31:38 +00:00
2022-10-11 14:11:23 +01:00
await interaction.response.send_modal(UpdateDateModal())
await self.update_display(interaction)
2022-10-09 19:27:02 +01:00
2022-10-11 14:11:23 +01:00
@discord.ui.button(label="Mark as [in]complete", custom_id="complete")
2022-10-09 19:27:02 +01:00
async def mark_as_complete(self, _, interaction: discord.Interaction):
await interaction.response.defer()
if assignment.submitted is True and assignment.submitted is True:
return await interaction.followup.send(
"\N{cross mark} You cannot mark an assignment as incomplete if it is marked as submitted!"
)
await assignment.update(finished=not assignment.finished)
2022-10-11 14:11:23 +01:00
await self.update_display(interaction)
2022-10-09 19:27:02 +01:00
return await interaction.followup.send(
"\N{white heavy check mark} Assignment is now marked as {}complete.".format(
"in" if assignment.finished is False else ""
)
)
2022-10-11 14:11:23 +01:00
@discord.ui.button(label="Mark as [un]submitted", custom_id="submitted")
2022-10-09 19:27:02 +01:00
async def mark_as_submitted(self, _, interaction: discord.Interaction):
await interaction.response.defer()
if assignment.finished is False and assignment.submitted is False:
return await interaction.followup.send(
"\N{cross mark} You cannot mark an assignment as submitted if it is not marked as complete!",
2022-10-30 16:31:38 +00:00
delete_after=10,
2022-10-09 19:27:02 +01:00
)
await assignment.update(submitted=not assignment.submitted)
2022-10-11 14:11:23 +01:00
await self.update_display(interaction)
2022-10-09 19:27:02 +01:00
return await interaction.followup.send(
"\N{white heavy check mark} Assignment is now marked as {}submitted.".format(
"in" if assignment.submitted is False else ""
),
2022-10-30 16:31:38 +00:00
delete_after=5,
2022-10-09 19:27:02 +01:00
)
@discord.ui.button(label="Save & Exit")
async def finish(self, _, interaction: discord.Interaction):
await interaction.response.defer()
await interaction.delete_original_response(delay=0.1)
self.stop()
@discord.ui.button(label="View details")
async def view_details(self, _, interaction: discord.Interaction):
await interaction.response.defer(ephemeral=True)
await assignment.created_by.load()
await interaction.followup.send(
2022-10-30 16:31:38 +00:00
embed=AssignmentsCog.generate_assignment_embed(assignment), ephemeral=True
2022-10-09 19:27:02 +01:00
)
2022-10-11 14:11:23 +01:00
await self.update_display(interaction)
2022-10-09 19:27:02 +01:00
await ctx.respond(view=EditAssignmentView())
@assignments_command.command(name="remove")
async def remove_assignment(
2022-10-30 16:31:38 +00:00
self, ctx: discord.ApplicationContext, title: discord.Option(str, autocomplete=assignment_autocomplete)
2022-10-09 19:27:02 +01:00
):
"""Edits an assignment"""
try:
entry_id = int(title.split(":", 1)[0])
2022-10-09 19:27:02 +01:00
except ValueError:
return await ctx.respond("\N{cross mark} Invalid Input.")
assignment: Assignments = await get_or_none(Assignments, entry_id=int(entry_id))
if not assignment:
return await ctx.respond("\N{cross mark} Unknown assignment.")
await assignment.delete()
return await ctx.respond(f"\N{white heavy check mark} Deleted assignment #{assignment.entry_id}.")
def setup(bot):
bot.add_cog(AssignmentsCog(bot))