Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading