简体   繁体   English

如何在 discord.py 的循环中发送消息?

[英]How to send message in loop of discord.py?

I want to make my bot be able to send message in loop with custom loop time eg.我想让我的机器人能够使用自定义循环时间循环发送消息,例如。 .remind 60s would make the bot say reminder in 60 seconds eg. .remind 60s 会让机器人在 60 秒内说提醒,例如。 !remind 23s would make the bot say reminder in 23 seconds !remind 23s 会让机器人在 23 秒内发出提醒

I currently have this, but it doesn't work.我目前有这个,但它不起作用。 Any help would be appreciated and if you could also provide a example that would be great.任何帮助将不胜感激,如果您也可以提供一个很好的例子。 Thanks谢谢

@tasks.loop(seconds=timeP)
async def Reminder():

timeP = 5
channel = client.get_channel(717689495620681731)
await client.change_presence(activity=discord.Game('online'))
print('test')
await channel.send('<@&717696163574186026> here')

You can use the task decorator from discord.py:您可以使用 discord.py 中的task装饰器:

import discord
from discord.ext import task

@tasks.loop(seconds=x) #you can also have minutes=x or hours=x
    async def printer(self):
        channel = client.get_channel(717689495620681731)
        await client.change_presence(activity=discord.Game('online'))
        print('test')
        await channel.send('<@&717696163574186026> here')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM