Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

node.js - Discord.js: how to use module.exports

i am currently trying to create a discord-bot with discord.js using the official documentation / instructions (https://discordjs.guide/). At the point "Command handling" I have to export the different commands into own files and then export them, so that I can use them in the main-file. I have already done some research and looked at how exporting in node.js works. For example

function add (a, b) {
return a + b;
}

function subtract (a, b) {
return a - b;
}

module.exports = {
    add: add,
    subtract: subtract
}

However, I don't understand why this function exports like this:

module.exports = {
    name: 'ping',
    description: 'Ping!',
    execute(message, args) {
            message.channel.send('Pong.');
    },
};

I didn't find anything on the internet about the "execute" function and I don't understand why I can't just export it like this:

function ping(message, args) {
    message.channel.send('Pong!');
}
module.exports = {
    ping: ping,
};

or like this:

exports.ping = function (message, args) {
    message.channel.send('Pong!');
};

Would be nice if someone could explain this to me, especially the "execute" function :)

question from:https://stackoverflow.com/questions/65916935/discord-js-how-to-use-module-exports

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...