From d71ded13644047d46c7c76b8a14f8ad6e55594a2 Mon Sep 17 00:00:00 2001 From: Alex Ott <66271487+AlexanderHOtt@users.noreply.github.com> Date: Fri, 30 Dec 2022 03:15:22 -0800 Subject: [PATCH] send the task completion message in the guild's configured channel --- discord-bot/bot/db/schemas.py | 13 +++++++- discord-bot/bot/extensions/text_labels.py | 15 +++++++-- discord-bot/bot/extensions/work.py | 37 ++++++++++++++--------- 3 files changed, 48 insertions(+), 17 deletions(-) diff --git a/discord-bot/bot/db/schemas.py b/discord-bot/bot/db/schemas.py index e3ce9032..efb903b8 100644 --- a/discord-bot/bot/db/schemas.py +++ b/discord-bot/bot/db/schemas.py @@ -1,7 +1,8 @@ # -*- coding: utf-8 -*- """Database schemas.""" -from aiosqlite import Row +from aiosqlite import Row, Connection from pydantic import BaseModel +import typing as t class GuildSettings(BaseModel): @@ -14,3 +15,13 @@ class GuildSettings(BaseModel): def parse_obj(cls, obj: Row) -> "GuildSettings": """Deserialize a Row object from aiosqlite into a GuildSettings object.""" return cls(guild_id=obj[0], log_channel_id=obj[1]) + + @classmethod + async def from_db(cls, conn: Connection, guild_id: int) -> t.Optional["GuildSettings"]: + async with conn.cursor() as cursor: + await cursor.execute("SELECT * FROM guild_settings WHERE guild_id = ?", (guild_id,)) + row = await cursor.fetchone() + if row is None: + raise ValueError("No settings found for this guild.") + + return cls.parse_obj(row) diff --git a/discord-bot/bot/extensions/text_labels.py b/discord-bot/bot/extensions/text_labels.py index e9d08c86..212aa04b 100644 --- a/discord-bot/bot/extensions/text_labels.py +++ b/discord-bot/bot/extensions/text_labels.py @@ -6,12 +6,15 @@ from datetime import datetime import hikari import lightbulb import miru +from aiosqlite import Connection plugin = lightbulb.Plugin( - "HotReloadPlugin", + "TextLabels", ) +plugin.add_checks(lightbulb.guild_only) # Context menus are only enabled in guilds from bot.utils import EMPTY +from bot.db.schemas import GuildSettings DISCORD_GRAY = 0x2F3136 @@ -48,6 +51,14 @@ class LabelModal(miru.Modal): ) # Send a notification to the log channel + assert context.guild_id is not None # `guild_only` check + conn: Connection = context.bot.d.db # type: ignore + guild_settings = await GuildSettings.from_db(conn, context.guild_id) + + + if guild_settings is None or guild_settings.log_channel_id is None: + return + embed = ( hikari.Embed( title="Message Label", @@ -61,7 +72,7 @@ class LabelModal(miru.Modal): .add_field("Global Ranking", "0/0", inline=True) .set_footer("Message ID: TODO") ) - channel = await context.bot.rest.fetch_channel(1058299131115872297) + channel = await context.bot.rest.fetch_channel(guild_settings.log_channel_id) assert isinstance(channel, hikari.TextableChannel) await channel.send(EMPTY, embed=embed) diff --git a/discord-bot/bot/extensions/work.py b/discord-bot/bot/extensions/work.py index 86462f87..1c1f38de 100644 --- a/discord-bot/bot/extensions/work.py +++ b/discord-bot/bot/extensions/work.py @@ -4,6 +4,7 @@ import asyncio import logging import typing as t from datetime import datetime +from aiosqlite import Connection import hikari import lightbulb @@ -14,6 +15,7 @@ from oasst_shared.schemas.protocol import TaskRequestType from bot.api_client import OasstApiClient, TaskType from bot.utils import EMPTY +from bot.db.schemas import GuildSettings plugin = lightbulb.Plugin("WorkPlugin") @@ -108,21 +110,28 @@ async def _handle_task(ctx: lightbulb.SlashContext, task_type: TaskRequestType) # Send a message in the log channel that the task is complete # TODO: Maybe do something with the msg ID - done_embed = ( - hikari.Embed( - title="Task Completion", - description=f"`{task.type}` completed by {ctx.author.mention}", - color=hikari.Color(0x00FF00), - timestamp=datetime.now().astimezone(), + assert ctx.guild_id is not None + conn: Connection = ctx.bot.d.db + guild_settings = await GuildSettings.from_db(conn, ctx.guild_id) + + if guild_settings is not None and guild_settings.log_channel_id is not None: + + channel = await ctx.bot.rest.fetch_channel(guild_settings.log_channel_id) + assert isinstance(channel, hikari.TextableChannel) # option converter + + done_embed = ( + hikari.Embed( + title="Task Completion", + description=f"`{task.type}` completed by {ctx.author.mention}", + color=hikari.Color(0x00FF00), + timestamp=datetime.now().astimezone(), + ) + .add_field("Total Tasks", "0", inline=True) + .add_field("Server Ranking", "0/0", inline=True) + .add_field("Global Ranking", "0/0", inline=True) + .set_footer(f"Task ID: {task.id}") ) - .add_field("Total Tasks", "0", inline=True) - .add_field("Server Ranking", "0/0", inline=True) - .add_field("Global Ranking", "0/0", inline=True) - .set_footer(f"Task ID: {task.id}") - ) - channel = await ctx.bot.rest.fetch_channel(1058299131115872297) - assert isinstance(channel, hikari.TextableChannel) - await channel.send(EMPTY, embed=done_embed) + await channel.send(EMPTY, embed=done_embed) # ask the user if they want to do another task choice_view = ChoiceView(timeout=MAX_TASK_ACCEPT_TIME)