如何让python discord bot每天向自己发送自动dm?

时间:2021-06-12 22:52:35

标签: python web-scraping discord discord.py

我对 discord bots 还很陌生,我想知道如何(如果可能的话)我可以每天通过我的 bot 向自己发送消息。我正在制作一个网络抓取工具,但不想每天都运行它,我宁愿让机器人用抓取的结果来代替我。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

最好的方法是任务。

@tasks.loop(hours=24) 
async def sendmessage():
    users = [Youridhere]
    for id in users:
        member = await bot.fetch_user(id)
        try:
            member.send("Yourmessagehere")
        except:
            pass #do something if message couldn't be sent

接下来您必须开始任务,您可以在事件或命令中执行此操作。例如:

@bot.command()
async def startmess(ctx):
    sendmessage.start()
    await ctx.message.add_reaction('✅')

@bot.command()
async def stopmess(ctx):
    sendmessage.stop()
    await ctx.message.add_reaction('❌')

您可以在此处阅读任务:https://discordpy.readthedocs.io/en/latest/ext/tasks/index.html