5 回答
![?](http://img1.sycdn.imooc.com/533e4d00000171e602000200-100-100.jpg)
TA贡献1809条经验 获得超8个赞
你需要举办你的活动。sendmessage.starton_ready
例子:
@client.event
async def on_ready():
sendmessage.start()
![?](http://img1.sycdn.imooc.com/5458620000018a2602200220-100-100.jpg)
TA贡献1744条经验 获得超4个赞
import discord
from discord.ext import commands, tasks
bot = commands.Bot('!') # ! = PREFIX
@tasks.loop(seconds=10)
async def sendmessage():
channel = bot.get_channel(123456789) #Channel ID
await channel.send("hello")
sendmessage.start()
bot.run("TOKEN")
![?](http://img1.sycdn.imooc.com/54584dad0001dd7802200220-100-100.jpg)
TA贡献1806条经验 获得超5个赞
您可能想尝试将通道移动到函数中,如下所示:
import discord
import datetime
from discord.ext import commands, tasks
TOKEN = 'token'
client = discord.Client()
@tasks.loop(seconds=10)
async def sendmessage():
channel = client.get_channel('channel id')
await channel.send("hello")
sendmessage.start()
client.run(TOKEN)
该函数仅接受整数作为输入。get_channel
![?](http://img1.sycdn.imooc.com/5458463b0001358f02200220-100-100.jpg)
TA贡献1993条经验 获得超5个赞
确保你的“频道 ID”是一个整数,并且假设你有某个地方,你的代码应该按原样工作。sendmessage.start()
额外注意:您可能想将自己的任务放入任务中。从内部缓存获取通道对象,因此如果缓存过期,您的通道对象也会过期。get_channel
get_channel
![?](http://img1.sycdn.imooc.com/545864490001b5bd02200220-100-100.jpg)
TA贡献1817条经验 获得超6个赞
您可以使用 '.start() 启动任务
channel = client.get_channel('channel id')
@tasks.loop(seconds=10)
async def sendmessage():
await channel.send("hello")
sendmessage.start()
添加回答
举报