I want to, when a command is ran give the person who ran it a certain role and another command to remove the role. How would I do this?
#bot.command
#lightbulb.command('getpings', 'Will be Pinged (/help For More Info)')
#lightbulb.implements(lightbulb.SlashCommand)
async def give_pings(ctx):
await bot.rest.add_role_to_member(user=ctx.author, guild=ctx.guild_id, role=roleid)
Error:
E 2022-06-09 21:23:29,648 hikari.event_manager: an exception occurred handling an event (InteractionCreateEvent)
Traceback (most recent call last):
File "C:\Users\avih2\AppData\Local\Programs\Python\Python310\lib\site-packages\lightbulb\app.py", line 1154, in invoke_application_command
await context.invoke()
File "C:\Users\avih2\AppData\Local\Programs\Python\Python310\lib\site-packages\lightbulb\context\base.py", line 292, in invoke
await self.command.invoke(self)
File "C:\Users\avih2\AppData\Local\Programs\Python\Python310\lib\site-packages\lightbulb\commands\base.py", line 544, in invoke
await self(context, **kwargs)
File "C:\Users\avih2\AppData\Local\Programs\Python\Python310\lib\site-packages\lightbulb\commands\base.py", line 459, in __call__
return await self.callback(context, **kwargs)
File "c:\Users\avih2\Desktop\PyProjects\API Test\bot.py", line 35, in give_pings
await bot.rest.add_role_to_member(user=ctx.author, guild=ctx.guild_id, role=roleid)
File "C:\Users\avih2\AppData\Local\Programs\Python\Python310\lib\site-packages\hikari\impl\rest.py", line 2705, in add_role_to_member
await self._request(route, reason=reason)
File "C:\Users\avih2\AppData\Local\Programs\Python\Python310\lib\site-packages\hikari\impl\rest.py", line 820, in _request
await self._handle_error_response(response)
File "C:\Users\avih2\AppData\Local\Programs\Python\Python310\lib\site-packages\hikari\impl\rest.py", line 842, in _handle_error_response
raise await net.generate_error_response(response)
hikari.errors.ForbiddenError: Forbidden 403: (50013) 'Missing Permissions' for https://discord.com/api/v8/guilds/980618833838624821/members/439959575647748096/roles/980948889567911958
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\avih2\AppData\Local\Programs\Python\Python310\lib\site-packages\lightbulb\app.py", line 1195, in handle_interaction_create_for_application_commands
await self.invoke_application_command(context)
File "C:\Users\avih2\AppData\Local\Programs\Python\Python310\lib\site-packages\lightbulb\app.py", line 1172, in invoke_application_command
raise new_exc
lightbulb.errors.CommandInvocationError: An error occurred during command 'getpings' invocation
First of all, you're going to have to get the role ID of the role you want to add/remove.
Let's assume the variable for the role you want to add is called roleid.
You can use lightbulb.BotApp.rest.add_role_to_member to add a role to a member.
assuming you have a variable called bot under which you instantiate lightbulb.BotApp,
await bot.rest.add_role_to_member(user=ctx.author, guild=ctx.guild_id, role=roleid)
Your code is good, but the role you want to add to user needs to be under the bot role. (idk why but that worked for me)
Related
#client.command(pass_context=True)
#commands.has_permissions(administrator=True)
async def clear(ctx,amount=1000):
await ctx.channel.purge(limit=amount)
#clear.error
async def clear_error(ctx,error):
if isinstance(error, MissingPermissions):
text = "Sorry {}, you do not have permissions to do that!".format(ctx.message.author)
await client.send_message(ctx.message.channel, text)
output: ( I want it to send a message to that user, who have no permission administration, and delete his message I dont understand this error, it happeen when I try to send please fix it ty would you fix it for me guys thank you im new here. )
Ignoring exception in on_message
Traceback (most recent call last):
File "C:\Users\cyberx\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\cyberx\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 855, in invoke
await self.prepare(ctx)
File "C:\Users\cyberx\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 777, in prepare
if not await self.can_run(ctx):
File "C:\Users\cyberx\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 1087, in can_run
return await discord.utils.async_all(predicate(ctx) for predicate in predicates)
File "C:\Users\cyberx\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\utils.py", line 348, in async_all
for elem in gen:
File "C:\Users\cyberx\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 1087, in <genexpr>
return await discord.utils.async_all(predicate(ctx) for predicate in predicates)
File "C:\Users\cyberx\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 1790, in predicate
raise MissingPermissions(missing)
discord.ext.commands.errors.MissingPermissions: You are missing Administrator permission(s) to run this command.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\cyberx\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 71, in wrapped
ret = await coro(*args, **kwargs)
File "c:\Users\cyberx\Desktop\Paid projects\Discord bot\bot.py", line 25, in clear_error
await client.send_message(ctx.message.channel, text)
AttributeError: 'Bot' object has no attribute 'send_message'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\cyberx\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", line 343, in _run_event
await coro(*args, **kwargs)
File "c:\Users\cyberx\Desktop\Paid projects\Discord bot\bot.py", line 14, in on_message
await client.process_commands(message)
File "C:\Users\cyberx\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\bot.py", line 976, in process_commands
await self.invoke(ctx)
File "C:\Users\cyberx\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\bot.py", line 943, in invoke
await ctx.command.dispatch_error(ctx, exc)
File "C:\Users\cyberx\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 424, in dispatch_error await injected(ctx, error)
File "C:\Users\cyberx\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 77, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'Bot' object has no attribute 'send_message'
You need to make an error handler, like this:
#<commandname>.error
async def <commandname>_error(self, ctx, error):
if isinstance(error, commands.MissingPermissions): #could be other type of error
await ctx.send(f"Sorry {ctx.author.mention}, you do not have permissions to do that!")
If you are a newcomer, I recommend reading this: https://vcokltfre.dev/tutorial/12-errors/
Also, to delete the trigger just use await ctx.message.delete()
Firstly, the error handler should be commands.MissingPermissions not MissingPermissions.
Secondly, client.send_message() was deprecated in the rewrite.
Thirdly, I don't think you can use discord.py in python 2.7, maybe upgrade to python 3.
Making a support ticket like thing. To prevent spamming of the reaction to make loads of tickets, I give them the role "help". If they have this role, the check will not work, and not allow the reaction to do anything. I am getting no errors, but it is not working either - you can still spam.
Btw the role adding works fine
Edit: new code (deletes message and resends new one) - now the second message when reacted to doesnt do anything - long error
def check(reaction, user):
helprole = discord.Object("851168291770597376")
if user != bot.user and helprole not in user.roles:
return str(reaction) == '⛑'
while True:
reaction, user = await bot.wait_for("reaction_add", check=check)
channel = await ctx.guild.create_text_channel("⛑{}s-support-ticket".format(user.name))
await channel.send("**This is your support ticket**\nPlease state your problem below\nA mod will be with you shortly")
modchannel = bot.get_channel(839265539741188157)
await modchannel.send("**NEW SUPPORT TICKET**\n<#&774589745664753665>\n<#{}>".format(channel.id))
supportrole = await ctx.guild.create_role(name="{}'s Ticket".format(user.name))
role = supportrole
helprole = discord.Object("851168291770597376")
await user.add_roles(role)
await user.add_roles(helprole)
await msg.delete()
await supportticket(ctx)
Ignoring exception in command supportticket:
Traceback (most recent call last):
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "main.py", line 325, in supportticket
await user.add_roles(role)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 374, in __call__
return await self.callback(*args, **kwargs)
File "main.py", line 308, in supportticket
try:
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/message.py", line 1022, in delete
await self._state.http.delete_message(self.channel.id, self.id)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/http.py", line 250, in request
raise NotFound(r, data)
discord.errors.NotFound: 404 Not Found (error code: 10008): Unknown Message
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 939, in invoke
await ctx.command.invoke(ctx)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 863, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "/opt/virtualenvs/python3/lib/python3.8/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: NotFound: 404 Not Found (error code: 10008): Unknown Message
Have you verified that the bot has an administrator? If not, put it, in case it has it, check that if the bot has a role, it is above the other, and also that the role is properly configured.
i want to create a bot, who will get user a role via reaction.
The code seems to be working but i get an error in console.
But i get an error.
Ignoring exception in on_reaction_add
Traceback (most recent call last):
File "/home/pi/.local/lib/python3.7/site-packages/discord/client.py", line 312, in _run_event
await coro(*args, **kwargs)
File "slh.py", line 47, in on_reaction_add
await user.add_roles(user, newrole)
File "/home/pi/.local/lib/python3.7/site-packages/discord/member.py", line 641, in add_roles
await req(guild_id, user_id, role.id, reason=reason)
File "/home/pi/.local/lib/python3.7/site-packages/discord/http.py", line 223, in request
raise NotFound(r, data)
discord.errors.NotFound: 404 Not Found (error code: 10011): Unknown Role
Ignoring exception in on_reaction_add
Traceback (most recent call last):
File "/home/pi/.local/lib/python3.7/site-packages/discord/client.py", line 312, in _run_event
await coro(*args, **kwargs)
File "slh.py", line 47, in on_reaction_add
await user.add_roles(user, newrole)
File "/home/pi/.local/lib/python3.7/site-packages/discord/member.py", line 641, in add_roles
await req(guild_id, user_id, role.id, reason=reason)
File "/home/pi/.local/lib/python3.7/site-packages/discord/http.py", line 223, in request
raise NotFound(r, data)
discord.errors.NotFound: 404 Not Found (error code: 10011): Unknown Role
I don't know why i get this error. Because the role "testrole" is exist in my discord. I have try to use discord.utils.get withe name="channelname" and with id="723xxxxx". Both brings the same error.
The Bot has all privilegs/rights on discord.
Who can help me to fix this issue ?
Here is the code:
#bot.event
async def on_reaction_add(reaction, user):
if reaction.emoji == '✅':
newrole = discord.utils.get(user.guild.roles, name="testrole")
await user.add_roles(user, newrole)
Hope someone can help me please.
The problem is that it's trying to add user as a role. You can simply delete that, you do not need to pass it in as an argument as you are already calling the function from the class. Use this instead:
await user.add_roles(newrole)
I don't know what this happen.
I created a text_channel, but it give me this error.
And I didn't find what is false.
Please help me to solve this problem.
Ignoring exception in command tk:
Traceback (most recent call last):
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 83, in wrapped
ret = await coro(*args, **kwargs)
File "/home/runner/FunMc/cmds/ticket.py", line 22, in tk
ticket = await guild.create_text_channel(f"questions-{r.randint(0,99999)}", overwrites=overwrites)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/guild.py", line 869, in create_text_channel
channel = TextChannel(state=self._state, guild=self, data=data)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/channel.py", line 107, in __init__
self._update(guild, data)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/channel.py", line 131, in _update
self._fill_overwrites(data)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/abc.py", line 294, in _fill_overwrites
self._overwrites.append(_Overwrites(id=overridden_id, **overridden))
TypeError: __new__() got an unexpected keyword argument 'allow_new'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 892, in invoke
await ctx.command.invoke(ctx)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 797, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "/opt/virtualenvs/python3/lib/python3.8/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: TypeError: __new__() got an unexpected keyword argument 'allow_new'
member = ctx.author
guild = ctx.guild
overwrites = {
guild.default_role: discord.PermissionOverwrite(read_messages=False),
guild.me: discord.PermissionOverwrite(read_messages=True, manage_channels=True, send_messages=True, manage_messages=True, manage_roles=True),
member: discord.PermissionOverwrite(read_messages=True)
}
ticket = await guild.create_text_channel(f"questions-{r.randint(0,99999)}", overwrites=overwrites)
Simply update discord.py
Linux/macOS
python3 -m pip install -U discord.py
Windows
py -3 -m pip install -U discord.py
Happened same for me:
I've got no idea what happened, I even reverted to latest working and tested out version of my code, where it actually created a room. But now a few days later it just doesn't. And spits out :
As you may see it never got through the "create_text_channel"
Im creating a bot that can ban and kick users if they dont follow the rules.
when i execute !ban #user it gives the error
Ignoring exception in command kick:
Traceback (most recent call last):
File "C:\Users\Daniel\AppData\Local\Programs\Python\Python37-32\lib\site-packages\discord\ext\commands\core.py", line 83, in wrapped
ret = await coro(*args, **kwargs)
File "C:/Users/Daniel/Desktop/DAN/1/8/0/1/PROGRAMMING/LegacyCoding/DiscordVidTutBot/bot.py", line 85, in kick
await ctx.guild.kick(member, reason=reason)
File "C:\Users\Daniel\AppData\Local\Programs\Python\Python37-32\lib\site-packages\discord\guild.py", line 1627, in kick
await self._state.http.kick(user.id, self.id, reason=reason)
File "C:\Users\Daniel\AppData\Local\Programs\Python\Python37-32\lib\site-packages\discord\http.py", line 221, 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\Daniel\AppData\Local\Programs\Python\Python37-32\lib\site-packages\discord\ext\commands\bot.py", line 892, in invoke
await ctx.command.invoke(ctx)
File "C:\Users\Daniel\AppData\Local\Programs\Python\Python37-32\lib\site-packages\discord\ext\commands\core.py", line 797, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "C:\Users\Daniel\AppData\Local\Programs\Python\Python37-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: Forbidden: 403 Forbidden (error code: 50013): Missing Permissions
I have been through the developer section of discord and made sure it has all the permissions needed to execute the ban and kick command such as admin kick members ban members and still its not working. does anybody know the cause of this and how i can fix it?
I found a fix for it and it turns out that i was trying to kick a member with higher permissions than the bot had so it would not kick them.