【问题标题】:How to send a DM from a looped event in Discord.py如何从 Discord.py 中的循环事件发送 DM
【发布时间】:2021-02-07 22:23:07
【问题描述】:

尝试向用户发送 PM 但没有任何效果:

async def main_loop():
    await bot.wait_until_ready()
    while not bot.is_closed():
        # do stuff...
        if some_random_condition:
            user = bot.get_user(user_id)
            user.send_message("some message")  # doesn't work, user is Nonetype, id is correct though

bot.loop.create_task(main_loop())
bot.run(DISCORD_TOKEN)

由于这只是一个循环函数,我实际上并没有通过 discord.py 的框架传入的上下文或消息对象,我只有机器人 - 但我不知道如何使用机器人来访问用户向他们发送 PM。

【问题讨论】:

  • 你必须更精确,说“没有任何效果”太模糊了
  • 我在那里的评论说“用户是Nonetype”,我的意思是bot.get_user(user_id)返回None,所以当我执行user.send_message时,它会出错,因为它是None。
  • 您是否启用了intents.members?

标签: python-3.x bots discord.py


【解决方案1】:

假设您根据您的评论获得了正确的 ID,这就是您必须做的:

async def main_loop():
    await bot.wait_until_ready()
    while not bot.is_closed():
        # do stuff...
        if some_random_condition:
            user = await bot.fetch_user(user_id)
            await user.send("some message")

bot.loop.create_task(main_loop())
bot.run(DISCORD_TOKEN)

【讨论】:

  • 就是这样 - 我猜我的函数名称有误!谢谢
猜你喜欢
  • 2021-04-16
  • 2021-08-07
  • 1970-01-01
  • 2020-10-01
  • 2021-05-06
  • 1970-01-01
  • 2020-06-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多