我正在尝试使用 JS 创建一个不和谐的机器人。我为我打算实施的每个命令创建了一个 .js 文件,并且在“主”脚本中有一个检查将调用其他命令脚本。当我说 VS 代码不会告诉我参数的变量类型,也不会告诉我有关方法调用的任何信息时,我将使用图片来说明我的意思。Intellisense 在这些命令 .js 脚本中似乎也不能很好地工作。
背景信息:我有更多的 Java 编程经验。不管出于什么原因,JS 似乎让我感到困惑,但我想更好地学习和理解它。
我不会展示所有代码,只展示我的示例所需的代码。
main.js 脚本:
require('dotenv').config();
const { Client } = require('discord.js');
const client = new Client();
const prefix = "++";
const fs = require('fs');
client.commands = new Discord.Collection();
const commandFiles = fs.readdirSync('.src/commands/').filter(file => file.endsWith('.js'));
for(const file of commandFiles){
const command = require(`.src/commands/${file}`);
client.commands.set(command.name, command);
}
client.on('message', (message) => {
if(message.author.bot) return;
console.log(`[${message.author.tag}]: ${message.content}`);
if(!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).split(/ +/);
const command = args.shift().toLowerCase();
if(command === 'ping'){
client.commands.get('ping').execute(message, args);
});
ping.js 脚本:
module.exports = {
name: 'ping',
description: 'This is a ping command.',
execute(message, args){
message.channel.send('Pong!');
}
}
图片:它只是说任何,我假设任何变量类型,我认为......但这看起来很混乱并且难以理解。
main.js https://gyazo.com/86f7118513df791743def98bcf052f06
ping.js https://gyazo.com/06bb2e47a3fd39d2e4445591d8537131
沧海一幻觉
相关分类