为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

心有法竹 2023-09-02 16:10:19
例如,我尝试在一般聊天中每 10 秒写一次"你好"。我怎么做?我正在使用任务功能。channel = client.get_channel('channel id') @tasks.loop(seconds=10)async def sendmessage():    await channel.send("hello")
查看完整描述

5 回答

?
海绵宝宝撒

TA贡献1809条经验 获得超8个赞

你需要举办你的活动。sendmessage.starton_ready


例子:


@client.event

async def on_ready():

    sendmessage.start()


查看完整回答
反对 回复 2023-09-02
?
慕无忌1623718

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")


查看完整回答
反对 回复 2023-09-02
?
忽然笑

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


查看完整回答
反对 回复 2023-09-02
?
ibeautiful

TA贡献1993条经验 获得超5个赞

确保你的“频道 ID”是一个整数,并且假设你有某个地方,你的代码应该按原样工作。sendmessage.start()

额外注意:您可能想将自己的任务放入任务中。从内部缓存获取通道对象,因此如果缓存过期,您的通道对象也会过期。get_channelget_channel


查看完整回答
反对 回复 2023-09-02
?
慕的地6264312

TA贡献1817条经验 获得超6个赞

您可以使用 '.start() 启动任务


channel = client.get_channel('channel id') 

@tasks.loop(seconds=10)

async def sendmessage():

    await channel.send("hello")


sendmessage.start()


查看完整回答
反对 回复 2023-09-02
  • 5 回答
  • 0 关注
  • 145 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信