discord bot can't handle requests from different users - python

im trying to create a chatbot that has conversations sequentially like this. this works with one user fine but if another user tries to use the bot at the same time it interferes with the first user. I understand I would have to use some sort of asynchronous programming to fix this but I'm not sure how. any help would be appreciated
#client.event
async def on_message(msg):
if msg.author.bot:
return
if msg.content == "hey":
await msg.channel.send('hi')
response = await client.wait_for('message')
if response.content =="how are you":
await response.channel.send('good')
client.run(token)

Explanation
response = await client.wait_for('message') will catch any message sent after the command is executed, regardless of whom it's from.
This can be rectified by adding a check to wait_for, like in the code below.
Code
response = await client.wait_for('message', check=lambda message: message.author == msg.author)
Reference
wait_for
lambda expressions

Related

discord.py add role on message

Ive seen a lot of questions about this and none of them worked for me, I don't understand why something that sounds this simple is that complicated, alredy spent more than 4 hours on this, I want to make a basic bot to make new users accept the rules:
Not much to explain, just a basic bot that when you say accept in a special channel it should add you a role.
import discord
from discord.utils import get
client = discord.Client()
TOKEN = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXX'
#client.event
async def on_ready():
#creates a message for users to react to
guild = client.guilds
channel = client.get_channel(836583535981608530)
Text= "Type accept below if you understand and `accept` ALL the rules in <#836600011484785843>, in order to gain access to the server:"
await channel.send(Text)
#client.event
async def on_message(message):
if message.author == client.user:
return
channel = message.channel
if channel.id == 836593532981608530 and message.content.lower() == ('accept'):
await message.delete()
user_role = discord.utils.get(message.guild.roles, name = "role name")
new_member = message.author
new_member.add_role(user_role, reason = "new member")
elif channel.id == 836593532981608530:
await message.delete()
The problem here in your command is that you have not awaited statement(s) that had to be awaited for library to work.
So, let's start.
The statement(s) that needs to be awaited:
new_member.add_role(user_role, reason = "new member")
How to fix this error?
You need to await it just like you have awaited other statements in your code.
Just change the line to:
await new_member.add_roles(user_role, reason="new member")
This would solve the problem you have facing.
Why do you need to await some statements?
Read the docs from the following link to see why some statements are needed to be awaited. This would help you to figure out what commands has to be awaited in future.
Hope it helps. If still you have any problem, feel free to ask me in the comments. :)
Thank You! :D

Discord.py Mass DM sometimes Bugs

i am Making a MassDM command but it dont works well, it sends a Message not to Everyone it stops automatically i think because they turn their DM's off thats why how can i Bypass it? or how can i Fix this error?
Code
#client.command()
async def massdm(self, ctx, *, message):
await ctx.message.delete()
embed = discord.Embed(description=f'**Sent everyone a DM with:** ``{message}``',color=embcolor)
await ctx.send(embed=embed, delete_after=delemb)
for user in list(ctx.guild.members):
try:
await asyncio.sleep(0.1)
await user.send(message)
except:
pass
To debug it properly, the first step would be to remove the try, except block and find out what exceptions are you actually getting. It's a bad habit to catch every exceptions at once. Without knowing what exception is it throwing, I'm unable to help more.
Another thing I would recommend is to benefit from the discord.py being an asynchronous package, and instead of iterating over the users synchronously, try something like that:
coros = [await user.send(message) for user in ctx.guild.members]
await asyncio.gather(*coros)
This way it will send the message to every user at the same time.
EDIT:
I also found another bug while using a loop like that. At some point the bot will try to send the DM to itself, and that is raising AttributeError. Make sure that you handle that, for example:
coros = [user.send(message) for user in ctx.guild.members if user != ctx.guild.me]
await asyncio.gather(*coros)
Maybe that was your issue all along.
Try this:) Hope it helps
#client.command(pass_context=True)
async def massdm(ctx):
await ctx.message.delete()
for member in list(client.get_all_members()):
try:
embed = discord.Embed(title="Test",
description="Test!",
color=discord.Colour.blurple())
embed.set_thumbnail(
url="test")
embed.set_footer(
text=
"test"
)
#delay. Set it to 10 to 20 seconds to avoid flagging
await asyncio.sleep(30)
await member.send(embed=embed)
except:
pass
#await ctx.send(f"Messaged: {member.name}")
print(f"Messaged: {member.name}")

discord.py bot isn't answering when mentioned from phone app

I made recently a discord bot for small server with friends. It is designed to answer when mentioned, depending on user asking. But the problem is, when someone mention bot from a phone app, bot is just not responding. What could be the problem?
Code:
import discord
from discord.ext import commands
from discord.ext.commands import Bot
import asyncio
bot = commands.Bot(command_prefix = '=')
reaction = "🤡"
#bot.event
async def on_ready():
print('Bot is ready.')
#bot.listen()
async def on_message(message):
if str(message.author) in ["USER#ID"]:
await message.add_reaction(emoji=reaction)
#bot.listen()
async def on_message(message):
mention = f'<#!{BOT-DEV-ID}>'
if mention in message.content:
if str(message.author) in ["user1#id"]:
await message.channel.send("Answer1")
else:
await message.channel.send("Answer2")
bot.run("TOKEN")
One thing to keep in mind is that if you have multiple functions with the same name, it will only ever call on the last one. In your case, you have two on_message functions. The use of listeners is right, you just need to tell it what to listen for, and call the function something else. As your code is now, it would never add "🤡" since that function is defined first and overwritten when bot reaches the 2nd on_message function.
The message object contains a lot of information that we can use. Link to docs
message.mentions gives a list of all users that have been mentioned in the message.
#bot.listen("on_message") # Call the function something else, but make it listen to "on_message" events
async def function1(message):
reaction = "🤡"
if str(message.author.id) in ["user_id"]:
await message.add_reaction(emoji=reaction)
#bot.listen("on_message")
async def function2(message):
if bot.user in message.mentions: # Checks if bot is in list of mentioned users
if str(message.author.id) in ["user_id"]: # message.author != message.author.id
await message.channel.send("Answer1")
else:
await message.channel.send("Answer2")
If you don't want the bot to react if multiple users are mentioned, you can add this first:
if len(message.mentions)==1:
A good tip during debugging is to use print() So that you can see in the terminal what your bot is actually working with.
if you print(message.author) you will see username#discriminator, not user_id

Does anyone know how i would convert this command into a on message command

Here Is My Code:
#client.command(pass_context=True)
async def banall(ctx):
await ctx.message.delete()
for user in list(ctx.guild.members):
try:
await ctx.guild.ban(user)
await ctx.send(f"{user.name} has been banned from {ctx.guild.name}")
except:
await ctx.send(f"{user.name} has FAILED to be banned from {ctx.guild.name}")
I'm trying to make it so when someone says "banall" it will ban all the members in the server. I found have found this command works but with for example !banall" and I want to get rid of the "!" and so it will just be "banall". Does anyone know how I would go about doing this. Thanks :D
Adapt the command to work with on_message(). This can be done by removing the method decorator (#client.command(pass_context=True)) and changing ctx to message since the on_message() function gets a message object.
async def banall(message):
await message.delete()
for user in list(message.guild.members):
try:
await message.guild.ban(user)
await message.channel.send(f"{user.name} has been banned from {message.guild.name}")
except:
await ctx.send(f"{user.name} has FAILED to be banned from {ctx.guild.name}")
Run the function within the on_message(). This can be done with an if statement to check whether the message's content is "banall".
#client.event
async def on_message(message):
if message.content == "banall":
await banall(message)
If you want to cut the "!" from "!banall" you need to do this:
string = "!banall"
string2 = string.replace('!', '')

How to delete a specific user's messages instantly?

I am trying to code a command into my Discord Bot that when triggered will actively delete new messages from a specific user.
I have tried a couple variations of using ctx but I honestly I don't entirely understand how I could use it to accomplish this.
This block of code deletes the message that triggers it. Because of this, I think I am taking the wrong approach because it only deletes from whoever triggers it and only if they type the command. Obviously, I am new to this. I would appreciate any and all help. Thanks so much :)
#client.event
async def on_message(ctx):
message_author = ctx.author.id
if message_author == XXXXXXXXXXXXXXXXX:
await ctx.message.delete()
There is no more CTX in discord rewrite (DiscordPy 1.0+) instead you should do:
#client.event
async def on_message(message):
if message.author.id == XXXXXXXXXXXXXXXXX:
await message.delete()
Source: link
You can make a command to add the users to a list then use on_message to check if a user is in that list and then delete it if true
dels=[]
#bot.event
async def on_message(msg):
if msg.author.id in dels:
await bot.delete_message(msg)
await bot.process_commands(msg)
#bot.command(name='del_msg')
async def delete_message(con,*users:discord.Member):
for i in users:
dels.append(i.id)
await con.send("Users {} have been added to delete messages list".format(" ,".join(users)))

Categories