bot utils
This commit is contained in:
parent
70cabd69bb
commit
57919e6506
18
v6d2ctx/handle_args.py
Normal file
18
v6d2ctx/handle_args.py
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
import discord
|
||||||
|
|
||||||
|
from v6d2ctx.context import Context, Implicit, Explicit
|
||||||
|
from v6d2ctx.handle_command import handle_command
|
||||||
|
|
||||||
|
|
||||||
|
async def handle_args(message: discord.Message, args: list[str]):
|
||||||
|
match args:
|
||||||
|
case []:
|
||||||
|
return
|
||||||
|
case [command_name, *command_args]:
|
||||||
|
ctx = Context(message)
|
||||||
|
try:
|
||||||
|
await handle_command(ctx, command_name, command_args)
|
||||||
|
except Implicit:
|
||||||
|
pass
|
||||||
|
except Explicit as e:
|
||||||
|
await ctx.reply(e.msg)
|
5
v6d2ctx/handle_command.py
Normal file
5
v6d2ctx/handle_command.py
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
from v6d2ctx.context import Context, of
|
||||||
|
|
||||||
|
|
||||||
|
async def handle_command(ctx: Context, name: str, args: list[str]) -> None:
|
||||||
|
await of('commands', name)(ctx, args)
|
15
v6d2ctx/handle_content.py
Normal file
15
v6d2ctx/handle_content.py
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
import shlex
|
||||||
|
|
||||||
|
import discord
|
||||||
|
|
||||||
|
from v6d2ctx.handle_args import handle_args
|
||||||
|
|
||||||
|
|
||||||
|
async def handle_content(message: discord.Message, content: str, prefix: str):
|
||||||
|
if message.author.bot:
|
||||||
|
return
|
||||||
|
if not content.startswith(prefix):
|
||||||
|
return
|
||||||
|
content = content.removeprefix(prefix)
|
||||||
|
args = shlex.split(content)
|
||||||
|
await handle_args(message, args)
|
15
v6d2ctx/lock_for.py
Normal file
15
v6d2ctx/lock_for.py
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
import asyncio
|
||||||
|
from typing import Hashable
|
||||||
|
|
||||||
|
from v6d2ctx.context import Explicit
|
||||||
|
|
||||||
|
locks: dict[Hashable, asyncio.Lock] = {}
|
||||||
|
|
||||||
|
|
||||||
|
def lock_for(key: Hashable, error_message: str) -> asyncio.Lock:
|
||||||
|
if key is None:
|
||||||
|
raise Explicit(error_message)
|
||||||
|
if key in locks:
|
||||||
|
return locks[key]
|
||||||
|
else:
|
||||||
|
return locks.setdefault(key, asyncio.Lock())
|
Loading…
Reference in New Issue
Block a user