我是写discord.js的新手,首先我不知道这是否是一个愚蠢的问题,但问题是我想制作一个机器人,这样第一部分就是问候语:
//greetings
const random_greeting = () =>{
return Math.floor(Math.random() * 5);
}
client.on('messageCreate', msg=>{
if(msg.author == client.user) return;
let greeting = ['Hi', 'Yo', 'Hello', '👋👋', 'Ok...',]
if (msg.content === 'hi'){
msg.reply(greeting[random_greeting()])}
if (msg.content === 'hello'){
msg.reply(greeting[random_greeting()])}
if (msg.content === 'yo'){
msg.reply(greeting[random_greeting()])}
if (msg.content === 'sup'){
msg.reply(greeting[random_greeting()])}
if (msg.content === 'wassup'){
msg.reply(greeting[random_greeting()])}
if (msg.content === 'yo'){
msg.reply(greeting[random_greeting()])}
})
正如你所看到的,我需要写:
if (msg.content === 'hello'){
msg.reply(greeting[random_greeting()])}
并不断发送6次垃圾邮件,所以我的问题是有什么方法可以做到更短或更快?
我尝试过添加greetingangswer变量:
let greetingAns = ['hi', 'hello']
但当我使用时
const random_greeting = () =>{
return Math.floor(Math.random() * 5);
}
client.on('messageCreate', msg=>{
if(msg.author == client.user) return;
let greeting = ['Hi', 'Yo', 'Hello', '👋👋', 'Ok...',]
let greetingAns = ['hi', 'hello']
if (msg.content === greetingAns()){
msg.reply(greeting[random_greeting()])}
我认为它应该起作用,但没有。那么msg.content ===
是一个变量吗?
是的,您可以做到这一点,并且您需要像以前一样使用字符串数组。因此,要检查消息内容是否在数组中找到,需要includes((javascript方法。
像这样:
if (greetingAns.includes(msg.content)) {
msg.reply(greeting[random_greeting()]);
}