【问题标题】:Discord.py Random errors from running bot?Discord.py 运行机器人的随机错误?
【发布时间】:2021-06-27 21:07:29
【问题描述】:

这个问题主要是代码,因为我不知道发生了什么以及如何解决它......

我还将提供主文件。谢谢!

代码(看起来像这样;Microsoft Lens 不错,但不是很好):

import discord
from discord.ext.commands import CommandNotFound
from discord_slash import SlashCommand
def mixedCase(*args) :
    total = []
    for string in args:
        a = map( ''. join, 
            itertools.product(*((c.upper(), c.lower( ))
                for c in string) ) )
        for x in list(a):
            total.append(x)
    return list(total)

client = commands.Bot(case_insensitive=True, command_prefix=mixedCase( "mc" ) )
slash = SlashCommand ( client, sync_commands=True, sync_on_cog_reload=True)

startTime = int(time.time( ))
client.main_startTime = startTime

class MyHelpCommand (commands.MinimalHelpCommand):
     async def send_pages(self):
         destination = self.get_destination( )
         e = discord.Embed(color=discord.Color.green( ), description='')
         for page in self.paginator. pages:
             e.description += page
             e.set_footer (text="Need help? Do 'mcsupport to join the support server!")

         await destination.send(embed=e)

client.help_command = MyHelpCommand( )
client.run(TOKEN)

错误:

    Traceback (most recent call last):
    
    File "Bot.py", line 127, in <module> client. run( "TOKEN" )
    
    File "/home/quartz_i_warrior/.local/lib/python3.7/site-p ackages/discord/client.py", line 723, in run return future. result( )
    
    File "/home/quartz_i_warrior/.local/lib/python3.7/site-p ackages/discord/client.py", line 702, in runner await self. start( *args,** kwargs )
    
    File "/home/quartz_i_warrior/.local/lib/python3.7/site-p ackages/discord/client.py", line 666, in start await self. connect (reconnect=reconnect)
    
    File "/home/quartz_i_warrior/.local/lib/python3.7/site-p ackages/discord/client.py", line 566, in connect await self.ws. poll_event( )
    
    File "/home/quartz_i_warrior/.local/lib/python3.7/site-p ackages/discord/gateway.py", line 559, in poll_event await self. received_message(msg. data)
    
    File "/home/quartz_i_warrior/.local/lib/python3.7/site-p ackages/discord/gateway.py", line 509, in received_message func (data)
    
    File "/home/quartz_i_warrior/.local/lib/python3.7/site-p ackages/discord/state.py", line 490, in parse_message_crea te message = Message(channel=channel, data=data, state=self )
    
    File "/home/quartz_i_warrior/.local/lib/python3.7/site-p ackages/discord/message. py", line 584, in _init ref. resolved = self.__class__(channel=chan, data=resol ved, state=state)
    
    File "/home/quartz_i_warrior/.local/lib/python3.7/site-p ackages/discord/message.py", line 562, in _init self. stickers = [Sticker(data=data, state=state) for d ata in data. get( 'stickers', [])]
    
    File "/home/quartz_i_warrior/.local/lib/python3.7/site-p ackages/discord/message.py", line 562, in <listcomp> self. stickers = [Sticker(data=data, state=state) for d ata in data. get( 'stickers', [])]
    
    File "/home/quartz_i_warrior/.local/lib/python3.7/site-p ackages/discord/sticker.py", line 77, in _init_ self. pack_id = int(data['pack_id'])
    
    KeyError: 'pack_id'

使用 Microsoft Lens 提取,我会尝试纠正遗漏的单词。

【问题讨论】:

  • 为什么不能从编辑器中剪切和粘贴代码?这里的代码格式严重错误。
  • 我不在家,所以我不得不通过手机访问 vps;我回来后会更新它。

标签: python python-3.x discord discord.py


【解决方案1】:

查看输出和回溯,我可以看到错误来自不和谐模块。当使用sticker.py 并初始化其中的类时,pack_id 属性将使用 int(data['pack_id']) 进行实例化,该属性应返回带有键'pack_id' 的字典数据中的值。似乎找不到这样的项目。 keyerror 表明在字典数据中找不到键“pack_id”。通常,最好使用 data.get('pack_id', 'default value') 以避免出现问题(如果 value 不存在,只需替换为默认值。我不确定问题出在哪里,但是我不相信这是由于您的代码。

【讨论】:

  • 嗯,也许模块需要更新,我的代码似乎不是问题,谢谢
  • 是的,我确实更新了 discord.py,希望这就是问题所在。我使用的是 discord.py 1.7.1,而最新的是 1.7.3。
  • 希望以后一切顺利。 :)
猜你喜欢
  • 2020-05-21
  • 2021-02-12
  • 1970-01-01
  • 2020-08-31
  • 2020-05-10
  • 1970-01-01
  • 2022-01-03
  • 2019-09-08
  • 2021-05-03
相关资源
最近更新 更多