Discord Python重写-反应帮助(Cog)

时间:2020-09-15 09:59:30

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

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

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))

但是它不起作用,我得到一个错误。

错误是:

回溯(最近通话最近):文件 “ C:\ Users \ PC \ AppData \ Local \ Programs \ Python \ Python38-32 \ lib \ site-packages \ discord \ ext \ commands \ bot.py”, _load_from_module_spec中的第607行 spec.loader.exec_module(lib)文件“”,在exec_module文件中的第779行 get_code文件中的“”行916, source_to_code中的“”行846 在第219行的文件“”中 _call_with_frames_removed文件“ C:\ Users \ PC \ Desktop \ Code \ Waifu Bot \ cogs \ testhelp.py”,第22行 尝试: ^ IndentationError:unindent与任何外部缩进级别都不匹配

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

回溯(最近通话最近):文件 “ C:\ Users \ PC \ AppData \ Local \ Programs \ Python \ Python38-32 \ lib \ site-packages \ discord \ client.py”, _run_event中的第312行 等待coro(* args,** kwargs)文件“ C:\ Users \ PC \ Desktop \ Code \ Waifu Bot \ setup.py”,行95,在on_ready中 在on_ready中提高文件“ C:\ Users \ PC \ Desktop \ Code \ Waifu Bot \ setup.py”的第92行 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(规格,名称)文件“ C:\ Users \ PC \ AppData \ Local \ Programs \ Python \ Python38-32 \ lib \ site-packages \ discord \ ext \ commands \ bot.py”, _load_from_module_spec中的第610行 引发错误。来自discord.ext.commands.errors.ExtensionFailed:ExtensionFailed(key,e):扩展名'cogs.testhelp' 引发错误:IndentationError:unindent与任何外部不匹配 缩进级别(testhelp.py,第22行)

1 个答案:

答案 0 :(得分:1)

这只是一个简单的缩进错误(位于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:为避免缩进错误,请避免在代码行之后跳过行,这将更容易检测到这些错误,并且您的代码将更易于理解^^