python-3.x - 如何从 Discord.py 中的循环事件发送 DM
问题描述
尝试向用户发送 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。
解决方案
假设您根据您的评论获得了正确的 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)
推荐阅读
- python - 使用 Python 从 Sharepoint 下载文件
- java - 从大输入值中获取所有最小值和最大值的总和
- python - Any 和 All 将 2d 关键字列表与字符串进行比较
- android - Android Studio - 如何切换并保持新活动
- javascript - 单击按钮时使用 React 导航到新页面
- docker - Cloud9 C 代码运行失败
- javascript - Material UI react自动完成设置不同的标签和不同的值
- python - Python类实例,索引超出范围
- haskell - Haskell 错误 :: 未找到实例(读取.........)
- r - apply( ) 类函数检索多个数据帧的信息以替换循环