You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于discord.py rewrite实现verify验证指令 发送DM给管理员审核用户身份

Discord verify验证指令实现方案

原有代码问题

  • 缩进错误:get_prefix函数的return语句没有缩进至函数内部,会触发语法错误
  • 命令参数错误:verify命令不需要额外传入message参数,上下文对象ctx已经包含消息、发送者等全部相关属性
  • 用户获取逻辑错误:不能直接调用discord.Member.get_user获取指定管理员用户,应使用客户端实例的异步方法fetch_user获取用户对象
  • 异步方法调用错误:create_dm是异步方法,调用时必须加await,且新版本discord.py可直接给用户对象发DM,无需手动创建DM通道
  • 状态设置错误:get_prefix是函数,不能直接插值到状态文本中,需要对应处理逻辑

修正后可运行完整代码

import discord
from discord.ext import commands
import os
import json

token = os.environ['token']

# 读取前缀配置
def get_prefix(client, message):
    with open('prefixes.json', 'r') as f:
        prefixes = json.load(f)
    return prefixes[str(message.guild.id)]

# 配置机器人
intents = discord.Intents.default()
intents.members = True # 成员权限需要同时在Discord开发者门户开启
intents.message_content = True # 消息内容权限需要同时在Discord开发者门户开启
client = commands.Bot(command_prefix=get_prefix, intents=intents)

# 管理员ID,替换为你的账号ID
ADMIN_ID = 270397954773352469

@client.event
async def on_ready():
    print('Bot is ready')
    await client.change_presence(activity=discord.Game('输入!verify触发验证'))

# 验证触发指令
@client.command()
async def verify(ctx):
    applicant = ctx.author
    admin = await client.fetch_user(ADMIN_ID)
    await ctx.send('已提交验证申请,审核完成后你会收到私信通知', delete_after=10)
    await admin.send(f'用户 {applicant.mention} (ID: {applicant.id}) 提交了验证申请,是否通过?\n回复 !approve 用户ID 即可通过,回复 !deny 用户ID 即可拒绝')

# 审核通过指令,仅管理员可用
@client.command()
async def approve(ctx, user_id: int):
    if ctx.author.id != ADMIN_ID:
        return
    applicant = await client.fetch_user(user_id)
    await applicant.send('你的验证申请已通过')
    await ctx.send('已通知用户验证通过')

# 审核拒绝指令,仅管理员可用
@client.command()
async def deny(ctx, user_id: int):
    if ctx.author.id != ADMIN_ID:
        return
    applicant = await client.fetch_user(user_id)
    await applicant.send('你的验证申请未通过')
    await ctx.send('已通知用户验证未通过')

client.run(token)

额外注意事项

  • 确保prefixes.json文件提前创建,结构为键是服务器ID字符串、值是对应命令前缀的格式,示例:{"1234567890":"!"}
  • 如果验证通过后需要给用户分配指定身份组,可在approve指令中补充添加角色的相关逻辑

内容的提问来源于stack exchange,提问作者JJ Malovich

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.25 11:06:04