Discord.py重写bot无法响应普通命令

时间:2020-06-03 07:47:06

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

我的命令正常,但是出现以下错误:Command "leave" is not found。这是我的代码: 我以前使用过反应脚本,所以我知道不是那样的。

@bot.command
async def leave(ctx):
    user1 = await bot.fetch_user(ctx.message.author.id)
    message = await ctx.send(f"Are you sure that you want to leave {ctx.message.guild}, {ctx.message.author.mention}? React within 30 seconds to verify")
    for emoji in ('✅'):
        await message.add_reaction(emoji)

        try:
            def check(rctn, user):
                return user.id == ctx.author.id and str(rctn) == '✅'

            rctn, user = await bot.wait_for("reaction_add", check=check, timeout=30)

            member = ctx.message.author
            await member.kick()
            user = bot.get_user(ctx.message.author.id)
            link = await ctx.channel.create_invite(unique=True, max_uses=1)
            await user.send(f'You left {ctx.message.guild}! But you can rejoin via {link}')

        except asyncio.TimeoutError:
            await ctx.send(f"**Error:** Not reacted in time")

有人看到错误吗?我认为我似乎找不到它,因为它应该可以工作。

2 个答案:

答案 0 :(得分:1)

正如Tin Nguyen所说, bot.command(),而不是bot.command中的@bot.event

答案 1 :(得分:1)

正确的命令修饰符为@bot.command(),文档建议您使用get_user()而不是fetch_user(),因为后者是API调用。另外,您不能user.send()使用DM,而不能使用user.dm_channel.send()

@bot.command()
async def leave(ctx):
    user1 = await bot.get_user(ctx.message.author.id)
    message = await ctx.send(f"Are you sure that you want to leave {ctx.message.guild}, {ctx.message.author.mention}? React within 30 seconds to verify")
    for emoji in ('✅'):
        await message.add_reaction(emoji)

        try:
            def check(rctn, user):
                return user.id == ctx.author.id and str(rctn) == '✅'

            rctn, user = await bot.wait_for("reaction_add", check=check, timeout=30)

            member = ctx.message.author
            await member.kick()
            user = bot.get_user(ctx.message.author.id)
            link = await ctx.channel.create_invite(unique=True, max_uses=1)
            await user.dm_channel.send(f'You left {ctx.message.guild}! But you can rejoin via {link}')

        except asyncio.TimeoutError:
            await ctx.send(f"**Error:** Not reacted in time")