如何解决discord.py 1.7.2 - 如何私信用户
discord.py 1.7.2
用例:
当用户加入我的频道时,我会将他们的不和谐 ID 保存在我的数据库中。
如果用户发送带有 $sub foo
字样的消息,则他们订阅了一个
我的服务。当他们订阅此服务时。他们的想法是为了
他们根据类型不时收到私人消息
代码
import discord,os,asyncio
from discord.ext import commands,tasks
from Entity.notifications import Notification
from dotenv import load_dotenv
from discord.utils import get
bot = commands.Bot(command_prefix='$')
@bot.event
async def on_ready():
print('Bot is ready')
@tasks.loop(seconds=10)
async def user_notification():
print("foo") ##terminal shows the word foo
await bot.wait_until_ready()
user_sub_notifications= Notification().get_active_user_sub_notification()
if user_sub_notifications:
for notification in user_sub_notifications:
if "user_discord_id" in notification:
#get user
user = get(bot.get_all_members(),id=notification['user_discord_id'])
#get the message to sent to the user
message = notification['message']
#private message the user
#bot.send_message(user,message)
await asyncio.sleep(10)
load_dotenv()
user_notificarion.start()
bot.run(os.getenv('DISCORD_TOKEN'))
我已经尝试了以下
user = get(bot.get_all_members(),id=notification['discord_id']) #got NONE
bot.send_message(notification['user_discord_id'],message) ##bot has not attribute send_message
如何给用户私信?谢谢
解决方法
感谢来自 discord.py github 的 Rapptz 回答了这个问题。所有功劳都归功于他。
在 v1.7 中,您需要先获取用户:
user = bot.get_user(user_id) or await bot.fetch_user(user_id)
await user.send('whatever')
将来在 v2.0(仍在开发中)中,您将能够通过用户 ID 直接打开 DM:
dm = await client.create_dm(user_id)
await dm.send('whatever')
实际有效的方法是 fetch_user
我想说的最好和最简单的方法是:
await member.send("{message}")
这将自动打开一个包含使用该命令的成员的 dm,您可以输入任何您想要的消息,类似于 ctx.send
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。