【发布时间】:2020-12-29 06:20:17
【问题描述】:
我正在尝试创建一个 Discord 机器人,它会每 X 秒(例如每 3 秒)自动发送一条消息,而无需任何用户交互/命令输入。
这是我的代码:
import discord
from discord.ext import tasks, commands
bot = commands.Bot(command_prefix="!")
@bot.event
async def on_ready():
await bot.wait_until_ready()
print("Logged in as:")
print(bot.user.name)
print("------")
channel = bot.get_channel(IDasInteger)
print("Channel is:")
print(channel) #Prints None
get_price.start()
@tasks.loop(seconds=3)
async def get_price():
await bot.wait_until_ready()
channel = bot.get_channel(IDasInteger)
print("Channel is:")
print(channel) #Prints None
await channel.send('Test')
@get_price.before_loop
async def before ():
print("Before done.")
bot.run('MyTokenHere')
问题是,当我执行这段代码时,它给了我以下错误:
AttributeError: 'NoneType' object has no attribute 'send'
当我尝试打印通道变量时,它返回无。 频道 ID 是正确的 - 我直接从 Discord 应用程序复制了它,没有对其值进行任何更改。
有什么想法吗?
谢谢
【问题讨论】:
标签: python-3.x discord.py