[英]How do I export this named function through module.exports?
I have a command file for a discord bot that contains the command and a piece of parsing logic contained within a function that I want to reuse within my index.js
我有一个 discord 机器人的命令文件,其中包含我想在我的
index.js
中重用的 function 中包含的命令和一段解析逻辑
// file: ./commands/scrumPrompt.js
// The function
const extractDeets = function (f, scrum) {
let items = [];
let re = new RegExp("(\n[ -]*" + f + ".*)", "g");
let replace = new RegExp("[ -]*" + f + "[ ]+");
for (const item of scrum.matchAll(re)) {
items.push(item[1].trim().replace(replace, ""));
}
return items;
};
// The actual command itself within the same file
module.exports = {
name: "scrum",
usage: `!scrum < followed by your message > as per Standup format - refer !show for showing the format`,
description: "Reply to standup prompt",
async execute(message, args) {
if (message.channel.type === "text") {
if (!args.length)
return message.reply(
"Please Provide your scrum as per the format in help menu !scrum < your message >"
);
else {
if (message.author.id !== -1) {
const client = new MongoClient(MONGO_URI);
try {
const database = client.db(DB_NAME);
const members = database.collection("members");
const query = { user_id: message.author.id };
const membersdetail = await members.findOne(query);
if (membersdetail !== null) {
// since this method returns the matched document, not a cursor, print it directly
//console.log("Adding Scrum for ", membersdetail.email);
let userscrum = args.splice(0).join(" ");
// Check if multiple !scrum commands are present in developer scrum message
if (userscrum.includes("!scrum") == false) {
// Expects notations of "-" to exist
let [f, e, b, o, bl] = ["f", "e", "b", "o", "x"];
let features = extractDeets(f, userscrum);
let enhancements = extractDeets(e, userscrum);
let bugs = extractDeets(b, userscrum);
let others = extractDeets(o, userscrum);
let blockers = extractDeets(bl, userscrum);
.
.
.
};
I want to keep the name of the function as extractDeets()
itself so that it doesn't mess with the usage within the command as well.我想将 function 的名称保留为
extractDeets()
本身,这样它也不会与命令中的用法混淆。 I'm not completely sure how to export it into the index.js
because it's already kind of being imported here:我不完全确定如何将它导出到
index.js
,因为它已经在这里被导入了:
// Imports the command file + adds the command to the bot commands collection
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
bot.commands.set(command.name, command);
}
I'm unsure of how to add the function as another import, maybe I should export it into another file and then import it from there?我不确定如何将 function 添加为另一个导入,也许我应该将它导出到另一个文件然后从那里导入? I'm not sure if that's possible or doable here.
我不确定这在这里是否可行或可行。 I've tried directly importing from here but then the command doesn't work, which is troublesome.
我试过直接从这里导入,但是命令不起作用,很麻烦。
You can do it like this:你可以这样做:
module.exports = { extractDeets };
Later, you can import it like this:稍后,您可以像这样导入它:
const { extractDeets } = require('../your_file');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.