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()
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 am trying to mute someone using a role Muted
The Code:
#bot.command()
async def mute(ctx, member:commands.MemberConverter):
role = discord.utils.find(lambda r: r.name == 'Muted', ctx.guild.roles)
await member.edit(roles=[role])
But I get this weird error:
Ignoring exception in command mute:
Traceback (most recent call last):
File "C:\Users\gathi\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\gathi\Vscode\Discord_Bot\tutorial.py", line 61, in mute
await member.edit(roles=[role]) # 848410518154117130
File "C:\Users\gathi\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\member.py", line 681, in edit
await http.edit_member(guild_id, self.id, reason=reason, **payload)
File "C:\Users\gathi\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\http.py", line 248, 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\gathi\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\bot.py", line 939, in invoke
await ctx.command.invoke(ctx)
File "C:\Users\gathi\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 863, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "C:\Users\gathi\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
I don't understand what to do.
Here is the perms of the bot:
The bots perms
The answer is within the bot Forbidden: 403 Forbidden (error code: 50013): Missing Permissions should instantly tell you it's a permissions issue.
The bot fails on await member.edit(roles=[role]) # 848410518154117130 which tells you the code works but it can't assign the role.
Make sure the bot:
Is higher than you in the role hierarchy
Has Manage Roles or the Administrator permission
If this continues to not work then consider going to my previous answer about intents.
You can use discord.Member.add_roles to assign a role.
#bot.command()
async def mute(ctx, member: commands.MemberConverter):
role = discord.utils.find(lambda r: r.name == 'Muted', ctx.guild.roles)
await member.add_roles(role)
And this is the unmute command.
#bot.command()
async def unmute(ctx, member: commands.MemberConverter):
role = discord.utils.find(lambda r: r.name == 'Muted', ctx.guild.roles)
await member.remove_roles(role)
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-
import discord
import logging
from discord.utils import get
from discord.ext import commands
logging.basicConfig(level=logging.INFO)
bot = commands.Bot(command_prefix='!')
#bot.event
async def on_ready():
print('Logged in as')
print(bot.user.name)
print(bot.user.id)
print('------')
#bot.command(pass_context=True)
async def setrole(ctx, a: str):
member = ctx.message.author
role = discord.utils.get(member.guild.roles, name=a)
await member.add_roles(member, role)
This is the code i use trying to assign roles to people entering the command. The role available in my server is rs6 so the code should be !setrole rs6 but its not working.
Ignoring exception in command setrole:
Traceback (most recent call last):
File "C:\Users\Ruiyang(Harry)Wang\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 83, in wrapped
ret = await coro(*args, **kwargs)
File "C:\Workshop\example_bot.py", line 19, in setrole
await member.add_roles(member, role)
File "C:\Users\Ruiyang(Harry)Wang\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\member.py", line 641, in add_roles
await req(guild_id, user_id, role.id, reason=reason)
File "C:\Users\Ruiyang(Harry)Wang\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\http.py", line 223, in request
raise NotFound(r, data)
discord.errors.NotFound: 404 Not Found (error code: 10011): Unknown Role
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\Ruiyang(Harry)Wang\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\bot.py", line 892, in invoke
await ctx.command.invoke(ctx)
File "C:\Users\Ruiyang(Harry)Wang\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 797, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "C:\Users\Ruiyang(Harry)Wang\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 92, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: NotFound: 404 Not Found (error code: 10011): Unknown Role
I tried to do print(role) after line role = discord.utils.get(member.guild.roles, name=a)
and it printed rs6 which is correct. Someone please help me! Thanks!
You can do a snazzy thing using command decorators. In the same way that you've set the type of arg to be a str, you can also set it to be a discord object:
#bot.command() # Note that context is automatically passed in rewrite
async def setrole(ctx, role: discord.Role):
await ctx.author.add_roles(role)
await ctx.send(f"I gave you {role.mention}!")
You might want an error handler to deal with if the role isn't found:
#setrole.error
async def _role_error(ctx, error):
if isinstance(error, commands.errors.BadArgument):
await ctx.send("I couldn't find that role!")
References:
commands.BadArgument
Member.add_roles()
Error handler per command
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)