如何为discord.pylash-commands命令添加权限?斜杠命令中没有@has_permissions()。
@slash.slash(
name="kick",
description="Kicks member from the server",
options=[manage_commands.create_option(
name = "member",
description = "Who do you want to kick?",
option_type = 3,
required = True
),
manage_commands.create_option(
name = "reason",
description = "What is the reason?",
option_type = 3,
required = False
)])
async def _kick(ctx, member: discord.Member, reason='Unspecified'):
await ctx.send(0)
await member.kick(reason=reason)
e=discord.Embed(title="🔨 Banhammer has spoken", color=0xfa2d4c)
e.add_field(name='Kick', value=f' :white_check_mark: Kicked {member} for `{reason}`', inline=False)
await ctx.channel.send(embed=e)```
由于目前没有 (discord-py-slash-command)[https://pypi.org/project/discord-py-slash-command/] 的权限检查,您可以在代码中检查成员是否有某个权限,如下图
@slash.slash(name="cool_command")
async def cool_command(ctx, option):
if not ctx.author.guild_permissions.manage_messages: # replace this with your desired permission
return # tell them they dont have permissions or raise an exception
... # do your stuff here
Discord.py 尚不支持斜杠命令。 同时,您可以使用 discord-py-slash-command 来使用斜线命令。 希望这有帮助
好吧,我尝试使用
@commands.has_permissions()
,就我而言它仍然有效。您可以使用测试命令尝试一下,它应该响应:
互动失败
如果您不是这样,请告诉我
现在,当您生成 ouath2 令牌来邀请机器人时,有斜杠命令权限,而不是仅添加“bot”,添加“bot”和“applications.commands”
Discord.py 没有实际支持斜杠命令,只有 api,但这里有一个不同的下载:
下
@slash.slash(name=“Kick”, description=“Kicks the member”)
添加:
@commands.has_permissions(kick_members=True)
就这样吧。应该有效
使用 dpy 2.0 执行斜杠命令
您需要在开发人员门户中注册命令 和 使其具有应用程序命令范围才能正常工作。
from discord.ext import commands
from discord import app_commands
bot = commands.Bot(prefix="...")
@bot.event
async def on_ready():
bot.tree.sync(guild=discord.Object(id=...)) # id is guild id(s)
# Global bot.tree.sync()
...
@bot.tree.command(name="num")
async def num(self, inter, number : app_commands.Range[int,10,12]):
await interaction.response.send_message(f'Your value is {value}', ephemeral=True)
类方法
from discord.ext import commands
class Bot:
def __init__(self, ...):
...
async def on_ready(self): # Not recommended
await self.tree.sync(guild=discord.Object(id=...))
...
齿轮:
from discord import app_commands
from discord.ext import commands
class Cog(commands.Cog):
def __init__(self, bot):
self.bot = bot
@app_commands.command(name="num")
async def num(self, inter, number : app_commands.Range[int,10,12]):
await interaction.response.send_message(f'Your value is {value}', ephemeral=True)
async def setup(bot):
bot.add_cog(Cog(bot))
使用@discord.app_commands.checks.has_permissions(这里输入你的权限),但是需要导入 from discord import app_commands
@slash.slash(
name="kick",
description="Kicks member from the server",
options=[manage_commands.create_option(
name = "member",
description = "Who do you want to kick?",
option_type = 3,
required = True
),
manage_commands.create_option(
name = "reason",
description = "What is the reason?",
option_type = 3,
required = False
)])
@discord.app_commands.checks.has_permissions(manage_roles=True)
async def _kick(ctx, member: discord.Member, reason='Unspecified'):
await ctx.send(0)
await member.kick(reason=reason)
e=discord.Embed(title="🔨 Banhammer has spoken", color=0xfa2d4c)
e.add_field(name='Kick', value=f' :white_check_mark: Kicked {member} for `{reason}`', inline=False)
await ctx.channel.send(embed=e)```