python 当我在制作discord bot禁止用户和踢用户是不正确的工作

5f0d552i  于 2023-04-19  发布在  Python
关注(0)|答案(1)|浏览(63)

当我在制作discord bot禁止用户和踢用户时,它不能正常工作,因为它可以被没有权限的用户踢

@bot.tree.command(name="kick", description="Kicking Other Users With My Hammer Tool")
@commands.has_permissions(kick_members=True)
async def kick(ctx, 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 = 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)
        await channel.send(embed=embed)
    except discord.Forbidden:
        await ctx.send("Ü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")
@commands.has_permissions(ban_members=True)  # Ban komutunu sadece ban_members yetkisi olan kullanıcıların kullanabilmesini sağlar
async def ban(ctx, member: discord.Member, reason: str = None):
  if ctx.author.guild_permissions.kick_members:
    if not reason:
      reason = "Not reason Given (İlyax's Turpin mod bot)"
    await member.ban(reason=reason)

    embed = Embed(title="Banning",
                color=discord.Color.red())  # Embed'in rengi (isteğe
    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.")  # Embed'in alt bilgisi
    await ctx.response.send_message(embed=embed)
    channel_id = 1038757871246127134
    channel = bot.get_channel(channel_id)
    await channel.send(embed=embed)`

我踢和班宁其他用户在不和谐

vohkndzv

vohkndzv1#

@commands.has_permissions()仅适用于Bot. command。
在这里,您使用的是app_commands.CommandTree.command(application / slash command),它使用不同的检查app_commands.checks.has_permissions。

@bot.tree.command(name="kick", description="Kicking Other Users With My Hammer Tool")
@app_commands.checks.has_permissions(kick_members=True)
async def kick(interaction, member: discord.Member, reason: str = None):
    ...

@bot.tree.command(name="ban", description="Banning Other Users With My Hammer Tool")
@app_commands.checks.has_permissions(ban_members=True)  # Ban komutunu sadece ban_members yetkisi olan kullanıcıların kullanabilmesini sağlar
async def ban(interaction, member: discord.Member, reason: str = None):
    ...

另外,你不应该把discord.Interaction当作ctx来使用,因为discord.Interaction和discord.ext.commands.Context有很大的不同。ctx.author变成了interaction.user。只要你正确使用它,它仍然可以工作,但是正确命名变量是一个很好的习惯。

相关问题