Discord.js |如何确保您可以使用变量中的文本运行命令



我英语不是很好,很抱歉,我希望你可以运行这样的命令/sayEN,但EN处于类似的变量中

const language = {
"EN": "English",
"ESP": "Espanol"
}

这样你就可以运行命令,并使用变量中的文本

我试着制作代码,但太混乱了

let messageArray = message.content.split(" ");
let cmd = messageArray[0];
let args = message.content.substring(message.content.indexOf(' ')+1);
var lang = {
"EN": "English",
"ESP": "Espanol"
}

if (cmd === (prefix + `say${lang}`)) /*lol how do i make like `say${the text that are in the variable*/ {
message.reply(${lang}) /*what i mean here is the command that the user inputs,
like if i say "/sayESP" then the reply will be ESP too*/
}

对不起我英语不好这里有更容易理解和简单的

const lang = {
"EN": "English",
"ESP": "Espanol
}
**The Discord Chat**
User: /sayEN
bot: English
User: /sayESP
bot: Espanol

尝试以下代码:让存储变量为a[使用数组而不是对象:const a = ['EN','ESP'];]:

const a = ['EN','ESP'];
const b = ['English','Espanol'];
if (command.startsWith(PREFIX+'say'){
command.slice(PREFIX.length+3);
if (a.includes(command){
message.channel.send(b[a.indexOf(command)]);
}
}
执行该操作的最佳方法是在项目文件夹中创建一个/commands文件夹,并为每个命令创建命令脚本。命令脚本模板可能如下:
module.exports.run = async (bot, message, args) => {
// All Command Operations Here
};
exports.conf = {
enabled: true,
guildOnly: false,
aliases: [],
permlevel: 0
};
exports.help = {
name: 'command name',
description: 'Description',
usage: 'usage description'
};

例如,创建一个名为sayENG.js的文件,并将其放入:

module.exports.run = async (bot, message, args) => {
// Code here what if you want to send when a user sends a message that starts with /sayENG
};
exports.conf = {
enabled: true,
guildOnly: false,
aliases: [],
permlevel: 0
};
exports.help = {
name: 'sayENG',
description: 'say English',
usage: '/sayENG <message>'
};

为了访问索引文件中的文件,您应该将该命令添加到索引文件中。

client.commands = new Discord.Collection();
client.aliases = new Discord.Collection();
fs.readdir("./commands/", (err, files) => {
if (err) console.error(err);
log(`${files.length} commands will be uploaded.`);
files.forEach(f => {
let props = require(`./komutlar/${f}`);
log(`Uploaded command: ${props.help.name}.`);
client.commands.set(props.help.name, props);
props.conf.aliases.forEach(alias => {
client.aliases.set(alias, props.help.name);
});
}); //
});
client.reload = command => {
return new Promise((resolve, reject) => {
try {
delete require.cache[require.resolve(`./commands/${command}`)];
let cmd = require(`./commands/${command}`);
client.commands.delete(command);
client.aliases.forEach((cmd, alias) => {
if (cmd === command) client.aliases.delete(alias);
});
client.commands.set(command, cmd);
cmd.conf.aliases.forEach(alias => {
client.aliases.set(alias, cmd.help.name);
});
resolve();
} catch (e) {
reject(e);
} //L
});
};
client.load = command => {
return new Promise((resolve, reject) => {
try {
let cmd = require(`./commands/${command}`);
client.commands.set(command, cmd);
cmd.conf.aliases.forEach(alias => {
client.aliases.set(alias, cmd.help.name);
});
resolve();
} catch (e) {
reject(e); //
}
});
};
client.unload = command => {
return new Promise((resolve, reject) => {
try {
delete require.cache[require.resolve(`./komutlar/${command}`)];
let cmd = require(`./commands/${command}`);
client.commands.delete(command);
client.aliases.forEach((cmd, alias) => {
if (cmd === command) client.aliases.delete(alias);
});
resolve();
} catch (e) {
reject(e);
}
});
};

最新更新