js discord-bot,每次随机发送消息而不重新启动


var messages = ["Message1", "Message2", "Message3"]
var randomMessage = messages[Math.floor(Math.random() * messages.length)];
client.on('interactionCreate', async interaction => {
if (!interaction.isChatInputCommand()) return;
const { commandName } = interaction;
if (commandName === 'ping') {
await interaction.reply('Pong!');
await interaction.channel.send(randomMessage)
}
});

我希望每次运行ping命令时都能发送一条随机消息。我怎么能拿到?

到目前为止,您只在数组中拾取一次随机消息。因此,无论何时使用消息,消息都将始终相同。为了不断挑选新的随机消息,请在client.on函数中放置var randomMessage = messages[Math.floor(Math.random() * messages.length)]行。

相关内容

最新更新