为什么我无法在 discord.py 中的 @tasks.loop 中发送消息?
Why I can't send messages in a @tasks.loop in discord.py?
我有一个问题:我无法在@tasks.loop() 函数中发送消息。
当我尝试使用 self.client.get_channel(channlid)
获取通道对象时,它 return 我是一个 Nonetype 变量。
我的代码:
import nextcord
from nextcord.ext import commands
from nextcord.ext import tasks
from datetime import date, datetime
channlid = 934533675083792465
global channel
def setup(client):
client.add_cog(Blagues(client))
class Blagues(commands.Cog):
def __init__(self, client):
self.client = client
self.index = 0
self.blaguedujour.start()
@tasks.loop(seconds=1)
async def blaguedujour(self):
channel = self.client.get_channel(channlid)
channel.send("a")
@commands.command()
async def test(self, ctx):
await ctx.send("test")
我的错误:
PS C:\Users\baron_btjit4i> & C:/Users/baron_btjit4i/AppData/Local/Programs/Python/Python39/python.exe c:/Users/baron_btjit4i/Desktop/Autrebot/main.py
Unhandled exception in internal background task 'blaguedujour'.
Traceback (most recent call last):
File "C:\Users\baron_btjit4i\AppData\Local\Programs\Python\Python39\lib\site-packages\nextcord\ext\tasks\__init__.py", line 168, in _loop
await self.coro(*args, **kwargs)
File "c:\Users\baron_btjit4i\Desktop\Autrebot\Cogs\blagues.py", line 20, in blaguedujour
channel.send("a")
AttributeError: 'NoneType' object has no attribute 'send'
你能帮帮我吗?
问题
您在客户端准备好之前调用 client.get_channel
。所以,客户端找不到你要找的频道,channel
变成了None
.
发生这种情况是因为您在 Blagues
class.
的初始化中开始 blaguedujour
解决方案
说明
不应在 __init__
中开始任务,而应在 on_ready
客户端准备就绪时开始。这可以通过在 Blagues
齿轮中添加 commands.Cog.listener()
来实现。
代码
class Blagues(commands.Cog):
def __init__(self, client):
self.client = client
self.index = 0
@tasks.loop(seconds=1)
async def blaguedujour(self):
channel = self.client.get_channel(channlid)
channel.send("a")
@commands.Cog.listener()
async def on_ready():
self.blaguedujour.start()
@commands.command()
async def test(self, ctx):
await ctx.send("test")
旁白
channel.send()
是协程:必须等待。请改用 await channel.send("a")
。
我有一个问题:我无法在@tasks.loop() 函数中发送消息。
当我尝试使用 self.client.get_channel(channlid)
获取通道对象时,它 return 我是一个 Nonetype 变量。
我的代码:
import nextcord
from nextcord.ext import commands
from nextcord.ext import tasks
from datetime import date, datetime
channlid = 934533675083792465
global channel
def setup(client):
client.add_cog(Blagues(client))
class Blagues(commands.Cog):
def __init__(self, client):
self.client = client
self.index = 0
self.blaguedujour.start()
@tasks.loop(seconds=1)
async def blaguedujour(self):
channel = self.client.get_channel(channlid)
channel.send("a")
@commands.command()
async def test(self, ctx):
await ctx.send("test")
我的错误:
PS C:\Users\baron_btjit4i> & C:/Users/baron_btjit4i/AppData/Local/Programs/Python/Python39/python.exe c:/Users/baron_btjit4i/Desktop/Autrebot/main.py
Unhandled exception in internal background task 'blaguedujour'.
Traceback (most recent call last):
File "C:\Users\baron_btjit4i\AppData\Local\Programs\Python\Python39\lib\site-packages\nextcord\ext\tasks\__init__.py", line 168, in _loop
await self.coro(*args, **kwargs)
File "c:\Users\baron_btjit4i\Desktop\Autrebot\Cogs\blagues.py", line 20, in blaguedujour
channel.send("a")
AttributeError: 'NoneType' object has no attribute 'send'
你能帮帮我吗?
问题
您在客户端准备好之前调用 client.get_channel
。所以,客户端找不到你要找的频道,channel
变成了None
.
发生这种情况是因为您在 Blagues
class.
blaguedujour
解决方案
说明
不应在 __init__
中开始任务,而应在 on_ready
客户端准备就绪时开始。这可以通过在 Blagues
齿轮中添加 commands.Cog.listener()
来实现。
代码
class Blagues(commands.Cog):
def __init__(self, client):
self.client = client
self.index = 0
@tasks.loop(seconds=1)
async def blaguedujour(self):
channel = self.client.get_channel(channlid)
channel.send("a")
@commands.Cog.listener()
async def on_ready():
self.blaguedujour.start()
@commands.command()
async def test(self, ctx):
await ctx.send("test")
旁白
channel.send()
是协程:必须等待。请改用 await channel.send("a")
。