- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我对 JS 和编程都很陌生,所以请记住这一点进行解释。我知道这个问题的表述不太好,但我想不出如何更好地表述它,所以请随意编辑它。我正在使用discord.js 制作一个discord 机器人。我做了一个名为“测试”的命令。我的代码如下(在名为 test.js 的文件中):
module.exports = {
name: 'test',
description: 'Test command. Sends back "Test successful."',
prefixType: 'devPrefix',
execute(msg, args) {
if (!args.length){
msg.channel.send('Test successful.');
} else {
msg.channel.send('Test successful. You entered ' + args.length + ' argument(s).')
}
},
};
如何获取值 prefixType
并在我的主 index.js 文件中使用它?然而,该解决方案需要适用于文件夹中的任何文件(称为“命令”),而不仅仅是 test.js。
如果这有帮助,我将在主 index.js 文件中包含处理和执行命令的代码:
const Discord = require('discord.js');
const fs = require('fs'); //For commands (fs = file system)
const botClient = new Discord.Client;
const CONFIG = require('./config.json');
//Get the commands
botClient.commands = new Discord.Collection();
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
botClient.commands.set(command.name, command);
}
botClient.once('ready', () => {
console.log('Bot online and ready.');
});
botClient.on('message', msg => {
if ((!msg.content.startsWith(CONFIG.prefix) && !msg.content.startsWith(CONFIG.devPrefix))
|| msg.author.bot) return;
const args = msg.content.slice(CONFIG.prefix.length).split(' ');
const commandName = args.shift().toLowerCase();
//If the command doesn't exist
if (!botClient.commands.has(commandName)){
msg.reply("That command does not exist. Do a.commands for a list of all commands");
return;
}
const command = botClient.commands.get(commandName);
try {
command.execute(msg, args);
} catch (error) {
console.error(error);
console.log('Error when trying to get and execute a command.');
msg.reply('There was an error trying to execute that command.');
}
});
谢谢
最佳答案
由于您将其导出到命令文件中(稍后需要并将其放入 botClient.commands
集合中),因此您可以在任何其他您有权访问 的地方访问
。 prefixType
>客户端botClient.commands.get('test').prefixType
将为您提供该属性。
此外,您可以通过执行 message 从大多数 Discord.js 结构中访问
或client
属性,例如 Message
或 User
。客户端user.client
。由于您的命令集合已附加到您的客户端对象,因此您可以像上面的示例一样访问它。
关于javascript - 如何使用 module.exports 从任何 .js 文件中获取值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61063686/
在我的项目代码优化期间,我替换了 var 的所有实例关键词 let因为我认为 var 没有特别的用途.为此,我在所有文件中执行了“查找和替换”操作。 在一些自定义模块中,我使用了如下语句: var e
我遇到过使用“双”导出来创建当前组件的情况。您能否解释一下它是否有真正的用途,或者只是作者的偏好? import React from 'react' import DuckImage from '.
我编写了一个简单的脚本来创建用户 (TestV100)、在该架构中创建表 (Xy100) 并将制表符分隔的平面文件从 hadoop 导出到此 Oracle 表。 这是 shell 脚本:-Export
我真的很困惑: export const foo 导出默认 foo module.exports = foo; 我知道这些是非常基本的,但有人可以区分并向我解释这些。我真的很想明白。 最佳答案 让我们
今天我在我的 AngularJS 项目中采用了 Browserify,但是我还不清楚。在所有示例和博客文章中,我都看到了这样的东西: /app.js: require('./messages'); a
我正在寻找一种方法: 隐藏 HTML 页面结果上的标题 导出时在 highcharts 图表上显示标题(PDF、PNG、JPEG 或打印) 我不知道如何继续。有人可以帮助我吗? 最佳答案 您可以在导出
在我的应用程序的首选项中,我调用以特殊方式处理某些参数的 Activity 。到目前为止它运作良好。但是,为了创建试用版和专业版,我刚刚创建了一个包含所有代码的库和一个调用该库的新演示应用程序。除了由
pentaho cde 导出组件正在为我工作,它导出表数据,文件名为 cda-export.csv,我可以自动设置文件名吗? 最佳答案 尝试使用参数 {filename : 'myfilename
我想从 Gephi 的预览选项卡中导出 SVG/PDF/PNG。导出的网络包括所有节点,但切断了左右两侧的节点标签。如何获得不切断标签的导出 pdf? 最佳答案 您必须通过控制选项 来调整输出的大小。
如标题所述,我在尝试使用我的 perl 模块时遇到此错误,但我不知道它是什么意思,而且我似乎无法在 Internet 上找到任何明确的结果。我的代码由 3 个文件组成:一个脚本 (myApp.pl),
我曾经每天下载一个 firebase 实时数据库的节点,通过导出该节点的 .JSON 文件来监控一些输出。 JSON 文件本身大约 8MB。 最近,我开始收到一个错误: “导出 JSON 无法导出在单
之间有什么区别 export * as bar from 'foo' 和 export { default as bar } from 'foo' 在我的特殊情况下,我尝试了以下两种方法,它们都有效,
我想我要求的是不可能的OOB,但我想确认一下。 我们正在升级到 ES6(使用 Babel)。项目是一个网站,使用 AMD (requirejs)。我想将实用程序模块 (foolib) 转换为 ES6,
我一直在我的 React 中广泛使用命名导出和默认导出,我遇到了这 2 个相似的语法。 从'./Button'导出默认值; export { default } from './Button'; 有人
在此页面 ( http://docs.nodejitsu.com/articles/getting-started/what-is-require ) 上,它指出“如果要将导出对象设置为函数或新对象,
在此页面 ( http://docs.nodejitsu.com/articles/getting-started/what-is-require ) 上,它指出“如果要将导出对象设置为函数或新对象,
我在运行 flow check 时收到此错误,但我不确定这是什么意思。 Cannot use exports as a type because exports is a value. To get
module.export和export有什么区别? 如果module.export对象中有一些属性怎么办?export.xx 会无效吗? 最佳答案 首先是 exports 和 module.expo
我正在学习 typescript 。在研究一些源文件时,我发现声明文件 .d 中有时有 export declare ... 有时只有 export ... .ts. 经过一些测试,在我看来它们是等价
假设我有一个要导出的变量。有什么区别 export const a = 1; 对比 export let a = 1; 我理解const 和let 之间的区别,但是当你导出它们时,有什么区别? 最佳答
我是一名优秀的程序员,十分优秀!