简体   繁体   English

如何让我的 discord 机器人每隔几秒发送一次消息?(discord.py)

[英]How to make my discord bot send messages every few seconds?(discord.py)

I am trying to make my discord bot DM the same message every few seconds(and not in quick succession).我试图让我的 discord 机器人 DM 每隔几秒钟(而不是快速连续)发送相同的消息。 So far, I've made my discord bot send a given message a certain number of times, as provided by the user.到目前为止,我已经让我的 discord 机器人发送给定消息一定次数,由用户提供。 Here's my code:这是我的代码:

@client.command()
async def send(ctx, member: discord.Member, amount=1, *, content=None):
for i in range(amount):
    channel = await member.create_dm()
    await channel.send(content)

Any help would be well appreciated!任何帮助将不胜感激! Thank you!谢谢!

One thing is that your indentation is off.一件事是您的缩进已关闭。 You need to add an indent to whatever you put into the command.您需要为您放入命令的任何内容添加缩进。

If you want to add a delay between messages, put await asyncio.sleep(time) in your loop, where time is your delay in seconds.如果要在消息之间添加延迟,请将await asyncio.sleep(time)放入循环中,其中time是以秒为单位的延迟。

So, you should have所以,你应该有

@client.command()
async def send(ctx, member: discord.Member, amount=1, *, content=None):
    for i in range(amount):
        channel = await member.create_dm()
        await channel.send(content)
        await asyncio.sleep(time)

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

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