Discord bot kick command error message python - 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.")

Related

Why is this happening with discord.ext and replit?

I am trying to run discord.py but it is not working. This is the error
/home/runner/Mafia-Bot/venv/lib/python3.8/site-packages/nextcord/health_check.py:23: DistributionWarning: discord.py is installed which is incompatible with nextcord. Please remove this library by using `pip3 uninstall discord.py`
warn(message, DistributionWarning, stacklevel=0)
/home/runner/Mafia-Bot/venv/lib/python3.8/site-packages/nextcord/health_check.py:23: DistributionWarning: discord is installed which is incompatible with nextcord. Please remove this library by using `pip3 uninstall discord`
warn(message, DistributionWarning, stacklevel=0)
Traceback (most recent call last):
File "main.py", line 31, in <module>
async def Hello(ctx):
File "/home/runner/Mafia-Bot/venv/lib/python3.8/site-packages/discord/ext/commands/core.py", line 1262, in decorator
result = command(*args, **kwargs)(func)
File "/home/runner/Mafia-Bot/venv/lib/python3.8/site-packages/discord/ext/commands/core.py", line 1432, in decorator
raise TypeError('Callback is already a command.')
TypeError: Callback is already a command.
It is for a video game but is not connected.
Code:
import discord
from discord.ext import commands
import os
from discord import Member
from discord.ext.commands import MissingPermissions
from discord.ext.commands import has_permissions
intents = discord.Intents.default()
intents.members=True
client = commands.Bot(command_prefix="~$", intents=intents)
#######Events######
#client.event
async def on_ready():
print("Bot is up and ready!")
await client.change_presence(activity=discord.Game('ERLC'))
#client.event
async def on_member_join(member):
await member.send("Welcome to the mafia!")
######Commands#######
#client.command(pass_context=True)
#has_permissions(manage_roles=True)
#client.command()
async def Hello(ctx):
await ctx.send('Hi')
#client.command()
#has_permissions(kick_members=True)
async def kick(ctx, member: discord.Member, *, reason=None):
await member.kick(reason=reason)
await ctx.send(f"User {member} has been kicked.")
#kick.error
async def kick_error(ctx, error):
if isinstance(error, commands.MissingPermissions):
await ctx.send("You do not have permissions to kick.")
#client.command()
#has_permissions(ban_members=True)
async def ban(ctx, member: discord.Member, *, reason=None):
await member.send("You have been banned")
await member.ban(reason=reason)
await ctx.send(f"User {member} has been banned.")
#ban.error
async def ban_error(ctx, error):
if isinstance(error, commands.MissingPermissions):
await ctx.send("You do not have permissions to ban.")
client.run(os.getenv("TOKEN"))
Its probably easy to fix but I can't, I have only been coding for a year. I have to add alot of details but thats all I can say.

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

name 'create_dm' is not defined

How can I send messages to the users after they got kicked from servers? Whenever I try the following code, it doesn't work properly.
import discord
from discord.ext import commands
intents = discord.Intents(messages=True, guilds=True, reactions=True, members=True,presences=True,guild_messages=True)
client = commands.Bot(command_prefix="!dc ", intents=intents)
#client.event
async def on_ready():
print("I am ready!")
#client.command(aliases=["ban"])
#commands.has_role("admin")
async def ban_user(self,ctx, member: discord.Member, *, reason=None):
await member.ban(reason=reason)
await ctx.send(f"{member} has been kicked from server.")
dm_channel = await create_dm(member) #These two code lines are where I got this error
await dm_channel.send("You've been banned from the server.You won't join the server until admin opens your ban.")
#commands.command(aliases=["kick"])
#commands.has_role("admin")
async def kick_user(self,ctx, member: discord.Member, *, reason=None):
await member.kick(reason=reason)
await ctx.send(f"{member} has been kicked from the server.")
client.run(myToken)
According to your question, I am assuming that you want to dm the user who was banned. To do this you can,
async def ban(self, ctx, member: discord.Member, *, reason=None):
await ctx.send(f'{member} has banned from the server.') # sends the message in the server
await member.send(f'You have been banned from {member.guild.name}.') #dms the member that he has been banned.
await member.ban(reason = reason) #bans the user from the server.
Note: If you first ban the member, and then try to send the message in the server and dm, you will get an error as the member would not be found.
You can use the DMChannel function,
the code would look like this:
# put this on top:
from discord import DMChannel
# some stuff
async def ban_user(self, ctx, member: discord.Member=None, *, reason=None):
if member is None:
# if the user don't inform a name to ban, returns this message:
return await ctx.send(f'You need to inform which member you want to ban')
# bans the user from the server:
await member.ban(reason = reason)
# sends the message in the server:
await ctx.send(f'{member} has banned from the server.')
# dm the member
await DMChannel.send(member, f'You have been banned from {member.guild.name}.')

How do I fix this discord ban bot using python

I'm trying to make a discord bot that bans a user when I say .ban #Example_User, but when I run the code no errors pop up but then when I try to use the error appears, I've been using this tutorial https://www.youtube.com/watch?v=THj99FuPJmI, and here is my code and error below:
import discord
import random
import os
from discord.ext import commands
client = commands.Bot(command_prefix = '.')
#client.event
async def on_ready():
print("Bot is ready")
#client.command()
async def kick(ctx, member : discord.member, * , reason=None):
await member.kick(reason=reason)
#client.command()
async def ban(ctx, member : discord.Member, *, reason=None):
await member.ban(reason=reason)
client.run(os.getenv('TOKEN'))
and here is my error

Check Role Permission Discord.py

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!')

Categories

Resources