Discord.js 是一个用于构建 Discord 机器人的 Node.js 库。它允许开发者通过 JavaScript 与 Discord API 进行交互,从而实现各种功能,如发送消息、命令处理、事件监听等。
欢迎消息可以是纯文本消息,也可以包含嵌入(embed)消息,后者可以包含更丰富的内容,如标题、描述、图片、链接等。
以下是一个简单的示例,展示如何在 Discord.js 中创建并发送欢迎消息:
const { Client, Intents } = require('discord.js');
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });
client.once('ready', () => {
console.log(`Logged in as ${client.user.tag}`);
});
client.on('guildMemberAdd', member => {
const channel = member.guild.channels.cache.find(ch => ch.name === 'general');
if (!channel) return;
const welcomeMessage = `Welcome to the server, ${member.user.username}! Please read the rules in <#rules-channel-id>.`;
channel.send(welcomeMessage);
});
client.login('your-token-goes-here');
client.on('guildMemberAdd', member => {
const channel = member.guild.channels.cache.find(ch => ch.name === 'general');
if (!channel) {
console.log('Welcome channel not found');
return;
}
const welcomeMessage = `Welcome to the server, ${member.user.username}! Please read the rules in <#rules-channel-id>.`;
channel.send(welcomeMessage).catch(err => {
console.error('Failed to send welcome message:', err);
});
});
通过以上代码,可以更好地处理可能遇到的问题,并提供相应的错误日志。
领取专属 10元无门槛券
手把手带您无忧上云