Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a function that parses a message and try to generate a build from it #39

Merged
merged 18 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ SUPABASE_KEY=your_key_here
CATBOX_USERHASH=your_hash_here

# Provide a secret to trusted minecraft servers to autheticate users
SYNERGY_SECRET=your_secret_here
SYNERGY_SECRET=your_secret_here

OPENAI_API_KEY=your_key_here
15 changes: 13 additions & 2 deletions api.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Simple FastAPI server to generate verification codes for users."""

import os
import random
from typing import Annotated
Expand Down Expand Up @@ -31,10 +32,20 @@ async def get_verification_code(user: User, authorization: Annotated[str, Header

db = DatabaseManager()
# Invalidate existing codes for this user
await db.table("verification_codes").update({"valid": False}).eq("minecraft_uuid", str(user.uuid)).gt("expires", utcnow()).execute()
await (
db.table("verification_codes")
.update({"valid": False})
.eq("minecraft_uuid", str(user.uuid))
.gt("expires", utcnow())
.execute()
)

code = random.randint(100000, 999999)
await db.table("verification_codes").insert({"minecraft_uuid": str(user.uuid), "username": username, "code": code}).execute()
await (
db.table("verification_codes")
.insert({"minecraft_uuid": str(user.uuid), "username": username, "code": code})
.execute()
)
return code


Expand Down
38 changes: 35 additions & 3 deletions bot/submission/submit.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
# from __future__ import annotations # dpy cannot resolve FlagsConverter with forward references :(

from collections.abc import Sequence
from textwrap import dedent
from typing import Literal, cast, TYPE_CHECKING, Any

import discord
from discord import InteractionResponse, Guild
from discord import InteractionResponse, Guild, Message
from discord.ext import commands
from discord.ext.commands import (
Context,
Expand All @@ -16,6 +17,7 @@
flag,
)
from postgrest import APIResponse
from pydantic import ValidationError

from bot import utils, config
from bot.submission.ui import BuildSubmissionForm, ConfirmationView
Expand All @@ -24,7 +26,7 @@
from database.database import DatabaseManager
from database.enums import Status, Category
from bot._types import SubmissionCommandResponse, GuildMessageable
from bot.utils import RunningMessage, parse_dimensions
from bot.utils import RunningMessage, parse_dimensions, parse_build_title, remove_markdown
from database.message import get_build_id_by_message
from database.schema import TypeRecord
from database.server_settings import get_server_setting
Expand Down Expand Up @@ -373,7 +375,9 @@ async def list_patterns(self, ctx: Context):
async with RunningMessage(ctx) as sent_message:
patterns: APIResponse[TypeRecord] = await DatabaseManager().table("types").select("*").execute()
names = [pattern["name"] for pattern in patterns.data]
await sent_message.edit(content="Here are the available patterns:", embed=utils.info_embed("Patterns", ", ".join(names)))
await sent_message.edit(
content="Here are the available patterns:", embed=utils.info_embed("Patterns", ", ".join(names))
)

@Cog.listener(name="on_raw_reaction_add")
async def confirm_record(self, payload: discord.RawReactionActionEvent):
Expand Down Expand Up @@ -424,6 +428,34 @@ async def confirm_record(self, payload: discord.RawReactionActionEvent):
# TODO: Add a check when adding vote channels to the database
raise ValueError(f"Invalid channel type for a vote channel: {type(vote_channel)}")

@Cog.listener(name="on_message")
async def infer_build_from_title(self, message: Message):
"""Infer a build from a message."""
if message.author.bot:
return

if message.channel.id not in [726156829629087814, 667401499554611210, 536004554743873556]:
return

title_str = remove_markdown(message.content).splitlines()[0]
try:
title = await parse_build_title(title_str, mode="ai" if len(title_str) <= 300 else "manual")
except ValidationError:
return

build = Build()
build.record_category = title.record_category
build.category = "Door"
build.component_restrictions = title.component_restrictions
build.door_width = title.door_width
build.door_height = title.door_height
build.door_depth = title.door_depth
build.wiring_placement_restrictions = title.wiring_placement_restrictions
build.door_types = title.door_types
build.door_orientation_type = title.orientation
# print(title)
await self.bot.get_channel(536004554743873556).send(embed=build.generate_embed())


def format_submission_input(ctx: Context, data: SubmissionCommandResponse) -> dict[str, Any]:
"""Formats the submission data from what is passed in commands to something recognizable by Build."""
Expand Down
Loading