【问题标题】:Is it possible to make a discord.py bot send a private message to a user without user trigger?是否可以让 discord.py 机器人在没有用户触发的情况下向用户发送私人消息?
【发布时间】:2019-11-25 10:49:26
【问题描述】:

我正在尝试设置一个不和谐的机器人,当网站上发生各种变化时通知我。我正在使用 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 waiting

【问题讨论】:

  • 您的网站监控是如何工作的?它是按计划运行,还是主动监控网站的变化?
  • 你可以使用 webhook

标签: python bots discord.py discord.py-rewrite


【解决方案1】:

根据我对问题的理解,一个可能的解决方案是:在 on_ready() 下,您可以使用 while True 循环检查该网站。像这样的:

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

希望这会有所帮助:)

【讨论】:

    猜你喜欢
    • 2017-06-04
    • 2019-10-28
    • 2019-09-19
    • 2021-12-03
    • 2022-06-11
    • 1970-01-01
    • 1970-01-01
    • 2019-06-27
    • 2012-09-21
    相关资源
    最近更新 更多