Is it possible to delete a message, if the message contain a letter?
I'm writing a counting game and I need to somehow check, if the message doesn't contain the next correct number/contain letters, it must be deleted.
Also, it would be great to allow users make a small commentary after the numbers, like "12 Hello".
#client.event
async def on_message(message):
c_channel = discord.utils.get(message.guild.text_channels, name='Counting')
if message.channel.id == 862353141535325:
messages = await c_channel.history(limit=2).flatten()
message = re.sub('\D', '', message)
messages = re.sub('\D', '', messages)
if message.channel == c_channel and int(messages[1].content) + 1 != int(message.content):
if message.author.bot:
return
else:
await message.delete()
await message.channel.send("Incorrect.", delete_after=1)
But, when I run it it gives me a mistake:
Ignoring exception in on_message
Traceback (most recent call last):
File "... Python\Python310\lib\site-packages\discord\client.py", line 343, in _run_event
await coro(*args, **kwargs)
File "... \Bot\Bot.py", line 85, in on_message
message = re.sub('\D', '', message)
File "... Python\Python310\lib\re.py", line 209, in sub
return _compile(pattern, flags).sub(repl, string, count)
TypeError: expected string or bytes-like object
Do you know how to "fix" the mistake or the better code than mine?
message is of the class discord.Message. The function re.sub was expecting a string or bytes-like object. So, you can use:
message = re.sub('\D', '', message.content)
messages = [re.sub('\D', '', msg) for msg in messages]
, where message.content returns the content of the message.
Note: You have to enable the message_content intent before accessing message content.
Related
I wan, that my discord.py bot delete a message, if I answer p!delete to a message.
I use this:
if message.content.lower() == 'p!delete':
message = message.reference
await message.delete()
and get this error:
Ignoring exception in on_message
Traceback (most recent call last):
File "C:\Users\USERNAME\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", line 343, in _run_event
await coro(*args, **kwargs)
File "FILEPATH", line 303, in on_message
await message.delete()
AttributeError: 'MessageReference' object has no attribute 'delete'
the problem is you're overwriting the message object. Here is the improved code
if message.content.lower() == 'p!delete':
await message.reference.delete()
Thank you all, but it now works with this code:
deleteMessage = message.reference.message_id
deleteMessage = await message.channel.fetch_message(deleteMessage)
await deleteMessage.delete()
await message.delete()
I know the name of emoji from printing \emoji in discord, but how to get the emoji that can be added to a message?
I would use ID and bot.get_emoji, but when I try to copy ID by Right click->Copy ID, the copied ID is an ID of a message with the emoji.
await msg2.add_reaction(":bookmark_tabs:")
I also tried to do that by discord.utils.get() but that caused an error
await msg2.add_reaction(discord.utils.get(bot.emojis, name = "bookmark_tabs"))
await msg2.add_reaction(discord.utils.get(bot.emojis, name = ":bookmark_tabs:"))
await msg2.add_reaction(discord.utils.get(bot.emojis, name = "<bookmark_tabs:>"))
await msg2.add_reaction(discord.utils.get(bot.emojis, name = "<:bookmark_tabs:>"))
All of these cause the error:
Ignoring exception in on_raw_reaction_add
Traceback (most recent call last):
File "C:\Users\plays\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\client.py",
line 312, in _run_event
await coro(*args, **kwargs)
File "C:\Users\plays\OneDrive\Рабочий стол\Python\bot2.py", line 172, in on_raw_reaction_add
await msg2.add_reaction(discord.utils.get(bot.emojis, name = "<bookmark_tabs:>"))
File "C:\Users\plays\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\message.py",
line 927, in add_reaction
emoji = self._emoji_reaction(emoji)
File "C:\Users\plays\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\message.py",
line 1014, in _emoji_reaction
raise InvalidArgument('emoji argument must be str, Emoji, or Reaction not
{.__class__.__name__}.'.format(emoji))
discord.errors.InvalidArgument: emoji argument must be str, Emoji, or Reaction not NoneType.
bookmark_tabs it's not a custom emoji so discord.utils.get will always return None, you need to get the unicode for it. To get it simply \:emoji: send it and copy the message.
The unicode got bookmark_tabs is 📑, to add a reaction with it:
await message.add_reaction('📑')
I am making a bot in python using discord.py , i was trying delete the command when it is executed
it is working but giving me a error and the error is
Ignoring exception in on_message
Traceback (most recent call last):
File "C:\Users\Dell\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\client.py",
line 312, in _run_event
await coro(*args, **kwargs)
File "C:/Users/Dell/Desktop/test_bot/add_role.py", line 25, in on_message
msg = await message.channel.fetch_message(761275239346339871)
File "C:\Users\Dell\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\abc.py",
line 956, in fetch_message
data = await self._state.http.get_message(channel.id, id)
File "C:\Users\Dell\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\http.py",
line 243, in request
raise NotFound(r, data)
discord.errors.NotFound: 404 Not Found (error code: 10008): Unknown Message
After giving the error also it works , but i want to know what rises the error
code :
msg = await message.channel.fetch_message(761275239346339871)
await msg.delete()
The problem is, The message id that you inputted has already been deleted. If you want to delete the author message just do
await ctx.message.delete()
if you want to delete the inputted message by wait_for you can do this
msg = await client.wait_for('message', check=lambda message: message.author == ctx.author)
await msg.delete()
if on_message.
#client.event
async def on_message(message):
message = await message.channel.send(message here)
await message.delete()
this is my code that should give message author a role:
color roles
#bot.event
async def on_message(ctx, message):
await bot.process_commands()
user = ctx.message.author
msg = message.content.lower
role = discord.utils.get(discord.guild.roles, name = "blue as alian egg")
if msg == ['I am blue']:
await user.add_roles(role)
and this is my errors:
C:\Users\Егор\AppData\Local\Programs\Python\Python38-32\python.exe C:/Users/Егор/PycharmProjects/Luiji/Luiji.py
Luiji is ready to work!
Ignoring exception in on_message
Traceback (most recent call last):
File "C:\Users\Егор\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\client.py", line 312, in _run_event
await coro(*args, **kwargs)
TypeError: on_message() missing 1 required positional argument: 'message'
on_message does not have a ctx, it should just be
async def on_message(message):
That is what the error on_message() missing 1 required positional argument: 'message' means, you have an extra argument, message.
I'm making a bot with Discord.py and I keep getting an error when trying to react to an embed I sent, I posted the whole loop below, but mainly the error points to the line await msg.add_reaction(emoji=reactions). I know that the the unicode string has to be passed in the function above, but it seems like it would not take it if it was received via the text channel even though the unicode was exactly the same. I even added the print function to just see if the unicode printed exactly the same and it did which the same exact length, no spaces. If I just input the unicode directly as a string for example: await msg.add_reaction(emoji='\U0001f310') then it works and is able to react to that message with the correct reaction, but if I have it received via msg.content and then passed to the function then it throws the unkown emoji error. I know that the message content is good since it gets printed exactly to the console. Any help would be appreciated.
Here is the error I get:
\U0001f310
Ignoring exception in on_message
Traceback (most recent call last):
File "C:\Users\Joe\AppData\Local\Programs\Python\Python36\lib\site-packages\discord\client.py", line 227, in _run_event
await coro(*args, **kwargs)
File "C:\Users\Joe\Desktop\voicebot discord\test2.py", line 94, in on_message
await msg.add_reaction(emoji=reactions)
File "C:\Users\Joe\AppData\Local\Programs\Python\Python36\lib\site-packages\discord\message.py", line 708, in add_reaction
await self._state.http.add_reaction(self.id, self.channel.id, emoji)
File "C:\Users\Joe\AppData\Local\Programs\Python\Python36\lib\site-packages\discord\http.py", line 214, in request
raise HTTPException(r, data)
discord.errors.HTTPException: BAD REQUEST (status code: 400): Unknown Emoji
My Code:
while embedLoop:
await channel.send('What is the title?')
msg = await client.wait_for('message', check=check)
title = msg.content
await channel.send('The title is ' + title)
await channel.send('What is the description?')
msg = await client.wait_for('message', check=check)
desc = msg.content
await channel.send('The description is ' + desc)
await channel.send('Enter emoji unicode: ')
msg = await client.wait_for('message', check=check)
reactions = msg.content
embed=discord.Embed(colour = 4691711)
embed.add_field(name=title, value= desc, inline=False)
msg = await embedChannel.send(embed=embed)
print(reactions)
await msg.add_reaction(emoji=reactions)