Skip to content

Instantly share code, notes, and snippets.

@TWT233
Created December 23, 2021 15:16
Show Gist options
  • Save TWT233/27142a6c0a53eb6ef1e2af5b3b5b83a8 to your computer and use it in GitHub Desktop.
Save TWT233/27142a6c0a53eb6ef1e2af5b3b5b83a8 to your computer and use it in GitHub Desktop.
from khl import Bot, Message
from khl.card import CardMessage, Card, Module, Element, ClickTypes
from khl.command import Command
bot1 = Bot('xxxxxx') # 不再需要 token=
bot2 = Bot('xxxxxx') # async 版的 Bot.run,支持一个文件运行多个 Bot
@bot1.task.add_interval(seconds=2)
async def go():
print(f'新增task要用bot.task.add_*()了')
@Command.command()
async def msg_and_bot(msg: Message, b: Bot, name: str):
await b.send(msg.ctx.channel, f'hey {name}, 现在命令的处理函数可以接受Bot了!')
await msg.reply('也就是说可以分开command的声明和注册')
await b.send(await b.fetch_public_channel('我的小圈圈'), f'也可以使用Bot中的接口')
bot1.command.add(msg_and_bot) # 这里是注册,可以将command的声明放到独立文件中了
@bot2.command()
async def new_card(msg: Message):
cm = CardMessage(Card(Module.Header('全新的card_message API'), color='#D26676'))
c2 = Card(Module.Header('从 from khl.card import CardMessage, Card, Module, Element 开始'))
c2.append(Module.Section('试试看!', Element.Button('try!', ClickTypes.LINK, 'https://kaiheila.cn/app/')))
cm.append(c2)
await msg.reply(cm)
bot1.schedule() # 启动Bot并放到后台
bot2.run() # 以往版本的阻塞型启动仍然可用
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment