【问题标题】:discord.py few tasks working some not [AttributeError: 'NoneType' object has no attribute 'send']discord.py 一些任务没有工作 [AttributeError: 'NoneType' object has no attribute 'send']
【发布时间】:2021-06-29 13:00:06
【问题描述】:

在成功让我的前 3 个任务完美运行后,我又来了,下一个让我有些头疼.. 它的代码与我在其他任务中的代码几乎相同。

代码:

    @tasks.loop(seconds=20)
async def online_stats():
    message_channel = bot.get_channel(target_channel_id2)
    link = my_link
    f = requests.get(link)
    sys.stdout = open("test2.txt", "w")
    print(f.text)
    sys.stdout.close()
    time.sleep(0.5)
    with fileinput.FileInput(filename2, inplace=True, backup='.bak') as file:
        for line in file:
         print(line.replace(text_to_search, replacement_text), end="")
    with fileinput.FileInput(filename2, inplace=True, backup='.bak') as file:
        for line in file:
         print(line.replace(text_to_search2, replacement_text2), end='') 
    with fileinput.FileInput(filename2, inplace=True, backup='.bak') as file:
        for line in file:
         print(line.replace(text_to_search3, replacement_text2), end='')
    with fileinput.FileInput(filename2, inplace=True, backup='.bak') as file:
        for line in file:
         print(line.replace(text_to_search4, replacement_text2), end='')
    with fileinput.FileInput(filename2, inplace=True, backup='.bak') as file:
        for line in file:
         print(line.replace(text_to_search5, replacement_text3), end='')
    with fileinput.FileInput(filename2, inplace=True, backup='.bak') as file:
        for line in file:
         print(line.replace(text_to_search6, replacement_text4), end='')
    with fileinput.FileInput(filename2, inplace=True, backup='.bak') as file:
        for line in file:
         print(line.replace(text_to_search7, replacement_text5), end='')
    with fileinput.FileInput(filename2, inplace=True, backup='.bak') as file:
        for line in file:
         print(line.replace(text_to_search8, replacement_text6), end='')

    with open('test2.txt') as fp:
        lines=fp.readlines()
    await message_channel.send(lines[148].strip('\n') + " " + lines[150])
    await message_channel.send(lines[156].strip('\n') + " " + lines[158].strip('\n'))
    await message_channel.send(lines[164].strip('\n') + " " + lines[166].strip('\n'))
    await message_channel.send(lines[172].strip('\n') + " " + lines[174].strip('\n'))
    await message_channel.send(lines[180].strip('\n') + " " + lines[182].strip('\n'))

控制台输出

    Unhandled exception in internal background task 'online_stats'.
Traceback (most recent call last):
  File "C:\Users\0x9\AppData\Roaming\Python\Python38\site-packages\discord\ext\tasks\__init__.py", line 101, in _loop
    await self.coro(*args, **kwargs)
  File ".\discord_bot_pi_aktuell.py", line 411, in online_stats
    await message_channel.send(lines[148].strip('\n') + " " + lines[150])
AttributeError: 'NoneType' object has no attribute 'send'

它与我用于正在运行的命令的代码基本相同,但是如果我想将代码用于任务,则它无法正常工作的原因是“AttributeError:'NoneType'对象没有属性'send”

回答每个问题!愿你们都拥有伟大的东方。

编辑:对于所有遇到相同问题的人来说,这是解决问题的解决方案:

你有一个前循环

@online_stats.before_loop
async def before():
    await bot.wait_until_ready()
    print("Finished waiting")

【问题讨论】:

  • 目标频道 ID 无效,因此 message_channel 为 None 并且您无法发送
  • 奇怪的原因,如果我将 ID 827255114196713482 添加为变量,则会发生相同的错误。

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


【解决方案1】:

您收到的错误是由 API 错误引起的,您的机器人无法获得您进入的频道

message_channel = bot.get_channel(id)

您应该检查机器人当前是否可以看到该频道。如果他不在包含该频道的服务器中,则您既不能发送消息也不能获取该频道。

【讨论】:

  • 嘿感谢您的回答,我将其更改为 message_channel = bot.get_channel(827255114196713482) 但得到相同的属性错误。机器人可以看到频道,其他任务在同一个频道上工作正常
  • 好的,然后尝试使用bot.fetch_channel。不知道为什么您会收到该错误,您的机器人似乎无法获得该频道。
  • 找到了问题,它是 bot.get_channel 函数,我只是忘记了 before.loop 函数..ty 无论如何都是为了您的帮助!我用解决方案编辑了主要帖子
  • 完美,希望对你有所帮助!
猜你喜欢
  • 1970-01-01
  • 2022-12-15
  • 1970-01-01
  • 2017-10-20
  • 2020-07-28
  • 2023-01-20
  • 1970-01-01
  • 2021-07-08
  • 2022-01-10
相关资源
最近更新 更多