import discord
from discord import app_commands
import json

intents = discord.Intents().all()

client = discord.Client(intents=intents)

tree = app_commands.CommandTree(client)

@tree.command(name="subscribe")
async def the_command(interaction, itemid: str):
    discid = int(interaction.user.id)
    print(discid)

    with open("data.json", "r") as file:
        data = json.load(file)

    if itemid in data:
        role_id = data[itemid]["roleid"]
        print(role_id)

        guild = interaction.guild
        print(guild)
        member = await guild.fetch_member(discid)
        print(member)

        if member is not None:
            role = guild.get_role(role_id)

            if role is not None:
                await member.add_roles(role)
                await interaction.response.send_message(f"Role assigned for item ID: {itemid}")

            else:
                await interaction.response.send_message(f"Role for item ID {itemid} not found in the guild.")
        else:
            await interaction.response.send_message("You are not a member of this guild.")
    else:
        await interaction.response.send_message(f"Item ID {itemid} not found in the data.")


它返回的角色是NONE,说discord不承认它.

是的,我已经判断了unions 本身的ID,它是一个具有相同ID的现有角色,所以我很困惑

我做错什么了吗?

推荐答案

基于这个错误,我唯一能想到的就是在将角色id传递给get_role()函数之前,try 将它转换为int,因为您可能会将其存储为str变量. 简单地说,role = int(role_id)然后guild.get_role(role)

Python相关问答推荐

实现的差异取决于计算出的表达是直接返回还是首先存储在变量中然后返回

2维数组9x9,不使用numpy.数组(MutableSequence的子类)

大Pandas 胚胎中产生组合

使用FASTCGI在IIS上运行Django频道

Deliveryter Notebook -无法在for循环中更新matplotlib情节(保留之前的情节),也无法使用动画子功能对情节进行动画

连接两个具有不同标题的收件箱

SQLGory-file包FilField不允许提供自定义文件名,自动将文件保存为未命名

在Python中管理打开对话框

Pandas DataFrame中行之间的差异

Odoo 16使用NTFS使字段只读

如何从需要点击/切换的网页中提取表格?

如何使regex代码只适用于空的目标单元格

try 检索blob名称列表时出现错误填充错误""

如何在达到end_time时自动将状态字段从1更改为0

使用Python从rotowire中抓取MLB每日阵容

手动设置seborn/matplotlib散点图连续变量图例中显示的值

python sklearn ValueError:使用序列设置数组元素

Polars map_使用多处理对UDF进行批处理

计算机找不到已安装的库'

如何训练每一个pandaprame行的线性回归并生成斜率