Check Role Permission Discord.py - python

I have a function that has a for loop that checks for a user roles:
for role in ctx.author.roles:
Now, I want to check if "role" has the permission Manage_Channels
Thanks for the helpers!!!

If you need this to check if the author has a permission to use the command. There is a bulit-in function to do that Docs.
from discord.ext.commands import has_permissions
#bot.command()
#commands.has_permissions(manage_channels=True)
async def test(ctx):
await ctx.send('You can manage channels.')
for the error message
from discord.ext.commands import MissingPermissions
#test.error
async def test_error(error, ctx):
if isinstance(error, MissingPermissions):
await ctx.send(f'Sorry {ctx.author.mention}, you do not have permissions to do that!')

Related

discord.py bot says function doesn't exist while it does

I was about to start making a discord bot with python when it didn't recognize a function i made.
The first minor problem I found was that when I made my async def on_ready() function, it didn't print anything while i did tell it to print("Bot is online.").
The second problem is that when I run the command !verify, it throws this error: discord.ext.commands.errors.CommandNotFound: Command "verify" is not found
This is my code:
import discord
from discord.ext import commands
client = commands.Bot(command_prefix="!")
async def on_ready():
print("Bot is online.")
async def verify(ctx):
role = discord.utils.get(ctx.guild.roles, name="Member")
if role not in ctx.message.author.roles:
print("NOT A MEMBER.")
else:
print("IS A MEMBER.")
client.run("TOKEN")
You need to add client.command() decorator to let discord.py know that it is a command:
#client.command()
async def verify(ctx):
role = discord.utils.get(ctx.guild.roles, name="Member")
if role not in ctx.message.author.roles:
print("NOT A MEMBER.")
else:
print("IS A MEMBER.")
Also write #client.event before your on_ready function because it is a discord.py event.

Discord bot kick command error message python

So I found this code for a kick command and I would like to know how to make it so if the person who sent the kick command doesn't have permissions it replies to the user and says that they don't have the right permissions.
#commands.has_permissions(administrator=True)
async def kick(ctx, user : discord.Member,*,reason):
await user.kick(reason=reason)
await ctx.send(f'{user} kicked for {reason} by {ctx.author}')
That's the code
This should send a message telling users that they don't have permissions if they try to use the command without the proper roles
from discord.ext.commands import MissingPermissions
#Import this at the top of your code
#Write the error handler below the code where you define the kick command
#kick.error
async def kick_error(self, ctx, error):
if isinstance(error, MissingPermissions):
await ctx.send("You don't have permission to kick members.")
For me I would write:
import discord
from discord.ext import commands
from discord.ext.commands import MissingPermissions
client = commands.Bot(command_prefix=("prefix"))
#client.command()
#commands.has_role("Administrator")
async def kick(self, ctx, member : commands.MemberConverter, *, reason=None):
await member.kick(reason=reason)
await ctx.send(f"{member} has been kicked, reason: {reason}")
#kick.error
async def kick_error(self, ctx, error):
if isinstance(error, MissingPermissions):
await ctx.send("You don't have permission to kick members.")

Discord.py how to delete all roles in server

I'm trying to delete all roles in my discord server, but it takes a huge amount of time. So I decided to do this task with discord.py bot, but I'm getting this error:
discord.errors.HTTPException: 400 Bad Request (error code: 50028): Invalid Role
Here's my code:
#client.command()
async def delroles(ctx):
for role in ctx.guild.roles:
await role.delete()
The problem is that all users have an "invisible role" called #everyone, which is impossible to remove.
Do:
async def delroles(ctx):
for role in ctx.guild.roles:
try:
await role.delete()
except:
await ctx.send(f"Cannot delete {role.name}")
import discord
from discord.ext import commands
client = commands.Bot(command_prefix='>')
#client.event
async def on_ready():
print("Log : "+str(client.user))
#client.command()
async def delete(ctx):
for role in ctx.guild.roles:
try:
await role.delete()
except:
pass
client.run("token")

Unable to get an addrole command working in Discord.py

i've encountered an issue where I can't seem to get my addrole command to work. I've searched through every video and article and have spent hours to try and figure out why my code isn't working.
I want it so whenever a admin calls the ,addrole command, the user they mentioned gets that role.
(example: ,addrole #{rolename} {#user}).
Here is 2 sections of the code which I think may be the issue.
Here is the imports and some other things.
from discord.ext import commands
import random
from discord import Intents
import os
from discord import Embed
from discord.ext.commands import has_permissions
from discord.utils import get
from dotenv import load_dotenv
load_dotenv()
TOKEN = os.getenv("TOKEN")
intents = discord.Intents.default()
intents.members = True
client = commands.Bot(command_prefix=",", intents=intents)
Here is the command.
#commands.has_permissions(administrator=True)
async def addrole(ctx, role: discord.Role, user: discord.Member):
await user.add_roles(role)
await ctx.send(f"{user.mention} has successfully received the role {role.mention}.")
I've tried changing so many things but whenever I run it and call the command nothing happens.
Thanks.
Your command is not responding as it is missing client.command() which actually calls the command
#client.command()
#commands.has_permissions(administrator=True)
async def addrole(ctx, role: discord.Role, user: discord.Member):
await user.add_roles(role)
await ctx.send(f"{user.mention} has successfully received the role {role.mention}.")
It may also be better if your allow the role to have multiple arguments, which means you can add a role to a user with spaces, for example, This role instead of Thisrole, this can be added by adding * which creates an multiple string argument. You would then need to pass it as the last argument.
#client.command()
#commands.has_permissions(administrator=True)
async def addrole(ctx, user: discord.Member=None, *, role: discord.Role):
if user == None:
await ctx.send('Please format like ,addrole #member role')
return
await user.add_roles(role)
await ctx.send(f"{user.mention} has successfully received the role {role.mention}.")

discord.py Mention user by name

I am trying to mention a user by their name in discord.py. My current code is:
#bot.command(name='mention')
#commands.has_role(OwnerCommands)
async def mention(ctx, *, member: discord.Member):
memberid = member.id
await ctx.message.delete()
await ctx.send('<#{}>'.format(memberid))
But it does not work. How do I do this?
Edit, I found an answer.
It is rather straight forward member.mention
#bot.command(name='mention')
#commands.has_role(OwnerCommands)
async def mention(ctx, *, member: discord.Member):
await ctx.message.delete()
await ctx.send(member.mention)
We can skip the manual work and use discord's Member Model directly. The mention on the Member object returns us the string to allow us to mention them directly.
#bot.command(name='mention')
#commands.has_role(OwnerCommands)
async def mention(ctx, *, member: discord.Member):
await ctx.message.delete()
await ctx.send(member.mention)
Nevermind, I found a way. I simply turned on Privileged indents in the Discord Developers page.
Another solution if you don't want to mention the member twice by using discord.py MemberConverter extension, And also don't forget to activate the members privileged indents from the discord developer portal.
resources:
https://discordpy.readthedocs.io/en/rewrite/ext/commands/api.html#discord.ext.commands.MemberConverter
How to convert a string to a user discord.py
import discord
from discord.ext import commands
from discord.ext.commands import MemberConverter
intents = discord.Intents(members = True)
client = commands.Bot(command_prefix="$", intents=intents)
#client.command()
async def mention(ctx, member):
converter = MemberConverter()
member = await converter.convert(ctx, (member))
await ctx.send(member.mention)

Categories

Resources