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

标签 python bots discord.py

我正在尝试设置一个 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)

希望这有帮助:)

关于python - 是否可以让 discord.py 机器人在没有用户触发的情况下向用户发送私有(private)消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57061678/

相关文章:

python - 在 matplotlib 极坐标图中设置 theta-ticks

node.js - MS Teams 机器人上下文或功能中是否存在租户名称的概念?

python - Twisted irc python bot - 缓冲消息

python - (Python) Discord bot 从语音聊天中断开连接

python - 如何比快速排序更快地对整数数组进行排序?

python - 如何在python中更改对象的方法

python - python可以一次性给不同的键赋同一个值吗?

javascript - 直线 API Microsoft Bot 框架

python - Discord.py 重写如何DM命令?

python-3.x - 属性错误 : 'Client' object has no attribute 'command'