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

拥有正确权限的Discord Bot无法分配角色问题求助

Discord验证Bot权限异常排查(Pycord v2.3)

遇到的异常:

discord.errors.Forbidden: 403 Forbidden (error code: 50013): Missing Permissions

Bot已通过管理员权限邀请至服务器,可执行修改成员昵称等管理员操作,但无法为非所有者/管理员的普通成员编辑或分配角色,触发上述权限错误。奇怪的是,手动以Bot角色视角查看服务器时,角色编辑功能是可用的,但Bot自身执行代码时却失败。

Bot源代码

from pathlib import Path
import hashlib
import discord
from discord.ext import commands


class EmployeeDatabase:
    def __init__(self):
        self.filename = "employee_database.dsv"
        path = Path(self.filename)
        if not path.is_file():
            open(self.filename, "x").close()

    def hash(self, value) -> str:
        return hashlib.md5(value.encode("utf-8")).hexdigest()

    def is_duplicate(self, code: str) -> bool:
        with open(self.filename, "r") as database:
            if self.hash(value=code) in [i.rstrip() for i in database.readlines()]:
                return True
            else:
                return False

    def read_all(self) -> list[str]:
        with open(self.filename, "r") as database:
            employee_code_hashes = database.readlines()
        return employee_code_hashes

    def insert(self, code) -> None:
        new_hash = self.hash(value=code)
        print(new_hash)
        with open(self.filename, "r+") as database:
            hashes = [i.rstrip() for i in database.readlines()]
            print(hashes)
            if new_hash in hashes:
                raise Exception("Attempted to insert a duplicate hash")
            else:
                if not hashes:
                    database.write(str(new_hash))
                else:
                    database.write(f"\n{new_hash}")

    def verify_employee(self, code: str) -> int:
        if self.is_duplicate(code=code):
            return 1
        elif len(code) != 6:
            return 0
        elif not code.isdigit():
            return 0
        else:
            self.insert(code=code)
            return 2


class MyModal(discord.ui.Modal):
    def __init__(self) -> None:
        super().__init__(
            discord.ui.InputText(
                label="Name",
                placeholder="First and last name",
                custom_id="name_input"
            ),
            discord.ui.InputText(
                label="Employee Code",
                placeholder="Code",
                custom_id="code_input"
            ),
            title="Employee Verification"
        )

    async def callback(self, interaction: discord.Interaction):
        child_values = {child.custom_id: child.value for child in self.children}
        name_input = child_values["name_input"].split(" ")
        full_name = f"{name_input[0].capitalize()} {name_input[1].capitalize()[0]}."
        employee_code = child_values["code_input"]
        
        verified_role = discord.utils.get(interaction.guild.roles, name="Verified")

        await interaction.user.edit(nick=full_name, roles=[verified_role])
        await interaction.response.send_message("Verified")



BOT_TOKEN = "bot token here"
intents = discord.Intents.all()
client = commands.Bot(intents=intents)
server_ids = [server ids here]


@client.event
async def on_ready() -> None:
    print("Bot initialized")


@client.event
async def on_member_join(member) -> None:
    pass


@client.slash_command(guild_ids=server_ids, name="ping", description="Pings bot")
async def ping(context):
    await context.respond(f"{round(client.latency * 1000)}ms ping")


@client.slash_command(guild_ids=server_ids, name="verify")
async def verify(context):
    modal = MyModal()
    await context.send_modal(modal=modal)


if __name__ == "__main__":
    client.run(BOT_TOKEN)

已尝试的修复操作

  • 确认Bot分配的是服务器内的有效角色,将目标角色与服务器可分配角色列表做过比对
  • 尝试过member.add_roles()等不同的角色编辑方法,确认问题属于权限范畴,而非Pycord模块本身的问题

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.02 19:20:41