[英]Discord.js get user DM & log message
我希望我的機器人記錄來自特定 ID 的消息,或者如果無法記錄所有 DM 消息並發送到不和諧服務器通道。 如果特定 ID 發送“你好!”,機器人將發送“你好!” 到指定的頻道 ID。
如果您啟用了所有必需的意圖,您只需監聽messageCreate
(DiscordJS v12 及更早版本的message
)並檢查您的消息來自的頻道類型。 例如:
const { Client, Intents } = require('discord.js');
// Initializing your client
const client = new Client({
intents: [
// Intent for catching direct messages
Intents.DIRECT_MESSAGES,
// Intents for interacting with guilds
Intents.GUILDS,
Intents.GUILD_MESSAGES
]
});
// Subscribe to the messages creation event
client.on('messageCreate', async (message) => {
// Here you check for channel type
// We only need direct messages here, so skip other messages
if (message.channel.type !== 'DM')
return;
// Now we need guild where we need to log these messages
// Note: It's better for you to fetch this guild once and store somewhere
// and do not fetch it on every new received message
const targetGuild = await client.guilds.fetch('YOUR GUILD ID');
// Here you getting the channel from the list of the guild channels
const targetLoggingChannel = await targetGuild.channels.fetch('LOGGING CHANNEL ID');
// Sending content of the message to the target channel
// You can also cover the message into embed with some additional
// information about sender or time this message was sent
await targetLoggingChannel.send(message.content);
});
// Authorizing
client.login('TOKEN HERE');
這是如何將來自機器人的 DM 的消息記錄到您想要的任何公會的某個頻道的最小示例。 您還應該檢查日志記錄通道和公會的存在以防止錯誤。 還要確保機器人可以向目標頻道發送消息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.