【发布时间】: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