簡體   English   中英

是否可以讓 discord.py 機器人在沒有用戶觸發的情況下向用戶發送私人消息?

[英]Is it possible to make a discord.py bot send a private message to a user without user trigger?

我正在嘗試設置一個不和諧的機器人,當網站上發生各種變化時會通知我。 我正在使用 python 和 discord.py 重寫。

我發現的所有示例和教程都要求用戶調用機器人,但是我需要機器人依賴外部提示(網站正在更新)而不是用戶交互。

async def send_notification(content):
    client = discord.Client()
    user = client.fetch_user(0000000)

    if user is not None:
        await user.send(content)
    client.run("TOKEN")

我收到以下消息: RuntimeWarning: coroutine 'send_notification' was never awaited

根據我對這個問題的理解,一個可能的解決方案是:在 on_ready() 下,你可以有一段時間檢查這個網站的 True 循環。 像這樣的東西:

@bot.event
async def on_ready():
    print("Online.")
    while True:
        checkWebsite()
        await asyncio.sleep(60) # Add delay if you want: (takes seconds)

希望這可以幫助 :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM