[英]How to send a DM message knowing the user ID?
import discord
from discord.ext import commands
from dotenv import load_dotenv
load_dotenv("token.env")
token = os.getenv("TOKEN")
bot = commands.Bot(command_prefix="!")
@bot.event
async def on_message(message):
user="An_ID"
if message.author == bot.user :
return
if message.content.startswith("!"):
await bot.process_commands(message)
return
else :
embed = discord.Embed(title="message",color=0x4a1775)
await message.author.send(embed=embed)
return
這段代碼向觸發該函數的消息的作者發送一條消息。 我想將其發送給與“用戶”變量中保存的 ID 對應的用戶。 這是一個更大項目的一部分,ID 是從我保存了不同用戶 ID 的外部數據庫導入的。
要在知道用戶 ID 的情況下向用戶發送消息,請執行以下操作:
user_object = bot.get_user(id)
await user_object.send("Message")
id
是一個整數,請參閱 API 文檔:
https://discordpy.readthedocs.io/en/latest/api.html?highlight=get_user#discord.Client.get_user
https://discordpy.readthedocs.io/en/latest/api.html?highlight=get_user#discord.User.send
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.