gpt4 book ai didi

javascript - 如何使 discord bot 的命令不区分大小写

转载 作者:行者123 更新时间:2023-12-01 16:25:22 25 4
gpt4 key购买 nike

const Discord = require("discord.js");
//ignore 1 up and 1 down
const client = new Discord.Client();
//prefix
const prefix = `+`;
//log for me
client.once(`ready`, () => {
console.log("IM POWERED UP")
});


//commands

client.on(`message`, message =>{
const args = message.content.slice(prefix.lengh).split(/ +/);

//Username..(embed)
if (message.content === `${prefix}Username`){
let messageArray = message.content.split(" ");
let args = message.content.split(` `);
let command = messageArray[1].toLowerCase();
const embed = new Discord.MessageEmbed()
.setTitle(`user information`)
.addField(`Your Username is`, message.author.username)
.addField(`Bugs`, `To report bugs please go to TxPsycho#1080 and you may earn a reward!`)
.setThumbnail(message.author.displayAvatarURL())
.setFooter(`Wanna get Valorant cheats? Join here`)
.setColor(`RANDOM`)
message.channel.send(embed);
}

//help command
if (message.content === `${prefix}Help`){
const embed = new Discord.MessageEmbed()
.setTitle(`Help`)
.addField(`A list of commands are below!`)
.setThumbnail(message.author.displayAvatarURL())
.setDescription(`AMOTHERFUCKINGTESTYOUMOTHERFUCKAYOUTHINKITSFUNNYCALLINGAT2AM`)
.setColor(`RANDOM`)
message.channel.send(embed);
}
});


//ignore this and leave it at bottom
client.login(`Nz`)


I am not sure how to make the commands caps insensitive, this is the error it’s been giving me. I have been searching but found no information. Down below is the link to an error that I have been receiving when I try to debug the code.

Click here to see the error picture

最佳答案

就像流氓说的那样,问题是在这里调用未定义的 toLowerCase:

let command = messageArray[1].toLowerCase();

真正的命令是 0,因为数组是 0 索引的

你可能想shift它,这样你就可以更容易地获得参数:

消息:!help 12 24 36

const args = message.content.split(" ");
const command = args.shift().toLowerCase();

console.log(args);
// => [12, 24, 36]

console.log(args[0]);
// => 12

之后你只需比较 command 而不是 message.content:

if(command === "!help") {


}

关于javascript - 如何使 discord bot 的命令不区分大小写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62631162/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com