So my Discord bot can now kick people from my server. But right now everyone can use the kick command. And I don’t want that. How can I fix this?
This is the code so far:
JavaScript
x
23
23
1
@bot.command(name="kick", aliases=["k"], help= "Kicks specified user from the server.")
2
async def kick(ctx, member: discord.Member, *, reason=None):
3
await member.send(f"You have been kicked, reason: {reason}")
4
await member.kick(reason=reason)
5
await ctx.send(f'User {member} has been kicked.')
6
embed=discord.Embed(
7
title= f"{bot.user} kicked {member}",
8
color=bot.embed_color,
9
timestamp = datetime.datetime.now(datetime.timezone.utc)
10
)
11
embed.set_author(
12
name = ctx.author.name,
13
icon_url = ctx.author.avatar_url
14
)
15
embed.set_footer(
16
text = bot.footer,
17
icon_url = bot.footerimg
18
)
19
await bot.debug_channel.send(embed = embed)
20
print("Sent and embed")
21
await ctx.message.add_reaction('✅')
22
print("Added a reaction to a message")
23
Thanks in advance!
Advertisement
Answer
I have done that before, this answer may help.
First, import the necessary modules first
JavaScript
1
4
1
import discord
2
from discord.ext import commands
3
from discord.ext.commands import has_permissions
4
I assume you already set up and defined the bot. Here’s the whole code that you should replace with:
JavaScript
1
24
24
1
@bot.command(name="kick", aliases=["k"], help= "Kicks specified user from the server.")
2
@has_permissions(administrator=True)
3
async def kick(ctx, member: discord.Member, *, reason=None):
4
await member.send(f"You have been kicked, reason: {reason}")
5
await member.kick(reason=reason)
6
await ctx.send(f'User {member} has been kicked.')
7
embed=discord.Embed(
8
title= f"{bot.user} kicked {member}",
9
color=bot.embed_color,
10
timestamp = datetime.datetime.now(datetime.timezone.utc)
11
)
12
embed.set_author(
13
name = ctx.author.name,
14
icon_url = ctx.author.avatar_url
15
)
16
embed.set_footer(
17
text = bot.footer,
18
icon_url = bot.footerimg
19
)
20
await bot.debug_channel.send(embed = embed)
21
print("Sent and embed")
22
await ctx.message.add_reaction('✅')
23
print("Added a reaction to a message")
24
What I added was simply @has_permissions(administrator=True)
. The answer you got previously in this post missed the s after permission, so it should be @has_permissions
instead of @has_permission
.
I hope this works! Let me know if you it works!