javascript - How do I pass module.exports in my index.js / discord.js
问题描述
So I made a "mute" and "warn" command for my discord bot and when I try to run them I get an error. This error is because in all my previous commands I used ".execute(message, args)" in my command code, so when I wanted to get the command from my index.js I just had to pass client.commands.get('name').execute(message, args)
. Now, I don't have this in my command, so how do I get it from index.js. I hope you understand what I'm saying.
Here's index.js:
const Discord = require('discord.js');
const { Client, Collection } = require ('discord.js');
const client = new Discord.Client();
const token = 'TOKEN';
const PREFIX = '-';
const fs = require('fs');
client.commands = new Discord.Collection();
const commandFiles = fs.readdirSync('./commands/').filter(file => file.endsWith('.js'));
for(const file of commandFiles){
const command = require(`./commands/${file}`);
client.commands.set(command.name, command);
}
client.on('ready', () =>{
console.log('Compass is online');
client.user.setActivity('GameCore', {type: 'WATCHING'}).catch(console.error);
})
client.on('message', message=>{
if (!message.content.startsWith(PREFIX)) return;
else {
const args = message.content.slice(PREFIX.length).split(/ +/);
const command = args.shift().toLowerCase();
if(command === 'help') {
client.commands.get('help').execute(message, args);
}
if(command === 'kick') {
client.commands.get('kick').execute(message, args);
}
if(command === 'ban') {
client.commands.get('ban').execute(message, args);
}
if(command === 'slowmode') {
client.commands.get('slowmode').execute(message, args);
}
if(command === 'warn') {
client.commands.get('warn').execute(message, args);
}
if(command === 'mute') {
client.commands.get('mute').execute(message, args);
}
}
})
client.login(token);
mute.js (warn.js is build in a similar format):
var Discord = require('discord.js');
var ms = require('ms');
module.export = async(message, args) => {
if(!message.member.hasPemission('MANAGE_MESSAGES')) return message.channel.send(':x:***You do not have permissions to run this command!***')
var user = message.mentions.users.first();
if(!user) return message.channel.send(':x:***Please mention a user!***')
var member;
try {
member = await message.guild.members.fetch(user);
} catch(err) {
member = null;
}
if(!member) return message.channel.send(':x:***Please mention a user!***');
if(member.hasPermission('MANAGE_MESSAGES')) return message.channel.send(':x:***You cannot mute that person!***')
var rawTime = args[1];
var time = ms(rawTime)
if(!time) return message.channel.send('You didn\'t specify a time!')
var reason = args.splice(2).join(' ');
if(!reason) return message.reply('You need to give a mute reason!');
var channel = message.guild.channels.catch.find(c => c.name === 'potato');
var log = new Discord.MessageEmbed()
.setTitle(':white_check_mark:***This user has been muted.***')
.addField('User:', user, true)
.addField('By:', message.author, true)
.addField('Expires:', rawTime)
.addField('Reason:', reason)
channel.send(log);
var embed = new Discord.MessageEmbed()
.setTitle('You have been muted!')
.addField('Expires:', rawTime, true)
.addField('Reason:', reason, true);
try {
user.send(embed);
} catch(err) {
console.warn(err);
}
var role = msg.guild.roles.cache.find(r => r.name === 'Muted');
member.roles.add(role);
setTimeout(async() => {
member.roles.remove(role);
}, time);
message.channel.send(`**${user}** has been muted by **${message.author}** for **${rawTime}**!`)
}
And the error I get when I type "-mute" or "-warn":
client.commands.get('mute').execute(message, args);
^
TypeError: Cannot read property 'execute' of undefined
at Client.<anonymous> (G:\Alexis\Desktop\Compass\index.js:46:40)
at Client.emit (events.js:315:20)
at MessageCreateAction.handle (G:\Alexis\Desktop\Compass\node_modules\discord.js\src\client\actions\MessageCreate.js:31:14)
at Object.module.exports [as MESSAGE_CREATE] (G:\Alexis\Desktop\Compass\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
at WebSocketManager.handlePacket (G:\Alexis\Desktop\Compass\node_modules\discord.js\src\client\websocket\WebSocketManager.js:384:31)
at WebSocketShard.onPacket (G:\Alexis\Desktop\Compass\node_modules\discord.js\src\client\websocket\WebSocketShard.js:444:22)
at WebSocketShard.onMessage (G:\Alexis\Desktop\Compass\node_modules\discord.js\src\client\websocket\WebSocketShard.js:301:10)
at WebSocket.onMessage (G:\Alexis\Desktop\Compass\node_modules\ws\lib\event-target.js:125:16)
at WebSocket.emit (events.js:315:20)
at Receiver.receiverOnMessage (G:\Alexis\Desktop\Compass\node_modules\ws\lib\websocket.js:797:20)
解决方案
导出模块时,您应该使用module.exports
而不是module.export
.
此外,您现在正尝试从模块中读取name
和command
属性,但没有。
const command = require(`./commands/${file}`);
client.commands.set(command.name, command);
因此,如果您想使用这样的方法,您的命令模块应该如下所示:
module.exports.name = 'mute';
module.exports.command = async (message, args) { ... };
或者您可以使用您的文件名并仅从模块中导出功能
在index.js
const path = require('path');
...
for(const file of commandFiles){
const command = require(`./commands/${file}`);
client.commands.set(path.parse(file).name, command);
}
...
并且在mute.js
...
module.exports = async (message, args) => { ... };
从https://www.tutorialsteacher.com/nodejs/nodejs-module-exports学习 nodejs 模块的基础知识
推荐阅读
- c# - 在同一个命名空间中使用 MongoDB.Driver.Linq 和 System.Linq 可能会遇到什么冲突?
- r - 在 R 中有条件地格式化不同的时间序列类型
- javascript - DNS更改后如何解决私人浏览器问题
- javascript - 如何遍历包含数组的对象?
- reactjs - 在 RowRenderer 上未定义 renderBaseRow
- django - 在 Django 中设置两层用户系统
- r - 无法从 URL 安装“未知包”
- flutter - AnimatedContainer 不能在有限和无界约束之间进行插值
- javascript - CommonJS 和 Jest 模拟 - 导出的函数未被模拟
- php - Composer.Json 使用具有另一个 composer.json 文件的私有存储库,并在拉存储库后运行它