Discord button failure discord.py - discord.py

I am getting a failed interaction when anyone presses my discord button, but it is executing all of the code (ie the final message is sent to users). Help please!
###code to add a join button
button = discord.ui.Button(label="Join village", emoji="🐺", style=discord.ButtonStyle.green)
async def button_callback(interaction):
if interaction.user.id in Rooms[room].players:
await interaction.user.send("You are already in room %s" % room)
return
Rooms[room].add_player(interaction.user.id)
await interaction.user.send("You have joined room %s" % room)
button.callback = button_callback
view = discord.ui.View()
view.add_item(button)
await ctx.send("Join the new %s village by clicking here! Note: expect an 'This interaction failed'- don't worry "
"you are still added to the game!" % room, view=view)
Picture of code
Failure of button

You can try interaction.response.send_message() and if you want to make it private add ephemeral=True inside the perameter like interaction.response.send_message("Your message", ephemeral=True)

Related

How to rig a bot's command to only a certain user

I've been making this pp size machine, which is somewhat like dank memer's one
#client.command()
async def pp(ctx,member : discord.Member):
size=random.randint(1,10)
message = "8"
for x in range (size):
message= message + "="
if x == size-1:
message = message + "D"
if member == "PROTATO#6826":
message = "8D"
ppsize = discord.Embed(color=discord.Colour.orange(), title=f"PP size",description=f"""{member}'s penis
{message} """)
await ctx.send(embed = ppsize)
But i want to rig the command for a certain user to only show "8D" instead of the random lengths.
But no matter what it is the
if member == "PROTATO#6826":
message = "8D"
code doesnt run or gets over looked?
Can someone help me out with this?
You can check if the member mentioned has the same id as a specific user. You can either enable developer mode on discord, or you can print the member.id at the very beginning. Do view a code example below.
#client.command()
async def pp(ctx, member: discord.Member):
print(member.id) # this would print a user's unique id
message = "Hey!"
if member.id == 394506589350002688: # replace this id with what was printed at the start
message = "Hello!"
await ctx.send(message)
Similar Questions:
How to check if message was sent by certain user? - SO
How do I detect if the message is sent by a specific user id? - SO

How to make application bot available for more than one user at a time?

when the application bot is used by more than 1 person at a time it merges the answers from applicant A and applicant B and sends them as 1 application.
I thought about limiting the bot's usage to 1 person at a time but didn't know how to do it.
here's the code:
#client.event
async def on_message(message):
if message.content.startswith('!add'):
var = list2.append #This list will have the raw answers
var3 = list3.append #THis list will have the questions and the answers lined up like this format {Question}: {Answer}
await message.author.send(
"``Fill in the blanks by replying, then the bot will give a confirmation message.``")
for element in report:
await message.author.send("**"+element+"**") #Sends the questions
try:
response = await client.wait_for('message',check=check,timeout=60*20) #waits for response for only 20 minutes
var(response.content) # if the response is sent before the 20min window closes it will add the response in list2
except asyncio.TimeoutError: #if there was no answer during the 20min window this script will run to abort
await message.author.send("```Report timed out, it will be deleted. ❌```")
list2.clear() #clears previous answered questions
return #prevents the rest of the code from running
if len(list2) == 10: #There are in list Report 10 questions. so when the answers reach 10 this will run
for i in range(len(list2)): #this will add the questions and the answers lined up in a single list
var3(report[i] + list2[i])
for element in list3:
await message.author.send(element + "\n") #this will send the questions and answers in dm for the user to double check
await message.author.send( content="**Double check the report then submit.:arrow_up:** \n```You got only 1min to submit!```", components=[Button(style=ButtonStyle.green, label="Submit")]) #sends confirmation button
try:
i=await client.wait_for("button_click", check=lambda i: i.component.label.startswith("Submit"),timeout=60) #if the "Submit" button is clicked on time
except asyncio.TimeoutError:
await message.author.send("```Submission timed out, report will get deleted. ❌```")
list2.clear()
list3.clear()
return
await i.respond(content="**" + "Report added Successfully" + "**" + ":white_check_mark:") #this will be sent in dm for confiramation
for element in list3:
await message.channel.send(element + "\n") #this will send the Questions and the Answers in a channel that the !add command was initiated in.

Discord bot sends many questions in dm at a time without waiting for the user to respond

I need my bot to send questions to users for them to answer when the user writes a command.
but the bot ended up sending 2 or 3 questions at a time randomly without waiting for the user to answer.
Image that shows the output of the bot.
which makes the value of RP N = "Name:"
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('!add'):
await message.author.send(
"``Fill in the blanks by replying, then the bot will give a confirmation message.``")
for element in report:
await message.author.send("**"+element+"**")
response = await client.wait_for('message')
var(response.content)
report is the list of the questions
var is list.append
Edit: I have tried to add check argument.
def check(m):
return m.content != element in report
And that didn't work, still having the same issue.
This fixed my problem.
def check(m):
return message.author == m.author and isinstance(m.channel, discord.DMChannel)
To delay commands I suggest using time.sleep() TimeSleep if you don't want it to respond so fast you can put a little delay.

How to use reaction as button in discord.py

If we have a message with ⬇️ and ⬆️ Reaction.
How can we get all users reacted in particular emojis and how to use as button.
Like,
If a user reacts his name will be added in message along with the emoji which he reached.
Here is a simple example this is not the best method since this will be active on all bot messages when someone reacts with ⬆️. The best is save the message id that you want then do a check here to make sure it is the wanted message.
If message.id is not an option then make a dedicated channel for this and hard code the id of that channel, this is not the best practice but it will work.
#bot.event
async def on_raw_reaction_add(payload):
channel = bot.get_channel(payload.channel_id)
message = channel.get_message(payload.message_id)
# guild = bot.get_guild(payload.guild_id)
emoji = payload.emoji.name
# skip DM messages
if isinstance(channel, discord.DMChannel):
return
# only work if in bot is the author
# skip messages not by bot
# skip reactions by the bot
if message.author.id != bot.user.id or payload.member.id == bot.user.id:
return
if emoji == '⬆️':
up_users = f"{message.content} \n {user.name}"
await message.edit(up_users)
# remove user reaction
reaction = discord.utils.get(message.reactions, emoji=emoji)
await reaction.remove(payload.member)

discord.py logs how i can do that?

Help me please, I want my bot to send logs (like "user entered the channel" and "message deleted", "user joined the server") thanks in advance
#commands.Cog.listener()
async def on_message_delete(self, message):
deleted = Embed(
description=f"Message deleted in {message.channel.mention}", color=0x4040EC
).set_author(name=message.author, url=Embed.Empty, icon_url=message.author.avatar_url)
deleted.add_field(name="Message", value=message.content)
deleted.timestamp = message.created_at
await channel.send(embed=deleted)
Let's make an example, you want to make it log when a user joins. Sure thing, I personally use client, and without cogs so you can change it if you want to:
#client.event
async def on_user_join(self, member):
logs_channel = client.get_channel(CHANNEL_ID_HERE)
logs_channel.send(f"{member} has joined")
# you can change the welcome message of course that is an example

Resources