首页 > 解决方案 > Discord Python 重写 - Reaction 帮助 (Cog)

问题描述

我正在尝试提供依赖于反应的帮助,我得到了这段代码

import discord
from discord import Embed
from discord.ext import commands

class Helptest(commands.Cog):
    def __init__(self, client):
        self.client = client

    @commands.command()
    async def helpreee(self, ctx):

        await ctx.message.delete()

        msg = await ctx.send("Eh idk just react")

        await msg.add_reaction("⬅️")
        await msg.add_reaction("➡️")

        def check(reaction, user):
            return user == ctx.message.author and str(reaction.emoji) in ['⬅️', '➡️']

        try:

            reaction, user = await client.wait_for('reaction_add', timeout=5, check=check)

            if reaction.emoji == '➡️':

                await ctx.message.delete()

                await msg.reaction.clear()

                msg1 = await msg.edit("Hewwo")

                await msg1.add_reaction("⬅️")

                reaction, user = await client.wait_for('reaction_add', timeout=5, check=check)

                if reaction.emoji == '⬅️':
                        
                    await msg.edit("Eh idk just react")
                    return

            elif reaction.emoji == '⬅️':
                await ctx.send("AAA")

            except asyncio.TimeoutError:
                await ctx.send("Timed out")

    @helpreee.error
    async def helpreee_error(self, ctx, error):
        await ctx.send(error)
        print(error)
        raise error

def setup(client):
    client.add_cog(Helptest(client))

但它没有用,我得到一个错误。

错误是:

Traceback(最近一次调用最后一次):文件“C:\Users\PC\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\bot.py”,第 607 行,在_load_from_module_spec spec.loader.exec_module(lib) 文件“”,第 779 行,在 exec_module 文件中“”,第 916 行,在 get_code 文件中“”,第 846 行,在 source_to_code 文件中“”,第 219 行,在 _call_with_frames_removed 文件中“C:\ Users\PC\Desktop\Code\Waifu Bot\cogs\testhelp.py",第 22 行尝试:^ IndentationError: unindent does not match any external indentation level

上述异常是以下异常的直接原因:

Traceback(最近一次调用最后一次):文件“C:\Users\PC\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\client.py”,第 312 行,在 _run_event await coro( *args, **kwargs) 文件“C:\Users\PC\Desktop\Code\Waifu Bot\setup.py”,第 95 行,on_ready raise e 文件“C:\Users\PC\Desktop\Code\Waifu Bot \setup.py”,第 92 行,on_ready client.load_extension(cog) 文件“C:\Users\PC\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\ bot.py”,第 664 行,在 load_extension self._load_from_module_spec(spec, name) 文件“C:\Users\PC\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands \bot.py",第 610 行,在 _load_from_module_spec 中引发错误。ExtensionFailed(key, e) from e discord.ext.commands.errors.ExtensionFailed: Extension 'cogs.testhelp' 引发错误:IndentationError: unindent does not match any external indentation level (testhelp.py, line 22)

标签: discorddiscord.pydiscord.py-rewrite

解决方案


这只是一个简单的缩进错误(at try...except):

import discord
from discord import Embed
from discord.ext import commands

class Helptest(commands.Cog):
    def __init__(self, client):
        self.client = client

    @commands.command()
    async def helpreee(self, ctx):
        await ctx.message.delete()
        msg = await ctx.send("Eh idk just react")
        await msg.add_reaction("⬅️")
        await msg.add_reaction("➡️")

        def check(reaction, user):
            return user == ctx.message.author and str(reaction.emoji) in ['⬅️', '➡️']

        try:
            reaction, user = await client.wait_for('reaction_add', timeout=5, check=check)
            if reaction.emoji == '➡️':
                await ctx.message.delete()
                await msg.reaction.clear()
                msg1 = await msg.edit("Hewwo")
                await msg1.add_reaction("⬅️")
                reaction, user = await client.wait_for('reaction_add', timeout=5, check=check)
                if reaction.emoji == '⬅️':
                    await msg.edit("Eh idk just react")
                    return
            elif reaction.emoji == '⬅️':
                await ctx.send("AAA")
        except asyncio.TimeoutError: #Indent error here, delete one tabulation
            await ctx.send("Timed out") #Also Delete one tabulation here

    @helpreee.error
    async def helpreee_error(self, ctx, error):
        await ctx.send(error)
        print(error)
        raise error

def setup(client):
    client.add_cog(Helptest(client))

PS:为了避免缩进错误,避免在代码行之后跳过一行,这样更容易检测到这些错误,你的代码也会更容易理解^^


推荐阅读