send the task completion message in the guild's configured channel

This commit is contained in:
Alex Ott
2022-12-30 03:15:22 -08:00
parent e4b097edff
commit d71ded1364
3 changed files with 48 additions and 17 deletions
+12 -1
View File
@@ -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)
+13 -2
View File
@@ -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)
+23 -14
View File
@@ -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)