v2 refactor to label-based koans, and add discord
This commit is contained in:
@@ -7,13 +7,14 @@ from meta import Bot, Context
|
||||
|
||||
from . import logger
|
||||
from ..koans import KoanRegistry
|
||||
from ..data import KoanData
|
||||
from ..data import KoansData
|
||||
from ..lib import minify
|
||||
|
||||
|
||||
class KoanComponent(cmds.Component):
|
||||
def __init__(self, bot: Bot):
|
||||
self.bot = bot
|
||||
self.data = bot.dbconn.load_registry(KoanData())
|
||||
self.data = bot.dbconn.load_registry(KoansData())
|
||||
self.koans = KoanRegistry(self.data)
|
||||
|
||||
async def component_load(self):
|
||||
@@ -25,6 +26,17 @@ class KoanComponent(cmds.Component):
|
||||
async def event_message(self, payload: twitchio.ChatMessage) -> None:
|
||||
print(f"[{payload.broadcaster.name}] - {payload.chatter.name}: {payload.text}")
|
||||
|
||||
async def component_command_error(self, payload):
|
||||
try:
|
||||
raise payload.exception
|
||||
except cmds.ArgumentError:
|
||||
if cmd := payload.context.command:
|
||||
usage = f"{cmd.qualified_name} {cmd.signature}"
|
||||
await payload.context.reply(f"USAGE: {usage}")
|
||||
return False
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
@cmds.group(invoke_fallback=True)
|
||||
async def koans(self, ctx: cmds.Context) -> None:
|
||||
"""
|
||||
@@ -38,74 +50,100 @@ class KoanComponent(cmds.Component):
|
||||
koans = await self.koans.get_community_koans(cid)
|
||||
|
||||
if koans:
|
||||
names = ', '.join(koan.name for koan in koans)
|
||||
await ctx.reply(
|
||||
f"Koans: {names}"
|
||||
)
|
||||
count = len(koans)
|
||||
parts = []
|
||||
for koan in koans[-10:]:
|
||||
minified = minify(koan.content, 20)
|
||||
formatted = f"#{koan.koanlabel}: {minified}"
|
||||
parts.append(formatted)
|
||||
partstr = '; '.join(parts)
|
||||
laststr = "Last 10: " if count > 10 else ""
|
||||
message = f"We have {count} koans! {laststr}{partstr}"
|
||||
await ctx.reply(message)
|
||||
else:
|
||||
await ctx.reply("No koans have been made in this channel!")
|
||||
|
||||
@koans.command(name='add', aliases=['new', 'create'])
|
||||
@cmds.is_moderator()
|
||||
async def koans_add(self, ctx: cmds.Context, name: str, *, text: str):
|
||||
async def koans_add(self, ctx: cmds.Context, *, content: str):
|
||||
"""
|
||||
Add or overwrite a koan to this channel.
|
||||
|
||||
!koans add wind This is a wind koan
|
||||
!koans add This is a wind koan
|
||||
"""
|
||||
community = await self.bot.profiles.fetch_community(ctx.broadcaster)
|
||||
cid = community.communityid
|
||||
profile = await self.bot.profiles.fetch_profile(ctx.chatter)
|
||||
pid = profile.profileid
|
||||
|
||||
name = name.lower()
|
||||
existing = await self.koans.get_koan_named(cid, name)
|
||||
await self.koans.create_koan(cid, name, text)
|
||||
koan = await self.koans.create_koan(
|
||||
cid,
|
||||
content,
|
||||
created_by=pid,
|
||||
)
|
||||
|
||||
if existing:
|
||||
await ctx.reply(f"Updated the koan '{name}'")
|
||||
await ctx.reply(f"Koan #{koan.koanlabel} created!")
|
||||
|
||||
# TODO: Error message on signature failure
|
||||
|
||||
@koans.command(name='edit', aliases=['update',])
|
||||
@cmds.is_moderator()
|
||||
async def koans_edit(self, ctx: cmds.Context, label: int, *, new_content: str):
|
||||
community = await self.bot.profiles.fetch_community(ctx.broadcaster)
|
||||
cid = community.communityid
|
||||
|
||||
koan = await self.koans.get_koan_label(cid, label)
|
||||
|
||||
if koan:
|
||||
await self.data.koans.update_where(koanid=koan.koanid).set(content=new_content)
|
||||
await ctx.reply(f"Updated koan #{label}")
|
||||
else:
|
||||
await ctx.reply(f"Created the new koan '{name}'")
|
||||
await ctx.reply(f"Koan #{label}' does not exist to delete!")
|
||||
|
||||
|
||||
@koans.command(name='del', aliases=['delete', 'rm', 'remove'])
|
||||
@cmds.is_moderator()
|
||||
async def koans_del(self, ctx: cmds.Context, name: str):
|
||||
async def koans_del(self, ctx: cmds.Context, label: int):
|
||||
"""
|
||||
Remove a koan from this channel by name.
|
||||
Remove a koan from this channel by number.
|
||||
|
||||
!koans del wind
|
||||
!koans del 5
|
||||
"""
|
||||
community = await self.bot.profiles.fetch_community(ctx.broadcaster)
|
||||
cid = community.communityid
|
||||
|
||||
name = name.lower()
|
||||
koan = await self.koans.get_koan_named(cid, name)
|
||||
koan = await self.koans.get_koan_label(cid, label)
|
||||
|
||||
if koan:
|
||||
await koan.delete()
|
||||
await ctx.reply(f"Deleted the koan '{name}'")
|
||||
await ctx.reply(f"Deleted koan #{label}")
|
||||
else:
|
||||
await ctx.reply(f"The koan '{name}' does not exist to delete!")
|
||||
await ctx.reply(f"Koan #{label}' does not exist to delete!")
|
||||
|
||||
@cmds.command(name='koan')
|
||||
async def koan(self, ctx: cmds.Context, name: Optional[str] = None):
|
||||
async def koan(self, ctx: cmds.Context, label: Optional[int] = None):
|
||||
"""
|
||||
Show a koan from this channel. Optionally by name.
|
||||
Show a koan from this channel. Optionally by number.
|
||||
|
||||
!koan
|
||||
!koan wind
|
||||
!koan 5
|
||||
"""
|
||||
community = await self.bot.profiles.fetch_community(ctx.broadcaster)
|
||||
cid = community.communityid
|
||||
|
||||
if name is not None:
|
||||
name = name.lower()
|
||||
koan = await self.koans.get_koan_named(cid, name)
|
||||
if label is not None:
|
||||
koan = await self.koans.get_koan_label(cid, label)
|
||||
if koan:
|
||||
await ctx.reply(koan.message)
|
||||
else:
|
||||
await ctx.reply(f"The requested koan '{name}' does not exist! Use '{ctx.prefix}koans' to see all the koans.")
|
||||
await ctx.reply(f"Koan #{label} does not exist!")
|
||||
else:
|
||||
koans = await self.koan.get_community_koans(communityid=cid)
|
||||
if koans:
|
||||
koan = random.choice(koans)
|
||||
await ctx.reply(koan.message)
|
||||
formatted = f"Koan #{koan.koanlabel}: {koan.message}"
|
||||
parts = [formatted[i: i:500] for i in range(0, len(formatted), 500)]
|
||||
for part in parts:
|
||||
await ctx.reply(part)
|
||||
else:
|
||||
await ctx.reply("This channel doesn't have any koans!")
|
||||
|
||||
Reference in New Issue
Block a user