You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Discord机器人踢人/封禁功能异常:无权限用户可执行踢人求助

Discord机器人踢人/封禁功能权限验证失效问题解决

问题

开发Discord机器人的踢人(kick)和封禁(ban)斜杠命令时,发现无权限的用户也能执行这些操作,权限校验完全失效。

原因分析

你使用的@commands.has_permissions()装饰器是针对传统前缀命令设计的,无法作用于Discord的斜杠命令(Application Commands),所以权限校验完全不起作用。另外,ban命令里的手动权限判断if ctx.author.guild_permissions.kick_members逻辑错误:判断的是踢人权限而非封禁权限,且手动判断不如装饰器规范可靠。

修复方案

  1. 导入discord.app_commands模块(若未导入)
  2. 将@commands.has_permissions()替换为斜杠命令专用的@app_commands.has_permissions()装饰器
  3. 移除ban命令中错误的手动权限判断,统一用装饰器处理
  4. 斜杠命令的响应必须使用ctx.response.send_message(),而非传统的ctx.send()

修复后的完整代码

import discord
from discord import app_commands
from discord.ext import commands

# 初始化Bot实例(需确保开启成员意图)
intents = discord.Intents.default()
intents.members = True
bot = commands.Bot(command_prefix="!", intents=intents)

@bot.tree.command(name="kick", description="Kicking Other Users With My Hammer Tool")
@app_commands.has_permissions(kick_members=True)  # 替换为斜杠命令专用权限装饰器
async def kick(ctx: discord.Interaction, member: discord.Member, reason: str = None):
    """Kick a member from the server."""
    if not reason:
        reason = "No reason given."
    try:
        await member.kick(reason=reason)
        embed = discord.Embed(title="Kicking", color=discord.Color.red())
        embed.add_field(name="Kick Users", value=f"This User: {member.mention} is kicked for: ``{reason}``", inline=False)
        embed.set_footer(text="https://turpincheat.glitch.me  --  İlyax.")
        await ctx.response.send_message(embed=embed)
        channel_id = 1038757871246127134
        channel = bot.get_channel(channel_id)
        if channel:  # 增加频道存在性判断,避免报错
            await channel.send(embed=embed)
    except discord.Forbidden:
        await ctx.response.send_message("Üzgünüm, bu işlemi gerçekleştirmek için yeterli yetkiye sahip değilim.")

@bot.tree.command(name="ban", description="Banning Other Users With My Hammer Tool")
@app_commands.has_permissions(ban_members=True)  # 替换为斜杠命令专用权限装饰器
async def ban(ctx: discord.Interaction, member: discord.Member, reason: str = None):
    if not reason:
        reason = "Not reason Given (İlyax's Turpin mod bot)"
    try:
        await member.ban(reason=reason)
        embed = discord.Embed(title="Banning", color=discord.Color.red())
        embed.add_field(
            name="Ban Users",
            value=f"This User : {member.mention} is banned for : ``{reason}``",
            inline=False)
        embed.set_footer(text="https://turpincheat.glitch.me  --  İlyax.")
        await ctx.response.send_message(embed=embed)
        channel_id = 1038757871246127134
        channel = bot.get_channel(channel_id)
        if channel:
            await channel.send(embed=embed)
    except discord.Forbidden:
        await ctx.response.send_message("Üzgünüm, bu işlemi gerçekleştirmek için yeterli yetkiye sahip değilim.")

# 启动时同步斜杠命令到服务器
@bot.event
async def on_ready():
    await bot.tree.sync()
    print(f"Logged in as {bot.user}")

额外注意事项

  • 修改权限装饰器后,必须通过bot.tree.sync()重新同步命令到服务器才能生效
  • 斜杠命令的上下文为discord.Interaction,所有响应操作需使用ctx.response相关方法
  • 增加频道存在性判断可避免因频道删除、ID错误导致的运行报错

内容的提问来源于stack exchange,提问作者İlyax

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 07:18:10