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

Discord.py rstaff命令报错求助:self参数缺失及Member找不到问题

问题排查与修复方案

错误1:self is a required argument that is missing.

原因:混淆了前缀式命令和Cog类命令的写法。用@client.command()装饰的普通函数不需要self参数,只有将命令写在discord.ext.commands.Cog子类中时才需要self。

错误2:Member<@&777487631889072138> not found.

原因:<@&ID>是角色的提及格式,说明执行命令时把角色参数放在了成员参数的位置,导致机器人尝试将角色识别为成员,无法找到对应对象。同时原代码中获取频道的方式也存在错误。

错误3:on_ready事件代码错误

原因:@client.event装饰的事件函数不需要self参数,直接使用client对象即可,且代码中client.server_id未定义,会引发新的错误。


修复后的完整代码

import discord
from discord.ext import commands

# 初始化客户端,确保intents正确配置
intents = discord.Intents.default()
intents.members = True  # 必须开启成员意图才能正确获取成员对象
client = commands.Bot(command_prefix="你的命令前缀", intents=intents)

# 正确的on_ready事件
@client.event
async def on_ready():
    print(f'已登录为 {client.user}')
    # 若需全局存储服务器对象,直接用client获取
    # client.server = client.get_guild(你的服务器ID)  # 替换为实际服务器ID

# 修复后的rstaff命令
@client.command()
@commands.has_permissions(manage_roles=True)
async def rstaff(ctx, member: discord.Member, role: discord.Role):
    admin = ctx.author
    # 直接用client获取频道,无需self
    channel = client.get_channel(1080557591899406366)
    
    # 日志嵌入消息
    log_embed = discord.Embed(
        title="✅管理员操作日志",
        description=f"**执行移除角色命令**\n管理员:{admin.mention}\n目标用户:{member.name}\n移除的角色:{role.mention}",
        colour=discord.Colour.green()
    )
    # 通知嵌入消息
    notify_embed = discord.Embed(
        title="✅操作完成",
        description=f"{member.mention} 已被移除 {role.mention} 权限。执行管理员:{admin.mention}",
        colour=discord.Colour.green()
    )
    
    await member.remove_roles(role)
    await ctx.send(embed=notify_embed)
    await channel.send(embed=log_embed)

# 替换为你的机器人Token
client.run("你的机器人Token")

额外注意事项

  1. Intents配置:必须在Discord开发者门户开启Server Members Intent,并在代码中启用intents.members = True,否则无法正确获取成员对象。
  2. 命令执行格式:确保执行命令时参数顺序正确:你的命令前缀rstaff @成员 @角色,不要颠倒成员和角色的位置。
  3. 权限检查:确保执行命令的用户拥有管理角色权限,且机器人的角色位置要高于目标角色,否则无法移除角色。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.24 21:45:31