Skip to main content
 首页 » 编程设计

python之是否可以让 discord.py 机器人在没有用户触发的情况下向用户发送私有(private)消息

2024年11月24日9lexus

我正在尝试设置一个 discord 机器人,当网站发生各种变化时它会通知我。我正在使用 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:从未等待协程“send_notification”

请您参考如下方法:

根据我对这个问题的理解,一个可能的解决方案是:在 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) 

希望这有帮助:)