【问题标题】:Discord.py How to get a Command to ignore an argumentDiscord.py 如何让命令忽略参数
【发布时间】:2020-12-24 15:47:28
【问题描述】:

所以我想做一个可以回复 .invites 或 .invites @user 的命令

当我以 .invites 身份运行此命令时,我的错误处理程序启动并说我需要一个 @user。

我尝试删除我的错误处理程序,但该命令什么也没做。

我怎样才能让这个命令双向工作?

@commands.command()
    async def invites(self, ctx, user: discord.Member):
        global last1
        global invites1
        try:
            userinvitecount = 0
            gld = self.bot.get_guild(int(guild_id))
            while True:
                invs = await gld.invites()
                tmp = []
                for i in invs:
                    if user.id == i.inviter.id:
                        top = i.uses
                        userinvitecount += int(top)

                    tmp.append(tuple((i.inviter.id, i.code, i.uses)))
                invites1 = tmp
                break
            usr = gld.get_member(int(user.id))
            eme = discord.Embed(description=f"{usr.mention} has {userinvitecount} invite's", color=0x03d692, title=" ")
            await ctx.send(embed=eme)
        except commands.MissingRequiredArgument:
            user = ctx.message.author.id
            userinvitecount = 0
            gld = self.bot.get_guild(int(guild_id))
            while True:
                invs = await gld.invites()
                tmp = []
                for i in invs:
                    if user.id == i.inviter.id:
                        top = i.uses
                        userinvitecount += int(top)

                    tmp.append(tuple((i.inviter.id, i.code, i.uses)))
                invites1 = tmp
                break
            usr = gld.get_member(int(user.id))
            eme = discord.Embed(description=f"{usr.mention} has {userinvitecount} invite's", color=0x03d692, title=" ")
            await ctx.send(embed=eme)
if isinstance(error, commands.MissingRequiredArgument):
        # generic error handler for commands entered wrong
        embed = discord.Embed(
            title="Failed",
            description="Failed to use command properly \n Please try again.",
            colour=discord.Colour.blurple(),
        )
        await ctx.send(embed=embed)

【问题讨论】:

    标签: python arguments discord bots


    【解决方案1】:

    您可以使用 默认 值来做到这一点。
    然后,在您的命令中,您可以简单地检查 user 是否为 None

    可以使用默认值,例如通过将用户设置为None,如下所示。

    @commands.command()
    async def invites(self, ctx, user: discord.Member = None):
    

    【讨论】:

    • ``` @commands.command() 异步 def 邀请(self,ctx,user:discord.Member = None):如果用户为 None:user = ctx.message.author ``` 工作完美谢谢
    猜你喜欢
    • 2018-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-19
    • 2014-04-04
    • 2019-11-26
    相关资源
    最近更新 更多