Allow only a message in a chat discord.py - python

I'm trying to make a only allowed message in a chat. The code I tried deletes every message even if is the correct one. What am I doing wrong?
#bot.event
async def on_message(message):
if message != '!d bump':
await message.delete()

You're comparing a Message instance to a string, to get the actual content of the message use the content attribute
#bot.event
async def on_message(message):
if message.content != '!d bump':
await message.delete()

Related

How do I delete a dm message that was sent by my discord bot

I did a script where you can send a message to a specific dm using my discord bot
#client.command(pass_context=True)
async def dm(ctx, user: discord.User, *, message=None):
await ctx.channel.purge(limit=1)
message = message
await user.send(message)
But how am I to make a script that can delete all messages that was sent by the bot in a specific dm
You can iterate over the DMChannel.history() and delete the messages whose author is your client:
#client.command()
async def clear_dm(ctx, user: discord.User):
async for message in user.dm_channel.history():
if message.author == client.user:
await message.delete()
Keep in mind that you need read_message_history and manage_messages permissions respectively, otherwise a Forbidden exception will be raised.
Also, maybe it's a good idea to catch HTTPException as well, to continue the iteration if the removal of any message fails for some reason.
Never mind I fixed it. Here is the script
#client.command()
async def cleardm(ctx, user: discord.User):
#await ctx.channel.purge(limit=1) remove '#' if you want the bot to delete the command you typed in the discord server
async for message in user.history():
if message.author == client.user:
await message.delete()

Discord.py how to get the last message and reply to it without any prefix/command

I want to make a bot for discord that will reply to each message using cleverbot. I would like that messages can be without any prefix or command. I will use cleverbot api or selenium for get the cleverbot answer. What is the easiest way to get the last message written in the chat and reply to it?
Something like this:
User: What is your favorite color?
Bot: Green.
You should use the on_message event to listen for a message. Then you can check the content, get your response, and then use channel.send (passing reference=message) to reply.
Below is an example where every time a user says "hi" the bot responds with "hello":
#bot.event # or, if in a cog, #commands.Cog.listener()
async def on_message(message):
if message.author.bot:
return # ignore bots
if message.content == "hi":
await message.channel.send("hello", reference=message)
on_message event is working when a message sent to a channel that your bot can see.
In cog:
#commands.Cog.listener()
async def on_message(self, message):
if not message.author.bot: #checking user is not a bot
if message.content.lower() == "what is your favorite color":
await message.reply("Green. What is your's?")
Not in cog:
#bot.event
async def on_message(message):
if not message.author.bot:
if message.content.lower() == "what is your favorite color":
await message.reply("Green. What is your's?")
#we are handling messages so your bot's commands will not work
#but there is a solution for this
else:
ctx = bot.get_context(message) #getting commands.Context object
await bot.invoke(ctx) #running command if exist

How to make a discord.py rewrite bot add emojis t its own message

Im trying to make a discord.py rewrite bot add a emoji to its own message but keeps adding emoji to the users message.
here is my code.
#client.event
async def on_message(message):
if message.author.bot:
return
if message.content.lower() == "hi":
em=discord.Embed(description=f"Hello, {message.author.name}",color=discord.Colour.red())
await message.channel.send(embed = em)
for emoji in ('👋'):
await message.add_reaction(emoji)
Because message is the message that the user sent, not the bot.
msg = await message.channel.send(embed=em)
await msg.add_reaction('👋')

Discord Python Rewrite - Slice Display Name

I made a working AFK code, but i need it to slice (delete) '[AFK]' if the user sends a message. The code is:
#client.command()
async def afk(ctx, *, message="Dind't specify a message."):
global afk_dict
if ctx.author in afk_dict:
afkdict.pop(ctx.author)
await ctx.send('Welcome back! You are no longer afk.')
await ctx.author.edit(nick=ctx.author.display_name(slice[-6]))
else:
afk_dict[ctx.author] = message
await ctx.author.edit(nick=f'[AFK] {ctx.author.display_name}')
await ctx.send(f"{ctx.author.mention}, You're now AFK.")
I got no errors. i only need await ctx.author.edit(nick=ctx.author.display_name(slice[-6])) to be working and i'll be happy.
You need to have a on_message event. Something like this should work.
#client.event
async def on_message(message):
if message.author in afk_dict.keys():
afk_dict.pop(message.author.id, None)
nick = message.author.display_name.split('[AFK]')[1].strip()
await message.author.edit(nick=nick)
I also recomend you to store user id's instead of usernames.
To do that you just have to use member.id instead of member.

argument of type 'TextChannel' is not iterable

So I am trying to make an on_message event that when someone says something in a specific channel then the bot will react to that message with the like and dislike emojis but I keep getting the error in the title.
async def on_message(message):
await client.process_commands(message)
channel = client.get_channel(738118906475970640)
if message.content in channel:
await message.add_reaction("👍")
await message.add_reaction("👎")
Your problem is, that u try to check if a string (message.content) is in a discord.TextChannel object (channel).
I think you are searching for something like
async def on_message(message):
if message.channel.id == 738118906475970640:
# react to these emojis

Categories