二:在群里运行
main.py
from mirai import Mirai, WebSocketAdapter, FriendMessage, GroupMessage, At
if __name__ == '__main__':
bot = Mirai(
qq=12345678, # 改成你的机器人的 QQ 号
adapter=WebSocketAdapter(
verify_key='yirimirai', host='localhost', port=8080
)
)
@bot.on(FriendMessage)
def on_friend_message(event: FriendMessage):
if str(event.message_chain) == '你好':
return bot.send(event, 'Hello, World!')
@bot.on(GroupMessage)
def on_group_message(event: GroupMessage):
if At(bot.qq) in event.message_chain:
return bot.send(event, [At(event.sender.id), '你在叫我吗?'])
@bot.on(GroupMessage)
def on_group_message_new(event: GroupMessage):
"""发挥你的创意……"""
bot.run()