Skip to content

Commit

Permalink
Addition of trust cog
Browse files Browse the repository at this point in the history
  • Loading branch information
Xithrius committed Sep 12, 2023
1 parent 7aa1596 commit b6b5825
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
7 changes: 6 additions & 1 deletion api/app/routers/v1/trusted.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ async def create_trusted_user(trusted: Trusted) -> Trusted:
return await trusted.save()


@router.get("/{uid}", response_model=list[Trusted])
@router.get("/", response_model=list[Trusted])
async def get_all_trusted_users() -> list[Trusted]:
return await Trusted.objects.all()


@router.get("/{uid}", response_model=Trusted)
async def user_is_trusted(uid: int) -> Trusted:
is_trusted = await Trusted.objects.get(pk=uid)

Expand Down
53 changes: 53 additions & 0 deletions xythrion/bot/extensions/core/trusted.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
from dataclasses import dataclass
from datetime import datetime

from discord.ext.commands import Cog, group, is_owner

from bot.bot import Xythrion
from bot.context import Context


@dataclass
class TrustedData:
id: int
uid: int
at: datetime


class Trusted(Cog):
"""Adding trusted users for elevated commands."""

def __init__(self, bot: Xythrion) -> None:
self.bot = bot

@group(aliases=("trusted",))
@is_owner()
async def trust(self, ctx: Context) -> None:
"""Trust group command."""
if ctx.invoked_subcommand is None:
await ctx.send("Missing subcommand")

@trust.command(aliases=("list",))
@is_owner()
async def list_trusted(self, ctx: Context) -> None:
data: list[TrustedData] = await self.bot.api.get("/v1/trusted/")

await ctx.send(data)

@trust.command()
@is_owner()
async def add_trust(self, ctx: Context, user_id: int) -> None:
data: TrustedData = await self.bot.api.post(f"/v1/trusted/{user_id}")

await ctx.send(f"Trust given to <@{user_id}> at {data.at}")

@trust.command()
@is_owner()
async def remove_trust(self, ctx: Context, user_id: int) -> None:
await self.bot.api.delete(f"/v1/trusted/{user_id}")

await ctx.send(f"Trust removed from <@{user_id}>")


async def setup(bot: Xythrion) -> None:
await bot.add_cog(Trusted(bot))

0 comments on commit b6b5825

Please sign in to comment.