Search code examples
pythondiscorddiscord.py

discord isnt recognising the roleid when it exists


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.")


it returns the role as None, saying discord doesnt recognise it.

And yes, i have checked the ids in the guild itself and its an existing role with the same ids, so im confused

anything im doing wrong?


Solution

  • it returns the role as None approaching based on this error the only thing I can think of is try to convert the role_id into an int before passing it into the get_role() function, because you maybe storing it as an str variable. Simply put role = int(role_id) then guild.get_role(role)