如何修改discord.js代码实现检测Roblox链接时发送Embed并指定频道记录日志
代码修改方案
你现有代码缺少客户端初始化的步骤,补齐逻辑后将Embed发送逻辑插入到触发条件的代码块中即可,完整可运行代码如下:
const token = ""; // 引入discord.js库 const discord = require("discord.js"); // 初始化客户端(原有代码遗漏该步骤,必须补充否则运行报错) const client = new discord.Client(); // 替换为你自己的日志频道ID const LOG_CHANNEL_ID = "你的日志频道ID"; client.on("message", (message) => { // 过滤机器人消息、私聊消息 if (message.author.bot) return; if (!message.guild) return; // 检测消息是否以roblox链接开头 if (message.content.startsWith("https://roblox.com")) { // 1. 构造Embed并发送到当前消息所在频道 const embed = new discord.RichEmbed() .setTitle("64 Games") .setColor(0x00AE86) .setDescription("A Game Has been found!!!") .setThumbnail("https://yagami.xyz/content/uploads/2018/11/discord-512-1.png") .setTimestamp() message.channel.send({embed}); // 2. 生成日志并发送到指定的日志频道 const msgLog = `[MESSAGE] [${message.guild.name}] [#${message.channel.name}] ${message.author.username}#${message.author.discriminator}: ${message.content}\n`; client.channels.get(LOG_CHANNEL_ID).send(msgLog); return; } }); client.login(token);
高版本兼容调整
如果你使用的是discord.js v12及以上版本,需要做两处适配修改:
- 将
new discord.RichEmbed()替换为new discord.MessageEmbed() - 将
client.channels.get(LOG_CHANNEL_ID)替换为client.channels.cache.get(LOG_CHANNEL_ID)
内容的提问来源于stack exchange,提问作者projectnil2
相关产品推荐
相关产品推荐

