【问题标题】:How do you make a ban/unban/kick command with nextcord?你如何用 nextcord 发出禁令/取消禁令/踢命令?
【发布时间】:2022-07-16 00:30:32
【问题描述】:

我想用我的 discord 机器人发出禁令、踢和取消禁令命令。 我还希望它与斜杠命令兼容。 如果您需要代码,现在就在这里。

from nextcord.ext import commands

bot = commands.Bot()

@bot.event
async def on_message(message):
    if message.author != bot.user:
        print(f'(#{message.channel}) {message.author}: {message.content}')
        
@bot.event
async def on_message_delete(message):
    print(f'User {message.author} has deleted "{message.content}"')

@bot.event
async def on_message_edit(before, after):
    print(f'User {before.author} has edited "{before.content}" to "{after.content}"')

bot.run('my token')

【问题讨论】:

  • I want to make a ban, kick, and unban command with my discord bot. I also want it to be compatible with slash commands → 好的,我们允许。请做出来。什么是实际问题?你想做什么?
  • @rzlvmp 问题是如何将这些命令添加到我的代码中。我正在尝试添加它们

标签: python nextcord


【解决方案1】:

对于踢命令,您可以执行以下操作:

@bot.command()
@commands.has_permissions(kick_members=True)
async def kick(ctx, user: discord.Member = None, *, reason=None):
    if user == None:
        await ctx.send("Please enter a user!")
        return

    await user.kick(reason=reason)
    await ctx.send(f'Kicked {user.name} for reason {reason}')

对于禁止命令,您可以执行以下操作:

@bot.command()
@commands.has_permissions(ban_members=True)
async def ban(ctx, user: discord.Member = None, *, reason=None):
    if user == None:
        await ctx.send("Please enter a user!")
        return

    await user.ban(reason=reason)
    await ctx.send(f'Banned {user.name} for reason {reason}')

对于解除禁令,您可以执行以下操作:

@bot.command()
@commands.has_permissions(ban_members=True)
async def unban(ctx, *, member):
    banned_users = await ctx.guild.bans()
    member_name, member_discriminator = member.split("#")

    for ban_entry in banned_users:
        user = ban_entry.user

        if (user.name, user.discriminator) == (member_name, member_discriminator):
            await ctx.guild.unban(user)
            await ctx.send(f'Unbanned {user.name}#{user.discriminator}')

我不太确定如何执行 / 命令,但我希望这会有所帮助!

【讨论】:

    【解决方案2】:

    只需将@bot.command() 更改为@bot.slash_command() 即可获得斜线命令

    【讨论】:

      猜你喜欢
      • 2019-10-17
      • 2020-11-18
      • 2020-10-20
      • 1970-01-01
      • 2021-05-14
      • 2019-07-23
      • 1970-01-01
      • 1970-01-01
      • 2021-10-06
      相关资源
      最近更新 更多