权限管理:
- 主人权限:最高权限,可以管理所有功能。
- 管理权限:可以执行踢人、禁言等操作。
指令功能:
- 踢出指定成员。
- 禁言指定成员。
- 解除禁言指定成员。
对接 DeepSeek AI:
- 提供 AI 对话功能。
功能开关:
- 仅管理员权限以上的用户才能开启或关闭 AI 功能。
1. 环境准备
确保你已经安装以下工具:
- Python 3.8+
nonebot2
框架go-cqhttp
(用于与 QQ 服务器通信)
安装依赖:
pip install nonebot2 nonebot-adapter-cqhttp requests
2. 创建项目
创建一个新的 nonebot2
项目:
mkdir qq_bot
cd qq_bot
nb create
选择 simple
模板。
3. 编写机器人代码
在 bot.py
中编写以下代码:
from nonebot import on_notice, on_message, on_command
from nonebot.adapters.cqhttp import Bot, GroupMessageEvent, GroupIncreaseNoticeEvent
from nonebot.rule import to_me
from nonebot.typing import T_State
from nonebot.matcher import Matcher
import re
import requests
# 定义主人和管理员
MASTER_ID = 123456789 # 主人的QQ号
ADMIN_IDS = [987654321, 112233445] # 管理员的QQ号列表
def is_master(event: GroupMessageEvent) -> bool:
"""检查发送者是否是主人"""
return event.user_id == MASTER_ID
def is_admin(event: GroupMessageEvent) -> bool:
"""检查发送者是否是管理员"""
return event.user_id in ADMIN_IDS
# 自动欢迎新成员
welcome = on_notice(rule=lambda event: isinstance(event, GroupIncreaseNoticeEvent))
@welcome.handle()
async def handle_welcome(bot: Bot, event: GroupIncreaseNoticeEvent):
user_id = event.user_id
group_id = event.group_id
await bot.send_group_msg(group_id=group_id, message=f"欢迎新成员[CQ:at,qq={user_id}]加入本群!")
# 踢出成员指令
kick = on_command("kick", rule=to_me())
@kick.handle()
async def handle_kick(bot: Bot, event: GroupMessageEvent):
if not is_master(event) and not is_admin(event):
await kick.finish("你没有权限执行此操作!")
# 解析被踢成员的QQ号
msg = event.get_plaintext().strip()
match = re.search(r"kick (\d+)", msg)
if not match:
await kick.finish("用法:kick @QQ号")
target_id = int(match.group(1))
await bot.set_group_kick(group_id=event.group_id, user_id=target_id)
await kick.finish(f"已踢出成员[CQ:at,qq={target_id}]。")
# 禁言成员指令
ban = on_command("ban", rule=to_me())
@ban.handle()
async def handle_ban(bot: Bot, event: GroupMessageEvent):
if not is_master(event) and not is_admin(event):
await ban.finish("你没有权限执行此操作!")
# 解析被禁言成员的QQ号和禁言时间
msg = event.get_plaintext().strip()
match = re.search(r"ban (\d+) (\d+)", msg)
if not match:
await ban.finish("用法:ban @QQ号 禁言时间(秒)")
target_id = int(match.group(1))
duration = int(match.group(2))
await bot.set_group_ban(group_id=event.group_id, user_id=target_id, duration=duration)
await ban.finish(f"已禁言成员[CQ:at,qq={target_id}],时长 {duration} 秒。")
# 解除禁言指令
unban = on_command("unban", rule=to_me())
@unban.handle()
async def handle_unban(bot: Bot, event: GroupMessageEvent):
if not is_master(event) and not is_admin(event):
await unban.finish("你没有权限执行此操作!")
# 解析被解除禁言成员的QQ号
msg = event.get_plaintext().strip()
match = re.search(r"unban (\d+)", msg)
if not match:
await unban.finish("用法:unban @QQ号")
target_id = int(match.group(1))
await bot.set_group_ban(group_id=event.group_id, user_id=target_id, duration=0)
await unban.finish(f"已解除成员[CQ:at,qq={target_id}]的禁言。")
# DeepSeek API 配置
DEEPSEEK_API_URL = "https://api.deepseek.com/v1/chat"
DEEPSEEK_API_KEY = "your_api_key" # 替换为你的 DeepSeek API 密钥
# AI 功能开关
ai_enabled = False
# 开启/关闭 AI 功能
ai_switch = on_command("ai_switch", rule=to_me())
@ai_switch.handle()
async def handle_ai_switch(bot: Bot, event: GroupMessageEvent):
global ai_enabled
if not is_master(event) and not is_admin(event):
await ai_switch.finish("你没有权限执行此操作!")
# 解析指令
msg = event.get_plaintext().strip()
if msg == "ai_switch on":
ai_enabled = True
await ai_switch.finish("AI 功能已开启。")
elif msg == "ai_switch off":
ai_enabled = False
await ai_switch.finish("AI 功能已关闭。")
else:
await ai_switch.finish("用法:ai_switch on/off")
# AI 对话功能
ai_chat = on_message()
@ai_chat.handle()
async def handle_ai_chat(bot: Bot, event: GroupMessageEvent):
global ai_enabled
if not ai_enabled:
return
# 获取用户发送的消息
user_message = event.get_plaintext().strip()
# 调用 DeepSeek API
response = await get_deepseek_response(user_message)
# 发送 AI 的回复
await ai_chat.finish(response)
async def get_deepseek_response(message: str) -> str:
"""调用 DeepSeek API 获取 AI 回复"""
headers = {
"Authorization": f"Bearer {DEEPSEEK_API_KEY}",
"Content-Type": "application/json"
}
data = {
"message": message,
"context": "" # 如果需要上下文,可以在这里添加
}
try:
response = requests.post(DEEPSEEK_API_URL, headers=headers, json=data)
response.raise_for_status() # 检查请求是否成功
result = response.json()
return result.get("response", "抱歉,我暂时无法回答这个问题。")
except Exception as e:
return f"调用 DeepSeek API 出错:{str(e)}"
4. 配置 .env
文件
在项目根目录下创建或编辑 .env
文件,配置 go-cqhttp
的地址和端口:
HOST=127.0.0.1
PORT=8080
5. 运行机器人
- 启动
go-cqhttp
并登录你的 QQ 账号。 运行
nonebot2
:nb run
6. 功能测试
权限管理:
- 测试主人和管理员是否能执行踢人、禁言等操作。
- 测试普通用户是否无法执行这些操作。
AI 功能开关:
- 使用
ai_switch on
开启 AI 功能。 - 使用
ai_switch off
关闭 AI 功能。 - 测试普通用户是否无法操作开关。
- 使用
AI 对话:
- 开启 AI 功能后,向机器人发送消息,测试 AI 回复功能。
踢出成员:
- 使用
kick @QQ号
指令踢出成员。
- 使用
禁言成员:
- 使用
ban @QQ号 时间
指令禁言成员。
- 使用
解除禁言:
- 使用
unban @QQ号
指令解除禁言。
- 使用
7. 部署
将你的机器人部署到服务器上,使用 supervisor
或 systemd
管理进程,确保 24 小时运行。
参考文档
如果有任何问题,欢迎随时提问!