Can't respond to Interaction after using interaction.response.defer() [Nextcord] - discord

I have a slash command, but it requires few seconds for processing. Since Discord limitation for responding to interactions seems to be 3s, i found on the documentation the interaction.response.defer() method, that should tell Discord that i've recieved the command (not to throw the error "Interaction didn't responded.")
#client.slash_command(description="Test command", guild_ids=[123456789123456789])
async def test(interaction: nextcord.Interaction):
await interaction.response.defer()
await asyncio.sleep(10) # Doing stuff
await interaction.response.send_message("My actual content")
But I get this error :
nextcord.errors.InteractionResponded: This interaction has already been responded to before
What am I doing wrong ?

interaction.followup.send() should be used instead of send_message()

Related

Is there a way to have multiple arguments in a command with discord py

I’m creating a discord bot using discord py and would like to have a kick command that dms the user a reason upon being kicked.
-kick #user reason
When I kick the user with a reason attached it doesn’t kick the user and I get an error in console saying can not find the user
Here is the code
#client.command(aliases=['Kick'])
#commands.has_permissions(administrator=True)
async def kick(ctx,member : discord.Member,*,reason= 'no reason'):
await ctx.send(f'{member.name} was kicked from the Server!\nand a Dm was sendet by me as a Information for him')
await asyncio.sleep(5)
await ctx.channel.purge(limit=2)
await member.send(f'**You was kicked from {ctx.guild.name}\nthe Stuff Team dont tell me the reason?**')
await member.kick(reason=reason)
print(f"{ctx.author} ----> just used {prefix}kick")
And yes I have tried Google, the discord py API guide with no luck
Can anyone help? Thanks
You can give a nice error message. It raises MemberNotFound.
Then you can make a local error handler.
#kick.error
async def kick_command_error(ctx, err):
if isinstance(err, commands.MemberNotFound):
await ctx.send('Hey! The member you gave is invalid or was not found. Please try again by `#`mentioning them or using the ID.')

Discord.py "Za Warudo" Bot Command

I'm making a bot command that...
Locks the channel; making no one able to send messages.
Sends an image and a message.
Waits 10 seconds before unlocking the channel, allowing others to send images.
However, I have tried different messages such as changing the role name and different sleep times, and the bot does not send a message, modify commands, or what it was intended to do. It is only this command that doesn't function, and no errors pop up in the console.
Could someone help me, please?
#commands.has_permissions(manage_messages=True)
async def warudo(ctx):
await ctx.channel.set_permissions(ctx.verified, send_messages=False)
await ctx.send('***Za Warudo!***')
await ctx.send(file=discord.File('https://pics.me.me/thumb_you-thought-it-was-an-emoji-but-it-was-me-71487398.png'))
await ctx.send( ctx.channel.mention + "has paused.")
await asyncio.sleep(10)
await ctx.channel.set_permissions(ctx.verified, send_messages=True)```

how to add reaction to a specifc user?

I've been working on this piece of code:
#client.event
async def lol(message,ctx):
if ctx.author.id == <user_id>:
await message.add_reaction('❤️')
else:
return
I am pretty sure that it is developed correctly, yet I am not getting the desired results.
I am pretty sure that if you check the console, an HTTP error would have been encountered. That's because you directly pasted the emoji in the function parameter. You have to add its unicode value. In your case:
await message.add_reaction(u'\u2764')
You are using an event decorator rather than a command, unless you were trying to detect if the user reacted, but its still completely off. What it looks like, the command that is called will send a message and the bot will react to it.
#client.command()
async def lol(ctx):
message = await ctx.send(f"{ctx.author.mention}, lol")
await message.add_reaction('😂')
If you then want to detect if the user had reacted to the emoji, you can use a event, but an on_reaction_add:
#client.event
async def on_reaction_add(reaction, user):
if reaction.emoji == '😂':
await user.send("Lol")

discord.py get webhooks of channel

I'm trying to make a webhook so if anyone says 'ez' it deletes it and sends a message with the webhook with a random message. Originally what I was doing was
if "ez" in message.content:
webhook = await message.create_webhook(name=ctx.author.name)
await webhook.send(ezmessages[random.randint(0, len(ezmessages))-1], username=message.author.name, avatar_url=message.author.avatar_url)
await message.delete()
await webhook.delete()
but the problem is this gets rate limited if webhooks are created and deleted too quickly. So instead what I want to do is check if the bot already has a webhook for the text channel, and if there is one use that but if not use a different one. I thought this would work:
for webhook in message.channel.webhooks:
await webhook.send(ezmessages[random.randint(0, len(ezmessages))-1], username=message.author.name, avatar_url=message.author.avatar_url)
but I get the error
TypeError: 'method' object is not iterable
Even though it should return a list
Anyone know how to correctly iterate over this?
TextChannel.webhooks it's not an attribute, its a function and a coroutine, so you need to call it and await it
webhooks = await message.channel.webhooks()
for webhook in webhooks:
...
docs

How can I delete a message in discord.py after a command was executed by an user?

I am trying to program a function that deletes the message with the command sent by the user.
E.g.:
User sends command /delmes
Bot sends a response to the command
Bot deletes the message sent by the user
Everything I was able to find until now (that would help me) was this Stack Overflow Thread: discord.py delete author message after executing command
But when I used the code, as described in the solutions, I only received AttributeError: 'Bot' object has no attribute 'delete_message'.
The discord.py API Reference (https://discordpy.readthedocs.io/en/latest/migrating.html#models-are-stateful / https://discordpy.readthedocs.io/en/latest/api.html?highlight=delete%20message#message) only revealed that some of the code had changed with newer versions.
So to speak client.delete_message would change to Message.delete(), if I interpreted it correctly!
After changing the code to what I thought it must be I received: NameError: name 'Message' is not defined
This is my code at the very last moment. (I am relatively new to discord.py and only used Python in school)
import discord
from discord.ext import commands
import random
import json
client = commands.Bot(command_prefix = '/', case_insensitive=True)
#client.command(pass_context=True)
async def delcommes(ctx):
await ctx.send("This is the response to the users command!")
await Message.delete(ctx.message, delay=3)
I couldn't understand your question very good but as far as I understand, when command executed, then after bot sends message, you want to delete the command message, in that case it's /delmes. You can use await ctx.message.delete().
#client.command(pass_context=True)
async def delcommes(ctx):
await ctx.send("This is the response to the users command!")
await ctx.message.delete()

Resources