discord.py bot repeating messages - discord

hi i am trying to make a discord.py bot so i can have a gif chat channel but when someone types a message in that channel then my bot starts repeating his message, pls help if u know.
my code:
#client.event
async def on_message(message):
with open("gif_chater.json", "r+") as file:
data=json.load(file)
if str(message.channel.id) in data:
if message.content.startswith("https://tenor.com/"):
print("wOw")
if not message.content.startswith("https://tenor.com/") and not message.author.id == "760083241133932544":
await message.channel.send("lol pls use gifs in this channel : )")
await message.delete()
exit

The on_message event
The issue is that the bot is constantly responding to itself, and it's because the on_message event triggers not just when users send a message but also when the bot sends a message. As such, once it tells the user that they must only post tenor gifs, it reacts to its own message and goes into an infinite loop, posting and deleting its responses.
Preventing the bot from responding to itself
To prevent the bot from responding to it's own messages, you should add a check at the start of the event like in the discord.py docs:
#client.event
async def on_message(message):
if message.author == client.user:
return
...
Also, the ID check at the end
The last condition in your code before it decides to send a message is checking the ID of the messenger (not message.author.id == "760083241133932544"). I don't know whether it's meant to avoid deleting you or the bot's messages but regardless, the check itself is bugged. message.author.id returns an integer but is then being compared to a string, and due to the conflicting types, will always return False.
To fix it, change your ID to an integer by removing the quotes: not message.author.id == 760083241133932544. As well, you should use the not-equals operator != instead of not to improve readability: message.author.id != 760083241133932544.
Also, since you already checked if the message starts with the website link, you can use an elif statement instead of rechecking the condition, since else/elif guarantees that the previous condition was false (aka, that the message didn't start with the website link):
if message.content.startswith("https://tenor.com/"):
print("wOw")
elif message.author.id != 760083241133932544:
await message.channel.send("lol pls use gifs in this channel : )")
await message.delete()
Fixes combined
With the new changes, your function could look something like this:
#client.event
async def on_message(message):
# Don't respond to the bot's own messages
if message.author == client.user:
return
with open("gif_chater.json") as file:
data = json.load(file)
if str(message.channel.id) in data:
if message.content.startswith("https://tenor.com/"):
print("wOw")
elif message.author.id != 760083241133932544:
await message.channel.send("lol pls use gifs in this channel : )")
await message.delete()

Related

Discord.py looping messages up to infinity

#client.event
async def on_message(message):
if message.author == client:
return
if message.channel.id == 885569828436447254:
await message.channel.send(bgmi)
await message.channel.send(news)
I am just trying to ping the role which is stored in variable bgmi for each message in a particular channel and it works!
But the glitch is that every time a message is received, the bot sends both the messages again and again until the bot is stopped. Please help me configure it and make it send the messages only a single time.
#client.event
async def on_message(message):
if message.guild is None or message.author == message.guild.me:
return
if message.channel.id == 885569828436447254:
await message.channel.send(bgmi)
await message.channel.send(news)
You should use Guild.me. In a guild, message.author is of Discord type Member, while client is just the python type used to manage the communication between discord. What you actually want is Guild.me
This works only if the message is sent in a discord server, for private channels you'll need to do otherwise
Replace if message.author == client: with if message.author == client.user:

How do I check to see if the channel id is equal to something

I'm trying to make and event that checks to see if the channel id is equal to a specific id, if it is then the bot adds a reaction to that message. I'm not quite sure how to do this and have looked up many solutions but none of them help. There are some errors in the code that I still have to figure out like channel.id is not and actual command. Here is my code:
#client.event
async def on_message(message):
await discord.Client.get_channel(<channel_id>)
if channel.id == <channel_id>:
await message.add_reaction("✔️")
await message.add_reaction("❌")
discord.Client is a type, not a instance. And there is a message property inside discord.Message.
#client.event
async def on_message(message):
if message.channel.id == <channel_id>:
await message.add_reaction("✔️")
await message.add_reaction("❌")

I cannot understand how to use this code in discord.py

It regards user imput, so like I was commissioned to do a prompt but I've never done this so like this is what i found online
playerChoice = await bot.wait_for('message', check=check(ctx.author), timeout=30)
I get some of it, but I don't get the 'message' part and the 'check = check'.
Here's my full code
#client.command()
async def event(ctx):
await ctx.send("Prompt will continue in DMs.")
embed = discord.Embed(title="Event Prompt", description="Please specify the type of event.")
embed.set_footer("Prompt will expire in ## seconds")
await ctx.author.send(embed=embed)
eventType = await bot.wait_for('message', check=check(ctx.author), timeout=30) # I want it to send the event type.
await ctx.send(eventType)
I'd like an explaination and a possible way to improve that and make it work. Thanks in advance
https://discordpy.readthedocs.io/en/stable/api.html?highlight=wait_for#discord.Client.wait_for
#client.event
async def on_message(message):
if message.content.startswith('$greet'):
channel = message.channel
await channel.send('Say hello!')
def check(m):
return m.content == 'hello' and m.channel == channel
msg = await client.wait_for('message', check=check)
await channel.send('Hello {.author}!'.format(msg))
await client.wait_for('message', check=check)
message refers to the event. You could have also do it for the event reaction_add, voice_state_update, etc.
So this means you are waiting for a message. The check defines what you are waiting for exactly. It is a method and returns a True or False.
In my case I am waiting for a message event which is posted in the same channel and has the message content "hello".
If you didn't have such check it would pick up a message that might have been send by a different author, in a different channel, in a different guild.

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} Delete a specific message from a specific user

I want to delete a specific message from a specific user using discord.py
The user's id is: 462616472754323457
The message that he can't send: lmao
So if he sends lmao, it deletes the message and send "You can't do that <#462616472754323457>
Try discord.TextChannel.purge.
#client.event
async def on_message(message):
if len(await message.channel.purge(limit=200, check=lambda x: ('lmao' in x.content.strip().lower()) and x.author.id == 462616572754323457)) > 0:
await message.channel.send('You are not allowed to do that, <#462616572754323457>')
Accounting for when the bot is offline, the bot will check 200 messages before the current message and delete any that are from 462616572754323457 and have 'lmao' in the contents. (You can further enhance this by using re.)
You can use the on_message event, which triggers whenever a message is sent.
#bot.event
async def on_message(message):
await bot.process_commands(message) # add this if also using command decorators
if message.author.id == 462616472754323457 and "lmao" in message.content.lower():
await message.delete()
await message.channel.send(f"You can't do that, {message.author.mention}")
References:
discord.on_message()
Message.delete()
Bot.process_commands()
Member.mention

Resources