import discord
from discord.ext import commands, tasks
from discord_webhook import DiscordWebhook
client = discord.Client()
bot = commands.Bot(command_prefix="$")
@tasks.loop(seconds=15.0)
async def getAlert():
#do work here
channel = bot.get_channel(channel_id_as_int)
await channel.send("TESTING")
getAlert.start()
bot.run(token)
当我打印“通道”时,我得到的是"None“,程序崩溃时会说"AttributeError:'NoneType‘对象没有属性'send’”。
我的猜测是,我得到的频道之前,它是可用,但我不确定。有人知道我怎样才能让这个信号发送到特定的频道吗?
发布于 2022-02-03 02:46:53
您的机器人无法立即获得通道,特别是如果它不在机器人的缓存中。相反,我建议获取服务器id,让bot从id获取服务器,然后从该服务器获取通道。请查看下面的修改代码。
@tasks.loop(seconds=15.0)
async def getAlert():
#do work here
guild = bot.get_guild(server_id_as_int)
channel = guild.get_channel(channel_id_as_int)
await channel.send("TESTING")
(编辑:包括评论中的回答,以便其他人可以参考)
您还应该确保您的getAlert.start()
处于on_ready()
事件中,因为机器人需要启动并输入不和谐,才能访问任何公会或通道。
@bot.event
async def on_ready():
getAlert.start()
print("Ready!")
有用链接
discord.ext.tasks.loop
- discord.py docson_ready
event - discord.py docsbot.get_guild(id)
- discord.py docsguild.get_channel(id)
- discord.py docshttps://stackoverflow.com/questions/70965136
复制相似问题