我怎样才能让我只能在我的 Discord 机器人上使用某些命令? (Python)

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

所以我的 Discord 机器人现在可以将人踢出我的服务器。但是现在每个人都可以使用 kick 命令。我不想那样。我该如何解决这个问题?

这是目前的代码:

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

提前致谢!

如果还没有,请先导入 discord.ext.commands。 如果您只希望管理员能够踢,则在命令装饰器之后添加装饰器 @commands.has_permissions(administrator=True)。所有权限的列表在这里 https://discordpy.readthedocs.io/en/stable/api.html#discord.Permissions

旁注:discord.py项目已正式停产,所以我建议不要使用它。

我以前做过。我要救你! ;) jkjk

无论如何,先导入必要的模块

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

我假设您已经设置并定义了机器人。这是您应该替换为的全部代码:

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

我添加的只是@has_permissions(administrator=True)。你之前在这个 post 中得到的答案在 permission 之后错过了 s,所以它应该是 @has_permissions 而不是 @has_permission.

我希望这能奏效!让我知道它是否有效,并确保将此标记为正确答案:)