【问题标题】:Discord.py Ban Command doesn't worksDiscord.py Ban 命令不起作用
【发布时间】:2021-01-20 14:08:51
【问题描述】:

大家好,我正在用 python 编写一个不和谐的机器人,我有禁止和踢命令。那么主要问题是这些命令不起作用!我不知道为什么!请帮忙,这是我的代码:

@client.command(aliases=['Kick','KICK','KİCK'])
@commands.has_permissions(kick_members=True)
async def kick(ctx, member : discord.Member, *, reason=None):
    await member.kick(reason=reason)
    await ctx.send(f"{member.mention} is kicked.")

@kick.error
async def kick_error(ctx, error):
    if isinstance(error, commands.MissingPermissions):
        await ctx.send("Sorry, but if you want to use this command you need to have **Kick Members** permission.")

@client.command(aliases=['Ban','BAN'])
@commands.has_permissions(ban_members=True)
async def ban(ctx, member : discord.Member, *, reason=None):
    await member.ban(reason=reason)
    await ctx.send(f'{member.mention} is banned.')

#ban someone in a server error
@ban.error
async def ban_error(ctx, error):
    if isinstance(error, commands.MissingPermissions):
        await ctx.send("Sorry, but if you want to use this command you need to have **Ban Members** permission.")

@client.command(aliases=['Unban','UNBAN'])
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'{user.mention} is unbanned.')
            return

#unban someone in a server error
@unban.error
async def unban_error(ctx, error):
    if isinstance(error, commands.MissingPermissions):
        await ctx.send("Sorry, but if you want to use this command you need to have **Ban Members** permission.

【问题讨论】:

  • 你有 on_message 吗?

标签: python discord.py discord.py-rewrite


【解决方案1】:

编辑:不要忘记导入以下内容:

import discord
import os
from discord.ext import commands,tasks
from discord.ext.commands import has_permissions, CheckFailure

 @commands.command()
    @commands.has_permissions(ban_members=True)
    async def bAn(ctx,*, member : discord.Member = None, reason=None):
        if member is None:
            await ctx.send("Please mention someone to ban")
        if reason is None:
            reason = "Reason was not specified"
        await ctx.send(f'{member.mention} is banned.')
        await member.ban(reason=reason)      # could use ctx.guild.ban(member, reason=reason) here, works the same way though.

上述命令是对您的禁令命令的重做。而不是client.commands(),我使用commands.command()

并将 Member 声明为 None 并指定一个条件,如果没有提及任何成员,bot 会要求用户提及用户 如果未提及原因,则默认原因为“未指定原因” kick 命令遵循与 ban 相同的模式。

另外,对于解禁,您可以使用以下内容

@commands.command(aliases=['ub'])
    @commands.has_guild_permissions(ban_members=True)
    @commands.bot_has_permissions(ban_members=True)
    async def unban(ctx, member: discord.User = None, *, reason=None): # discord.User allows you to use discord ID/discordname#discriminator (674670744776474644 or Mystery R#6892)

if reason is None:
          reason = f"{ctx.author.name}#{ctx.author.discriminator} did not provide any reason"
if member is None:
          await ctx.send("Please write a ID#discriminator to unban")
x = len(reason)   
if x > 460: # 460 is the character limit of the reason in discord
          return await ctx.send('Reason must be less or equal to 460 characters')
else:
          await ctx.guild.unban(member, reason=reason) # add the response from bot after members are unbanned yourself after this line

@unban.error
async def unban_error(self,ctx, error): 
    if isinstance(error, commands.MemberNotFound):
                    await ctx.send("No member was found with the given argument")
    elif isinstance(error, commands.BotMissingPermissions):
                    await ctx.send("Bot is missing Ban Members permission(s) to run this command.")
    elif isinstance(error,commands.MissingPermissions):
                    await ctx.send("You are missing Ban Members permission(s) to run this command.")

注意:命令“ben”是禁令,我将其改为“ben”,因为我已经在另一个文件中有一个禁令命令!

【讨论】:

  • 你遇到了什么问题?
  • 它没有给出任何错误?通过不和谐的 AquaRose#6892 联系我
【解决方案2】:

我认为您在论点中有成员:discord.Member,但不和谐提供了成员提及。至少我正在获取 member.mention,然后遍历公会中的成员列表,将 member.mention 与提供的 member.mention 进行比较,然后我拥有该成员,我可以禁止该成员。希望你能理解

【讨论】:

  • 我不明白?
  • 不,他使用的转换器已经给了他一个discord.Member实例,没有必要从中取出mention字符串,遍历公会中的每个成员,然后尝试找到匹配的那个:他已经拥有 Member 实例。完全没有必要将它从members 列表中拉出来,它已经存在了。如果你还没有member,你就不能做member.mention,所以你发现它自己使用...?
  • 我就是这样使用它,它可以工作,但我使用的技术略有不同
猜你喜欢
  • 2021-11-19
  • 2021-01-05
  • 1970-01-01
  • 2020-08-16
  • 2019-07-23
  • 1970-01-01
  • 2021-07-27
  • 2022-01-24
  • 2021-03-24
相关资源
最近更新 更多