当前位置: 首页 > 知识库问答 >
问题:

不和谐的Python重写-反应帮助(Cog)

宗政文彬
2023-03-14

我正试图做出一个依靠反应的帮助,我得到了这个代码

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:\用户\PC\AppData\本地\程序\Python\Python38-32\lib\site-包\discord\client.py",第312行,在_run_event等待coro(*args,**kwargs)文件"C:\用户\PC\Desktop\Code\Waifu Bot\setup.py",第95行,on_ready提出e File"C:\用户\PC\Desktop\Code\Waifu Bot\setup.py",第92行,on_readyclient.load_extension(cog)File"C:\用户\PC\AppData\本地\程序\Python\Python38-32\lib\site-包_load_from_module_spec(规范,名称)文件"C:\用户\PC\AppData\本地\程序\Python\Python38-32\lib\site-包\discord\ext\命令\bot.py",第610行,_load_from_module_spec引起错误。扩展失败(键,e)从ediscord.ext.commands.errors.扩展失败:扩展cogs.testhelp引发了一个错误:缩进错误:unindent不匹配任何外部缩进级别(testhelp.py,第22行)

共有1个答案

王修为
2023-03-14

这只是一个简单的缩进错误(在try...除了):

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:为了避免缩进错误,避免在代码行之后跳过一行,这样更容易检测到这些错误,你的代码也会更容易理解^^

 类似资料:
  • 我目前正在编写一个discord bot反应角色命令。 一切都很顺利,但我有一个问题。 当我启动机器人并运行创建嵌入的命令时,一切都很好。问题是当我对信息做出反应时,它并没有给我这个角色。这是我的代码:

  • 我们使用了一个管理角色的免费工具,在不和谐服务器上犯了一个错误。我就不点名了。该工具不允许我们编辑包含所有表情符号和相关角色的原始嵌入。此外,不和谐似乎不允许您编辑其他用户的消息,即使您拥有服务器。这很有道理。 所以我们剩下的就是创建一个新的角色消息,它看起来像旧的角色消息,并向其中添加反应。然而,我们不希望每个人都必须回去重做他们的反应,因为移动消息不会对他们现有的角色产生任何影响。我们仍然希望

  • 如何检查消息中添加了哪些反应?我想创建一个带有一些反应的命令(!roles),如果您单击其中一个,您将获得一个角色。我试着这样做: 但它不起作用。

  • 问题内容: 我试图使我的python脚本非常用户友好,因此我想为其提供某种帮助。您对此有何建议?我可以提出一些逻辑,即如果用户将帮助作为脚本的参数传递给他们,他们将获得帮助。是否有最佳实践或惯例? 问题答案: 使用argparse。 例如,使用 test.py : 跑步 产量

  • 几个月来,我一直在我的不和谐机器人中使用相同的反应角色代码,但是突然在过去的几天里,机器人不会给任何人任何角色。我不知道怎么了。我的机器人有权限,我不会返回任何类型的错误消息。这是我添加和删除角色的代码。 在这里定义。 这是我希望人们对这个角色做出反应的信息。

  • 我试过这些代码 我有这些情欲 忽略on_消息回溯中的异常(最近的上次调用):文件“C:\Users\PC\AppData\Local\Programs\Python\Python38\lib\site packages\discord\client.py”, 请帮忙,几周前它还在工作。