【问题标题】:discord.py channel.history returning object "Context" han no attribute 'id'discord.py channel.history 返回对象“上下文”没有属性“id”
【发布时间】:2021-07-07 11:05:55
【问题描述】:
    @bot.command(cls = CommandWithCooldown, pass_context = True)
    @commands.cooldown(1, 30, commands.BucketType.user)
    async def strikeball(message, yy=None):
        embed = discord.Embed(color=0xeee657, inline = False) #created embed
        embed.set_author(name = message.author, icon_url = message.author.avatar_url)
        today = datetime.datetime.today()
        embed.set_footer(text = 'BotSiniy© | {}'.format(today.strftime("%H:%M %d/%m/%Y")))
        if yy==None:
            embed.add_field(name = 'Команда: strikeball',  #description command
            value = "**Описание:** Начинает игру в strikeball(перестрелка)\n**Cool:** 1 сообщения за 30 секунд\n**Использование:** `s!strikeball start`")
            await message.channel.send(embed = embed)
        elif yy=='start':
            red=[]
            blue=[]
            await message.channel.send('Игра начнется через 60 секунд.... Для присоединения используйте `join (red или blue)`')
            for x in range(1,6,1):
                time.sleep(10)
                await message.channel.send('Игра начнется через {} секунд.... Для присоединения используйте `join (red или blue)`'.format(int(60-(x*10))))
                chat_history = await message.channel.history(limit=10, after = message).flatten()
            for msg in chat_history:
                if msg.author in red or msg.author in blue:
                    pass
                else:
                    if msg.content== "join red":
                        red.append(msg.author)
                        ping = msg.author.mention
                        await message.channel.send(ping+',присоединился к красной команде!')
                    elif msg.content=='join blue':
                        blue.append(msg.author)
                        ping = msg.author.mention
                        await message.channel.send(ping+',присоединился к синей команде!')
            time.sleep(10)
        if len(red)<1 or len(blue)<1:
            await message.channel.send("Недостаточно пользователей для начала игры...")

厄洛尔:

忽略命令罢工球中的异常: 回溯(最近一次通话最后): 罢工球中的文件“bot.py”,第 243 行 message.channel.history 中的 msg 异步(限制 = 10,之后 = 消息): 文件“C:\Users\Trava\AppData\Local\Programs\Python\Python38-32\lib\site-packag es\discord\iterators.py",第 91 行,在 anext 中 味精 = 等待 self.next() 文件“C:\Users\Trava\AppData\Local\Programs\Python\Python38-32\lib\site-packag es\discord\iterators.py",第 285 行,在下一个 等待 self.fill_messages() 文件“C:\Users\Trava\AppData\Local\Programs\Python\Python38-32\lib\site-packag es\discord\iterators.py",第 331 行,在 fill_messages 中 数据 = 等待 self._retrieve_messages(self.retrieve) 文件“C:\Users\Trava\AppData\Local\Programs\Python\Python38-32\lib\site-packag es\discord\iterators.py",第 360 行,在 _retrieve_messages_after_strategy after = self.after.id if self.after else None AttributeError:“上下文”对象没有属性“id” 上述异常是以下异常的直接原因: 回溯(最近一次通话最后): 文件“C:\Users\Trava\AppData\Local\Programs\Python\Python38-32\lib\site-packag es\discord\ext\commands\bot.py",第 902 行,在调用中 等待 ctx.command.invoke(ctx) 文件“C:\Users\Trava\AppData\Local\Programs\Python\Python38-32\lib\site-packag es\discord\ext\commands\core.py",第 864 行,在调用中 等待注入(*ctx.args,**ctx.kwargs) 文件“C:\Users\Trava\AppData\Local\Programs\Python\Python38-32\lib\site-packag es\discord\ext\commands\core.py",第 94 行,已包装 从 exc 引发 CommandInvokeError(exc) discord.ext.commands.errors.CommandInvokeError:命令引发异常:Att ributeError: 'Context' 对象没有属性 'id

之后出现问题,但我不明白为什么。 请帮帮我

【问题讨论】:

  • 你能给我们提供更多关于你的代码和漏洞错误信息的细节
  • @Guddi 。我做

标签: python-3.x discord.py


【解决方案1】:

命令中的第一个参数总是commands.Context,简称ctx
您将其命名为 message,这很令人困惑,因为它仍然是 commands.Context,而不是 discord.Message,正如您在 docs 'Context' object has no attribute 'id' 中看到的那样,这也是错误

所以先重命名:

# old
async def strikeball(message, yy=None):
# new
async def strikeball(ctx, yy=None):

如果您想要discord.Message 的命令消息,您可以使用ctx.message

@bot.command()
async cef strikeball(ctx, yy=None):
    message = ctx.message

现在await message.channel.history(limit=10, after=message).flatten() 也应该可以工作了

【讨论】:

    猜你喜欢
    • 2020-09-22
    • 2019-05-23
    • 2019-06-19
    • 1970-01-01
    • 1970-01-01
    • 2020-12-17
    • 2021-07-27
    • 2021-01-05
    • 2021-03-06
    相关资源
    最近更新 更多