-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.py
48 lines (38 loc) · 1.45 KB
/
script.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from khl import Bot, Message
from kook_chatbots.utils import read_config
from kook_chatbots.api_ import ai_reply,CommandManager,print_msg_info,clear_chat_history
import asyncio
# init Bot
testBot_token = read_config()['testBot']
bot = Bot(token=testBot_token)
# command magager
command_manager = CommandManager()
command_manager.register_command('hello',lambda :'world','send a hello world sentence')
command_manager.register_command('clearhistory',clear_chat_history,'clear dialogue history of the chatbot')
# register command, send `/hello` in channel to invoke
# @bot.command(name='hello')
# async def world(msg: Message):
# task1 = asyncio.create_task(print_msg_info(msg))
# # await print_msg_info(msg)
# await msg.reply('world!')
# await task1
@bot.on_message()
async def auto_reply(msg: Message):
print_msg_info(msg)
msg_content = msg.content
# command
if msg_content.startswith('/'):
response = command_manager.parse_command_and_exec(msg_content)
if response is not None:
await msg.reply(response)
else:
await msg.reply('Done!')
# normal text
else:
if msg.ctx.channel.id == '5025001924381672': #机器人聊天频道
response = ai_reply(msg_content)
await msg.reply(response)
# everything done, go ahead now!
bot.run()
# now invite the bot to a server, and send '/hello' in any channel
# (remember to grant the bot with read & send permissions)