112 lines
3.6 KiB
Python
112 lines
3.6 KiB
Python
from typing import Optional
|
|
import random
|
|
import twitchio
|
|
from twitchio.ext import commands as cmds
|
|
|
|
from meta import Bot, Context
|
|
|
|
from . import logger
|
|
from ..koans import KoanRegistry
|
|
from ..data import KoanData
|
|
|
|
|
|
class KoanComponent(cmds.Component):
|
|
def __init__(self, bot: Bot):
|
|
self.bot = bot
|
|
self.data = bot.dbconn.load_registry(KoanData())
|
|
self.koans = KoanRegistry(self.data)
|
|
|
|
async def component_load(self):
|
|
await self.data.init()
|
|
await self.bot.version_check(*self.data.VERSION)
|
|
await self.koans.init()
|
|
|
|
@cmds.Component.listener()
|
|
async def event_message(self, payload: twitchio.ChatMessage) -> None:
|
|
print(f"[{payload.broadcaster.name}] - {payload.chatter.name}: {payload.text}")
|
|
|
|
@cmds.group(invoke_fallback=True)
|
|
async def koans(self, ctx: cmds.Context) -> None:
|
|
"""
|
|
List the (names of the) koans in this channel.
|
|
|
|
!koans
|
|
"""
|
|
community = await self.bot.profiles.fetch_community(ctx.broadcaster)
|
|
cid = community.communityid
|
|
|
|
koans = await self.koans.get_community_koans(cid)
|
|
|
|
if koans:
|
|
names = ', '.join(koan.name for koan in koans)
|
|
await ctx.reply(
|
|
f"Koans: {names}"
|
|
)
|
|
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):
|
|
"""
|
|
Add or overwrite a koan to this channel.
|
|
|
|
!koans add wind This is a wind koan
|
|
"""
|
|
community = await self.bot.profiles.fetch_community(ctx.broadcaster)
|
|
cid = community.communityid
|
|
|
|
name = name.lower()
|
|
existing = await self.koans.get_koan_named(cid, name)
|
|
await self.koans.create_koan(cid, name, text)
|
|
|
|
if existing:
|
|
await ctx.reply(f"Updated the koan '{name}'")
|
|
else:
|
|
await ctx.reply(f"Created the new koan '{name}'")
|
|
|
|
@koans.command(name='del', aliases=['delete', 'rm', 'remove'])
|
|
@cmds.is_moderator()
|
|
async def koans_del(self, ctx: cmds.Context, name: str):
|
|
"""
|
|
Remove a koan from this channel by name.
|
|
|
|
!koans del wind
|
|
"""
|
|
community = await self.bot.profiles.fetch_community(ctx.broadcaster)
|
|
cid = community.communityid
|
|
|
|
name = name.lower()
|
|
koan = await self.koans.get_koan_named(cid, name)
|
|
if koan:
|
|
await koan.delete()
|
|
await ctx.reply(f"Deleted the koan '{name}'")
|
|
else:
|
|
await ctx.reply(f"The koan '{name}' does not exist to delete!")
|
|
|
|
@cmds.command(name='koan')
|
|
async def koan(self, ctx: cmds.Context, name: Optional[str] = None):
|
|
"""
|
|
Show a koan from this channel. Optionally by name.
|
|
|
|
!koan
|
|
!koan wind
|
|
"""
|
|
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 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.")
|
|
else:
|
|
koans = await self.koan.get_community_koans(communityid=cid)
|
|
if koans:
|
|
koan = random.choice(koans)
|
|
await ctx.reply(koan.message)
|
|
else:
|
|
await ctx.reply("This channel doesn't have any koans!")
|