#Discord.py slash commands

1 messages · Page 1 of 1 (latest)

sly bone
#
@tree.command(name = "claim", description = "Provide our OrderID to claim your customer role!")
async def claim(interaction: discord.Interaction, OrderID: str):
    if "-" or "_" in interaction.content:
            # Extract the message after the command
            claimed_message = interaction.content
            MESSAGE = interaction.content
            # Check if the message is long enough
            if len(claimed_message) > 30:
                # Open the file in read mode
                with open('claimed_messages.txt', 'r') as f:
                    # Read each line of the file
                    for line in f:
                        # Check if the message is in the line
                        if claimed_message in line:
                            # Send a response to the user
                            await interaction.channel.send('This Order ID was already claimed before! If this is incorrect, open a support ticket.')
                            # Skip the rest of the code
                            return
                # If the message was not found, continue with the rest of the code
                # Create a cursor object
                cursor = conn.cursor()
                # Execute the SQL query
                cursor.execute("SELECT * FROM orders WHERE slug = %s AND paid = true", (MESSAGE,))
                # Fetch the results
                results = cursor.fetchall()
                if len(results) > 0:
                    # A matching row was found
                    # Give the user the role
                    role = discord.utils.get(interaction.guild.roles, name=ROLE_NAME)
                    await interaction.author.add_roles(role)
                    # Write the message and order ID to the file
                    with open('claimed_messages.txt', 'a') as f:
                        author_id = interaction.author.id
                        f.write(f"{claimed_message} , {author_id}\n")
                    await interaction.channel.send(f"You have claimed your customer role {interaction.author.mention}!")
                else:
                    # No matching row was found
                    await interaction.channel.send('Order ID not found or order has not been paid.')
            else:
                await interaction.channel.send('Invalid Order ID!')```

Not sure why its not popping up in my slash command list
#

@tree.command(name = "test", description = "testing", guild = discord.Object(id = GUILD))
async def self(interaction: discord.Interaction, name: str): 
    await interaction.response.send_message(f"Hello {name}! I was made with Discord.py", ephemeral = True)```

I have this in there too as a different slash command and thats showing up just fine
jovial shuttle
#

Are both commands in the same file?

jovial shuttle
#

I'm assuming somewhere in the file you have tree.run(...)?

sly bone
#
class aclient(discord.Client):
    def __init__(self):
        super().__init__(intents=discord.Intents.default())
        self.synced = False
        self.added = False

    async def on_ready(self):
        await self.wait_until_ready()
        if not self.synced:
            await tree.sync(guild = discord.Object(id = GUILD))
            self.synced = True
        if not self.added:
            self.add_view(ticket_launcher())
            self.add_view(main())
            self.added = True
        print(f"We have logged in as {self.user}.") 

client = aclient()
tree = app_commands.CommandTree(client)```
#

this is what else is in there

#

@jovial shuttle

jovial shuttle
#

Do you never call client.run(...)???

sly bone
#

sorry didint quite understand

jovial shuttle
#

Ok, is it after both commands?

sly bone
#

yes

jovial shuttle
#

Ok, so that's not the issue.

sly bone
#

is it maybe because I didint specify a guild in the nonworking command?

jovial shuttle
#

Worth a shot

#

to try adding one

sly bone
jovial shuttle
#

Just the numbers

sly bone
#

alr

sly bone
#

could it be that theyre still syncing? idk

#

@jovial shuttlenvm now back to the same problem where the test command works but not the other

jovial shuttle
#

I have no ideas for what could be broken.

sly bone
#

but now when running the command I recieve this error

jovial shuttle
#

What was wrong?

sly bone
#

but when using the command it shows the error I sent above

jovial shuttle
#

I think you need to do interaction.message.content

sly bone
#

ill try

#

honestly sounds like it would work tho

sly bone
#

seems to get a attributeerror

#

went ahead and changed it too

#

oops

#

forgot to change one

jovial shuttle
#

Not sure why interaction.message would be None.

sly bone
# jovial shuttle Not sure why `interaction.message` would be `None`.
@tree.command(name = "claim", description = "Provide OrderID to claim your customer role!", guild = discord.Object(id = GUILD))
async def claim(interaction: discord.Interaction, orderid: str):
    if "-" or "_" in interaction.message.content:
            # Extract the message after the command
            claimed_message = interaction.message.content
            MESSAGE = interaction.message.content
            # Check if the message is long enough
            if len(claimed_message) > 30:
                # Open the file in read mode
                with open('claimed_messages.txt', 'r') as f:
                    # Read each line of the file
                    for line in f:
                        # Check if the message is in the line
                        if claimed_message in line:
                            # Send a response to the user
                            await interaction.channel.send('This Order ID was already claimed before! If this is incorrect, open a support ticket.')
                            # Skip the rest of the code
                            return
                # If the message was not found, continue with the rest of the code
                # Create a cursor object
                cursor = conn.cursor()
                # Execute the SQL query
                cursor.execute("SELECT * FROM orders WHERE slug = %s AND paid = true", (MESSAGE,))
                # Fetch the results
                results = cursor.fetchall()
                if len(results) > 0:
                    # A matching row was found
                    # Give the user the role
                    role = discord.utils.get(interaction.guild.roles, name=ROLE_NAME)
                    await interaction.author.add_roles(role)
                    # Write the message and order ID to the file
                    with open('claimed_messages.txt', 'a') as f:
                        author_id = interaction.author.id
                        f.write(f"{claimed_message} , {author_id}\n")
                    await interaction.channel.send(f"You have claimed your customer role {interaction.author.mention}!")
                else:
                    # No matching row was found
                    await interaction.channel.send('Order ID not found or order has not been paid.')
            else:
                await interaction.channel.send('Invalid Order ID!')```

This is the updated code which I tried now and still getting attribute error
sly bone
jovial shuttle
#

It's not an issue with your code. For some reason interaction.message isn't being set by whatever Discord API wrapper you're using.

jovial shuttle
#

Oh make sure your bot has the messages intent. I don't think it's a default intent and you will need to enable it in the developer portal!

sly bone
#

its gotta be an issue with my code tho

#

since the first command works just fine its just that second long command thats having the issue

jovial shuttle
sly bone
# jovial shuttle Is it? Your code that you shared looks to be using the default intents and isn't...

well its working now here is the updated code with a new issue

@tree.command(name = "claim", description = "Provide our OrderID to claim your customer role!", guild = discord.Object(id = GUILD))
async def claim(interaction: discord.Interaction, orderid: str):
    MESSAGE = str(orderid)
    if isinstance(MESSAGE, str) and ( '-' in MESSAGE or '_' in MESSAGE):
        print("Its working so far")
        with open('claimed_messages.txt', 'r') as f:
                for line in f:
                    if MESSAGE in line:
                        await interaction.channel.send('This Order ID was already claimed before! If this is incorrect, open a support ticket.')
                        return
            # If the message was not found, continue with the rest of the code
        cursor = conn.cursor()
            # Execute the SQL query
        cursor.execute("SELECT * FROM orders WHERE slug = %s AND paid = true", (MESSAGE,))
            # Fetch the results
        results = cursor.fetchall()
        if len(results) > 0:
                # A matching row was found
                # Give the user the role
            role = discord.utils.get(interaction.guild.roles, name=ROLE_NAME)
            await interaction.author.add_roles(role)
                # Write the message and order ID to the file
            with open('claimed_messages.txt', 'a') as f:
                author_id = interaction.author.id
                f.write(f"{MESSAGE} , {author_id}\n")
            await interaction.channel.send(f"You have claimed your customer role {interaction.author.mention}!")
        else:
                # No matching row was found
            await interaction.channel.send('Order ID not found or order has not been paid.')
    else:
            await interaction.channel.send('Invalid Order ID!')```
jovial shuttle
sly bone
jovial shuttle
#

Use the user attribute.

#

Last one in the list

sly bone
#

its working now

#

any ideas how to fix this tho?

#

even though it worked, still says application did not respond

#

whoops

#

there we go thats fixed, had 'interaction.channel.send' instead interaction.response

sly bone
#

thank you for all the help @jovial shuttle you're awesome!

#

everything is working flawlessly