I find that I am unable to kick members or other bots using the kick command in my bot. Both I and the bot have administrator permissions. Why could this be happening? I get no compilation errors.
#client.command() ##kick
#has_permissions(kick_members = True) # to check the user itself
async def kick(ctx, member : discord.Member, *, reason=None):
try:
await member.kick(reason=reason)
await ctx.send(+member.mention + " has been sent to the ministry of love for reeducation.")
except:
await ctx.send("You cannot!")
EDIT: Thanks for the fixes in the comments below. I also came to the realization that I was trying to kick a user of equal level (deprecated bot) and so that also played a part in it not working. I tried to kick a standard user and it worked great!
Guessing that the message received from the bot is "You cannot!", the exception thrown is a TypeError, and it is handled because you have wrapped it in try/except. To make sure, remove the error handling and check your result.
The complete error would look like this: TypeError: bad operand type for unary +: 'str'.
Resources
TypeError
Bug
In line 6, you have added a bad binary operator + in the beginning of argument list, but the operator requires two operands and they are not provided.
await ctx.send(+member.mention + " has been sent to the ministry of love for reeducation.")
Hence a TypeError is thrown, but it is also handled, so the only result you will see is the "You cannot!" message from the bot.
Fix
Simply remove the bad operator, and it should be working fine.
await ctx.send(member.mention + " has been sent to the ministry of love for reeducation.")
In the #has_permissions(kick_members = True) line, you have to add commands. Here's the fixed command.
#client.command()
#commands.has_permissions(kick_members=True)
async def kick(ctx, member: discord.Member, *, reason=None):
try:
await member.kick(reason=reason)
await ctx.send(+member.mention + " has been sent to the ministry of love for reeducation.")
except:
await ctx.send("You cannot!")
import discord
from discord.ext import commands
import os
client = commands.Bot(command_prefix = '!')
#client.command()
#commands.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')
await member.send(f"You have been kicked from {member.guild.name} | reason: {reason}")
client.run('WRITE YOUR TOKEN HERE')
Related
I am trying to make a discord.py bot with a kick command. So when you do ;kick (member) it will kick the mentioned user and send that user a dm telling them they are kicked from the server. However it only dms the user if the dm is a normal message and not embed. I'm still pretty new to python so I don't understand whats wrong with the code. Here is my code:
#commands.guild_only()
#has_permissions(kick_members = True)
async def kick(self, ctx, member : discord.Member=None, *, reason="No reason was provided"):
aA=ctx.author.avatar_url
desc1=f"You are missing the following argument(s): `Member`\n```{prefix}kick <member> [reason]```"
embed1=discord.Embed(title="Missing Argument",description=desc1,color=rC)
embed1.set_author(name="Error",icon_url=aA)
try:
if member == None:
await ctx.send(embed=embed1)
return
if member == ctx.author:
await ctx.send(f"You can't kick yourself.")
return
try:
em1=discord.Embed(description=f"You were kicked out from **{ctx.guild.name}**\nReason: `{reason}`.",color=rC)
em1.set_author(icon_url=aA)
await member.send(embed=em1)
except:
pass
em2=discord.Embed(description=f"{tick} {member.mention} has been kicked out of **{ctx.guild.name}**. Reason: `{reason}`",color=gC)
await member.kick(reason = reason)
await ctx.send(embed=em2)
except:
await ctx.send("An unknown error occured.)
There isn't any errors sent in the console when I run the bot or use the command
you can use:
await member.send(embed=em2)
ctx will send in the channel where the command was done.
This is the code for my kick command
#bot.command()
#commands.has_permissions(kick_members=True)
async def kick(context, member : discord.Member = None, *, reason=None):
if user == None:
embed = discord.Embed(title=f"**Kick**",
description=f"Kicks a member"
f"\nUsage: -kick <user> <reason>")
await ctx.send(embed=embed)
await member.kick(reason=reason)
embed = discord.Embed(description = f"Kicked {member}", color = 0xf30e0e)
embed.add_field(name='Reason', value=f'{reason}')
await ctx.message.add_reaction('✅')
await ctx.send(embed=embed)
#kick.error
async def kick_error(ctx, error):
if isinstance(error, commands.MissingPermissions):
await ctx.send("You do not have the permissions to run this command")
When i use it it doesnt work and yet theres no errors, can someone explain why this isnt working?
I would sincerely recommend you learn Python before you learn discord.py
#bot.command()
#commands.has_permissions(kick_members=True)
async def kick(ctx, member : discord.Member = None, *, reason=None):
if user == None:
embed = discord.Embed(
title="**Kick**",
description="Kicks a member\nUsage: -kick <user> <reason>"
)
await ctx.send(embed=embed)
await member.kick(reason=reason)
embed = discord.Embed(description = f"Kicked {member}", color = 0xf30e0e)
embed.add_field(name='Reason', value=f'{reason}')
await ctx.message.add_reaction('✅')
await ctx.send(embed=embed)
I'm not 100 percent sure if it was the issue as I cannot test, but you did not add "ctx" to your function, instead putting "context." You also had a lot of weird indentation, and (this doesn't cause any issues) random F strings for no reason.
You also were not getting any errors because your error catching function blocks all errors except a permission error, in which it logs in Discord rather than your console.
Like other's have said, you're only allowing the bot to send a message if an error is a missing permission. Try adding the following:
#kick.error
async def kick_error(ctx, error):
if isinstance(error, commands.MissingPermissions):
await ctx.send("You do not have the permissions to run this command")
elif isinstance(error, commands.CommandError): # If the command fails...
print(error) # Print the error in the console
Then, try solving the error you receive in the console. :)
I have a command in my bot that allows a user to dm an anonymous message to me. However, when I tested the command my bot only sends the first word of the message to me.
#commands.command(name='msgetika', aliases=['msgmax'])
async def msgetika(self, ctx, message1=None):
'''Send an annonymous message to discord'''
if message1 is None:
await ctx.send('Please specify a message')
maxid = await self.bot.fetch_user('643945264868098049')
await maxid.send('Anonymous message: ' + message1)
msg = await ctx.send('Sending message to max.')
await ctx.message.delete()
await asyncio.sleep(5)
await msg.delete()
You need to add a * if you want to catch the entire phrase. View the docs here about using this and have a look at the example working beneath the code.
#commands.command(name='msgetika', aliases=['msgmax'])
async def msgetika(self, ctx, *, message1=None):
Also, unless you are using an older version of discord.py, user ids are ints.
maxid = await self.bot.fetch_user(USER ID HERE)
Someone asked me to make a bot for him that sends a DM to anyone he specifies through a command, like *send_dm #Jess#6461 hello.
I've searched alot and I came across this code:
async def send_dm(ctx,member:discord.Member,*,content):
await client.send_message(member,content)
but then I got the error:
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'Bot' object has no attribute 'send_message'
I want to type for example : *send_dm #Jess#6461 hello and the bot sends a DM saying "hello" to that user.
client.send_message() has been replaced by channel.send() in the version 1 of discord.py
You can use Member.create_dm() to create a channel for sending messages to the user
async def send_dm(ctx, member: discord.Member, *, content):
channel = await member.create_dm()
await channel.send(content)
HI there hope this help you
client.command(aliases=['dm'])
async def DM(ctx, user : discord.User, *, msg):
try:
await user.send(msg)
await ctx.send(f':white_check_mark: Your Message has been sent')
except:
await ctx.send(':x: Member had their dm close, message not sent')
change you client if you use any other str
The easiest way to do is:
async def send_dm(ctx,member:discord.Member,*,content):
await member.send(content)
I've been programming a bot with discord.py (the rewrite branch) and I want to add a ban command. The bot still doesn't ban the member, it just shows an error:
#client.command(aliases=['Ban'])
async def ban(ctx,member: discord.Member, days: int = 1):
if "548841535223889923" in (ctx.author.roles):
await client.ban(member, days)
await ctx.send("Banned".format(ctx.author))
else:
await ctx.send("You don't have permission to use this command.".format(ctx.author))
await ctx.send(ctx.author.roles)
It'll ban the pinged user and confirm that it did ban
Member.roles is a list of Role objects, not strings. You can use discord.utils.get to search through that list using the id (as an int).
from discord.utils import get
#client.command(aliases=['Ban'])
async def ban(ctx, member: discord.Member, days: int = 1):
if get(ctx.author.roles, id=548841535223889923):
await member.ban(delete_message_days=days)
await ctx.send("Banned {}".format(ctx.author))
else:
await ctx.send("{}, you don't have permission to use this command.".format(ctx.author))
await ctx.send(ctx.author.roles)
There also is no longer a Client.ban coroutine, and the additional arguments to Member.ban must be passed as keyword arguments.
async def ban(ctx, member: discord.Member=None, *, reason=None):
if reason:
await member.send(f'You got banned from {ctx.guild.name} by {ctx.author}, reason: ```{reason}```')
await member.ban()
await ctx.send(f'{member.mention} got banned by {ctx.author.mention} with reason: ```{reason}```')
if reason is None:
await ctx.send("please specify a reason")