Skip to content

Commit

Permalink
Modify command
Browse files Browse the repository at this point in the history
  • Loading branch information
trickeydan committed Sep 17, 2024
1 parent cbf743a commit 5208b2b
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 15 deletions.
51 changes: 44 additions & 7 deletions kmibot/api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from datetime import datetime
from logging import getLogger
from typing import Any, Optional
from typing import Any
from uuid import UUID
import discord
import httpx
Expand Down Expand Up @@ -74,6 +74,14 @@ def parse_menu_url(cls, val: str) -> str | None:
return val if val else None


class PubEventSchema(BaseModel):
id: UUID
timestamp: datetime
pub: UUID
discord_id: int | None
attendees: list[UUID]


class FerryAPI:
def __init__(self, api_url: str, api_key: str) -> None:
self._api_url = api_url
Expand All @@ -91,7 +99,13 @@ async def _request(self, method: str, endpoint: str, **kwargs) -> Any:
resp = await self._client.request(
method, self._api_url + endpoint, headers=headers, **kwargs
)
resp.raise_for_status()
try:
resp.raise_for_status()
except httpx.HTTPStatusError as e:
LOGGER.error(str(e))
LOGGER.error(resp.content)
raise

data = resp.json()
LOGGER.info(f"{method} {endpoint} -> {resp.status_code} {data} ")
return data
Expand Down Expand Up @@ -134,16 +148,13 @@ async def get_fact_for_person(self, person_id: UUID) -> FactSchema:
data = await self._request("GET", f"v2/people/{person_id}/fact/")
return FactSchema.model_validate(data)

async def create_accusation(
self, created_by: UUID, suspect: UUID, quote: str
) -> AccusationSchema:
async def create_accusation(self, created_by: UUID, suspect: UUID, quote: str) -> None:
payload = {
"quote": quote,
"suspect": str(suspect),
"created_by": str(created_by),
}
data = await self._request("POST", "v2/court/accusations/", json=payload)
return AccusationSchema.model_validate(data)
await self._request("POST", "v2/court/accusations/", json=payload)

async def get_accusation(self, accusation_id: UUID) -> AccusationSchema:
data = await self._request("GET", f"v2/court/accusations/{accusation_id}/")
Expand All @@ -166,3 +177,29 @@ async def get_pubs(
data = await self._request("GET", "v2/pub/pubs/")
ta = TypeAdapter(list[PubSchema])
return ta.validate_python(data["results"])

async def create_pub_event(
self,
timestamp: datetime,
pub_id: UUID,
created_by: UUID,
scheduled_event_id: int,
) -> None:
payload = {
"timestamp": timestamp.isoformat(),
"pub": str(pub_id),
"discord_id": scheduled_event_id,
"table": None,
"attendees": [],
"created_by": str(created_by),
}
await self._request("POST", "v2/pub/events/", json=payload)

async def get_pub_event_by_discord_id(self, scheduled_event_id: int) -> PubEventSchema:
try:
data = await self._request("GET", f"v2/pub/events/?discord_id={scheduled_event_id}")
except httpx.HTTPStatusError as exc:
raise exc

ta = TypeAdapter(list[PubEventSchema])
return ta.validate_python(data["results"])[0]
2 changes: 1 addition & 1 deletion kmibot/modules/ferry/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import math
import random
from logging import getLogger
from typing import TYPE_CHECKING, Any, Optional, Union
from typing import TYPE_CHECKING, Any

import discord
from discord.app_commands import Group, command, describe
Expand Down
2 changes: 1 addition & 1 deletion kmibot/modules/ferry/modals.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import TYPE_CHECKING, Union
from typing import TYPE_CHECKING

import discord

Expand Down
17 changes: 12 additions & 5 deletions kmibot/modules/pub/commands.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from datetime import datetime, timedelta
from logging import getLogger
from typing import Optional

import discord
from discord.app_commands import Group, command
Expand Down Expand Up @@ -71,11 +70,11 @@ async def _create_pub_event(
pub: PubSchema,
start_time: datetime,
*,
user: str = "A user",
user: discord.User | discord.Member,
title: str = "Pub",
) -> discord.ScheduledEvent:
LOGGER.info(f"Creating scheduled event at {start_time}")
return await guild.create_scheduled_event(
scheduled_event = await guild.create_scheduled_event(
name=f"{pub.emoji} {title} {self.config.pub.supplemental_emoji}",
start_time=start_time,
end_time=start_time + timedelta(hours=3),
Expand All @@ -85,6 +84,14 @@ async def _create_pub_event(
description=self.config.pub.description,
reason=f"{user} used the /pub next command",
)
person = await self.api_client.get_person_for_discord_member(user)
await self.api_client.create_pub_event(
timestamp=start_time,
pub_id=pub.id,
created_by=person.id,
scheduled_event_id=scheduled_event.id,
)
return scheduled_event

@command(description="Select the pub for next week.") # type: ignore[arg-type]
async def next(self, interaction: discord.Interaction) -> None: # noqa: A003
Expand Down Expand Up @@ -113,7 +120,7 @@ async def next(self, interaction: discord.Interaction) -> None: # noqa: A003
interaction.guild,
pub,
pub_time,
user=interaction.user.name,
user=interaction.user,
)

LOGGER.info(f"Posting pub info in {pub_channel}")
Expand Down Expand Up @@ -149,7 +156,7 @@ async def now(self, interaction: discord.Interaction) -> None:
interaction.guild,
pub,
pub_time,
user=interaction.user.name,
user=interaction.user,
title="Spontaneous Pub",
)

Expand Down
1 change: 0 additions & 1 deletion kmibot/modules/pub/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import asyncio
from typing import Optional

import discord

Expand Down

0 comments on commit 5208b2b

Please sign in to comment.