猿问

对于discord python,如何使用任务每x秒打印一条消息?

例如,我尝试在一般聊天中每 10 秒写一次"你好"。我怎么做?我正在使用任务功能。


channel = client.get_channel('channel id') 

@tasks.loop(seconds=10)

async def sendmessage():

    await channel.send("hello")


心有法竹
浏览 146回答 5
5回答

海绵宝宝撒

你需要举办你的活动。sendmessage.starton_ready例子:@client.eventasync def on_ready():    sendmessage.start()

慕无忌1623718

import discordfrom discord.ext import commands, tasksbot = 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")

忽然笑

您可能想尝试将通道移动到函数中,如下所示:import discordimport datetimefrom discord.ext import commands, tasksTOKEN = '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

ibeautiful

确保你的“频道 ID”是一个整数,并且假设你有某个地方,你的代码应该按原样工作。sendmessage.start()额外注意:您可能想将自己的任务放入任务中。从内部缓存获取通道对象,因此如果缓存过期,您的通道对象也会过期。get_channelget_channel

慕的地6264312

您可以使用 '.start() 启动任务channel = client.get_channel('channel id') @tasks.loop(seconds=10)async def sendmessage():    await channel.send("hello")sendmessage.start()
随时随地看视频慕课网APP

相关分类

Python
我要回答