Discord.py Emoji 400 - python

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)

Related

discord.py Text sifting

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.

Discord.py ticket system

I have started making Ticket bot using discord.py but I have an error in my code and I don't know how to solve it. Embed and reacting to message work but when I try to react everythings crashes. Here is my error:
Ignoring exception in command ticket:
Traceback (most recent call last):
File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "c:\Users\HP\Desktop\222\dscbot-dscbot.py", line 150, in ticket
if reaction == '📩':
NameError: name 'reaction' is not defined
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\bot.py", line 902, in invoke
await ctx.command.invoke(ctx)
File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 864, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 94, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: NameError: name 'reaction' is not defined
I hope someone have and idea how to fix this, here is my current code:
import discord
from discord.ext import commands
from discord.utils import get
from discord import Embed, Color
import DiscordUtils
import os
from discord.ext.commands import has_permissions, MissingPermissions
import json
intents = discord.Intents.default()
intents.members = True
client = commands.Bot(command_prefix = "-", intents = intents)
client.remove_command("help")
#client.command(pass_context=True)
async def ticket(ctx):
guild = ctx.guild
embed = discord.Embed(
title = 'Ticket system',
description = 'React 📩 to make a ticket.',
color = 0
)
embed.set_footer(text="ticket system")
msg = await ctx.send(embed=embed)
await msg.add_reaction("📩")
reaction = await msg.fetch_message(msg.id)
await client.wait_for("reaction_add")
await client.wait_for("reaction_add")
if reaction == '📩':
await guild.create_text_channel(name=f'ticket - {reaction.author.name}')
ps: I am new to discord.py
reaction = await msg.fetch_message(msg.id) is just giving you msg. You instead need to take the reaction from await client.wait_for("reaction_add")
Take a look at wait_for. You implement a check (Which is where the reaction is) to see if the reaction is a mailbox.
You need to create a new function that creates a channel if the reaction is a mailbox, then pass that into client.wait_for.
def check(reaction, user):
return str(reaction) == '📩' and ctx.author == user
await client.wait_for("reaction_add", check=check)
await guild.create_text_channel(name=f'ticket - {ctx.author}')
You didn't define reaction, that's why this error is raised.
Take a look at this post to find out how to fetch reactions from messages.

discord python bot kick command

i am trying to make a discord bot that can kick people with the command .kick
I have it display a message that says (username) has been kicked from the server and the message still shows up, but it doesn't actually kick them.
Here is my code:
import discord
from discord.ext import commands
client = commands.Bot(command_prefix=".")
#client.event
async def on_ready():
print("Bot is Ready")
#client.command(aliases=['c'])
#commands.has_permissions(manage_messages = True)
async def clear(ctx,amount=2):
await ctx.channel.purge(limit = amount)
#client.command(aliases=['k'])
#commands.has_permissions(kick_members = True)
async def kick(ctx,member : discord.Member,*,reason= "I do not need a reason"):
await ctx.send(member.name + " has been kicked from the server, because "+reason)
await member.kick(reason=reason)
#client.command(aliases=['b'])
#commands.has_permissions(ban_members = True)
async def ban(ctx,member : discord.Member,*,reason= "I do not need a reason"):
await ctx.send(member.name + " has been banned from the server, because"+reason)
await member.ban(reason=reason)
the error message says:
File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "C:\Users\user\discordbot\bot.py", line 18, in kick
await member.kick(reason=reason)
File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\member.py", line 512, in kick
await self.guild.kick(self, reason=reason)
File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\guild.py", line 1849, in kick
await self._state.http.kick(user.id, self.id, reason=reason)
File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\http.py", line 241, in request
raise Forbidden(r, data)
discord.errors.Forbidden: 403 Forbidden (error code: 50013): Missing Permissions
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\bot.py", line 903, in invoke
await ctx.command.invoke(ctx)
File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 859, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 94, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: Forbidden: 403 Forbidden (error code: 50013): Missing Permissions
please note that my .clear command is working perfectly fine, it is just kick and ban
please help, I am on python 3.9.0
#client.command()
#commands.has_permissions(administrator=True)
async def kick(ctx, member: discord.Member):
await member.kick()
await ctx.message.add_reaction("reaction")
await ctx.send(f"{member.name} has been kicked by {ctx.author.name}!")
await log_channel.send(f"{ctx.author.name} has kicked {member.display_name}")
This is what I used for my discord bot. It only lets the people with the kick permission use this command. I hope this helped.
This type of error comes when either user who is giving this command or your bot has not been given such admin permissions. In your server you can simply make one role for your bot or can give all privileges to your bot if it is managed by you.
link below may help you regarding this
https://support.discord.com/hc/en-us/articles/206029707-How-do-I-set-up-Permissions-

How to get a emoji by name, not ID and add it to the message? discord.py

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('📑')

404 NOT FOUND (error code : 1008) : Unknown message

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()

Categories