Переписать Discord Python — справка по реакции (Cog)

#discord #discord.py #discord.py-rewrite

#Discord #discord.py

Вопрос:

Я пытаюсь создать справку, основанную на реакциях, я получил этот код

 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:UsersPCAppDataLocalProgramsPythonPython38-32libsite-packagesdiscordextcommandsbot.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:UsersPCDesktopCodeWaifu Botcogstesthelp.py «, строка 22 попробуйте: ^ Ошибка отступа: unindent не соответствует ни одному внешнему уровню отступа

Вышеупомянутое исключение было прямой причиной следующего исключения:

Обратная трассировка (последний последний вызов): файл «C:UsersPCAppDataLocalProgramsPythonPython38-32libsite-packagesdiscordclient.py «, строка 312, в файле _run_event ожидают coro (*args, **kwargs) «C:UsersPCDesktopCodeWaifu Botsetup.py «, строка 95, в файле on_ready raise e «C:UsersPCDesktopCodeWaifu Botsetup.py «, строка 92, в файле on_ready client.load_extension (cog) «C:UsersPCAppDataLocalProgramsPythonPython38-32libsite-packagesdiscordextcommandsbot.py» , строка 664, в файле load_extension self._load_from_module_spec(спецификация, имя) «C:UsersPCAppDataLocalProgramsPythonPython38-32libsite-packagesdiscordextcommandsbot.py» строка 610 в _load_from_module_spec вызывает ошибки.Сбой расширения (ключ, e) из e discord.ext.commands.errors.Сбой расширения: расширение ‘cogs.testhelp’ выдало ошибку: Ошибка отступа: unindent не соответствует ни одному внешнему уровню отступа (testhelp.py , строка 22)

Ответ №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: Чтобы избежать ошибки отступа, избегайте пропускать строку после строки кода, так будет легче обнаружить эти ошибки, и ваш код будет более понятным ^^

Комментарии:

1. он запускается, но игнорирует добавление реакции