📅  最后修改于: 2023-12-03 15:14:42.313000             🧑  作者: Mango
Discord.js是一款用于创建Discord机器人的JavaScript库。其中之一的功能是发送随机消息到频道中,可以用于调节气氛或提供趣味性的消息。下面是一个使用Discord.js发送随机消息的示例代码:
const Discord = require('discord.js');
const client = new Discord.Client();
const messages = [
'Hello World!',
'Welcome to our server!',
'How are you doing today?',
'Have you heard any good jokes lately?',
'What\'s your favorite movie?',
'Let\'s play a game!'
];
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.on('message', msg => {
if (msg.content === '!randommessage') {
const randomMessage = messages[Math.floor(Math.random() * messages.length)];
msg.channel.send(randomMessage);
}
});
client.login('token');
上述代码中,我们定义了一个包含多个字符串的数组,并在接收到特定命令时从数组中随机选择并发送一条消息。这个功能可以在多种场景下使用,如发送随机图片、表情等等。
使用Markdown格式呈现代码片段如下:
const Discord = require('discord.js');
const client = new Discord.Client();
const messages = [
'Hello World!',
'Welcome to our server!',
'How are you doing today?',
'Have you heard any good jokes lately?',
'What\'s your favorite movie?',
'Let\'s play a game!'
];
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.on('message', msg => {
if (msg.content === '!randommessage') {
const randomMessage = messages[Math.floor(Math.random() * messages.length)];
msg.channel.send(randomMessage);
}
});
client.login('token');
以上就是使用Discord.js发送随机消息的示例,希望对大家有所帮助!