传递机器人配置文件,以便可以从客户端全局访问它
Pass the bot configuration file so that it is accessible globally from Client
我想将我的 bot 的配置文件 (config.json
) 传递到 bot 客户端,这样我就可以获得例如 bot 的所有者 ID,而不是在我只需要 bot 的每个命令中指定它所有者访问,或嵌入页脚文本,例如:
async execute(client, message, args, footerTxt) {
if (message.author.id == BotOwnerId) {
我希望能够做到这一点
async execute(client, message, args){
if (message.author.id == client.config.ownerID) {
我已经尝试运行通过命令执行
try {
await client.commands.get(command).execute(client, message, args, footerTxt, config);
} catch (error) {
导致
TypeError: Cannot read properties of undefined (reading 'ownerID')
at Object.execute
config.json
{
"ownerID": "XXXXXXXXXXX",
"someOtherStuff": [],
"someMoreStuff": { "id": 1, "name": "Test" }
}
index(假设config.json在同一个文件夹)
const config = require('./config.json')
await client.commands.get(command).execute(client, message, args, footerTxt, config);
命令:
if (message.author.id == config.ownerID) {
为什么不简单地要求索引中的配置文件并将其通过参数传递给命令?或者在命令中要求它?
如您所愿,但它应该可以解决问题
我想将我的 bot 的配置文件 (config.json
) 传递到 bot 客户端,这样我就可以获得例如 bot 的所有者 ID,而不是在我只需要 bot 的每个命令中指定它所有者访问,或嵌入页脚文本,例如:
async execute(client, message, args, footerTxt) {
if (message.author.id == BotOwnerId) {
我希望能够做到这一点
async execute(client, message, args){
if (message.author.id == client.config.ownerID) {
我已经尝试运行通过命令执行
try {
await client.commands.get(command).execute(client, message, args, footerTxt, config);
} catch (error) {
导致
TypeError: Cannot read properties of undefined (reading 'ownerID')
at Object.execute
config.json
{
"ownerID": "XXXXXXXXXXX",
"someOtherStuff": [],
"someMoreStuff": { "id": 1, "name": "Test" }
}
index(假设config.json在同一个文件夹)
const config = require('./config.json')
await client.commands.get(command).execute(client, message, args, footerTxt, config);
命令:
if (message.author.id == config.ownerID) {
为什么不简单地要求索引中的配置文件并将其通过参数传递给命令?或者在命令中要求它? 如您所愿,但它应该可以解决问题