Skip to content
Advertisement

How can I make sue only I can use certain commands on my Discord bot? (Python)

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:

@bot.command(name="kick", aliases=["k"], help= "Kicks specified user from the server.")
async def kick(ctx, member: discord.Member, *, reason=None):
  await member.send(f"You have been kicked, reason: {reason}")
  await member.kick(reason=reason)
  await ctx.send(f'User {member} has been kicked.')
  embed=discord.Embed(
    title= f"{bot.user} kicked {member}",
    color=bot.embed_color,
    timestamp = datetime.datetime.now(datetime.timezone.utc)
  )
  embed.set_author(
    name = ctx.author.name,
    icon_url = ctx.author.avatar_url
  )
  embed.set_footer(
  text = bot.footer,
  icon_url = bot.footerimg
  )
  await bot.debug_channel.send(embed = embed)
  print("Sent and embed")
  await ctx.message.add_reaction('✅')
  print("Added a reaction to a message")

Thanks in advance!

Advertisement

Answer

I have done that before, this answer may help.

First, import the necessary modules first

import discord
from discord.ext import commands
from discord.ext.commands import has_permissions

I assume you already set up and defined the bot. Here’s the whole code that you should replace with:

@bot.command(name="kick", aliases=["k"], help= "Kicks specified user from the server.")
@has_permissions(administrator=True)
async def kick(ctx, member: discord.Member, *, reason=None):
  await member.send(f"You have been kicked, reason: {reason}")
  await member.kick(reason=reason)
  await ctx.send(f'User {member} has been kicked.')
  embed=discord.Embed(
    title= f"{bot.user} kicked {member}",
    color=bot.embed_color,
    timestamp = datetime.datetime.now(datetime.timezone.utc)
  )
  embed.set_author(
    name = ctx.author.name,
    icon_url = ctx.author.avatar_url
  )
  embed.set_footer(
  text = bot.footer,
  icon_url = bot.footerimg
  )
  await bot.debug_channel.send(embed = embed)
  print("Sent and embed")
  await ctx.message.add_reaction('✅')
  print("Added a reaction to a message")

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!

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement