如何在模块中填充集合以便在 events/commands 中使用
How to have collections populated in a module for use inside events/commands
我知道要填充公会和频道等集合,机器人必须已经登录,即它可以在命令文件和事件中使用。我拥有的是一个模块,它将在我的控制 discord 服务器中显示我的日志,我希望能够在我的事件和命令中引用这个模块。
我已经尝试在事件中导入模块,以及其他有意义的选项。
这是我模块中的代码
const Discord = require('discord.js')
const bot = new Discord.Client()
const CC = '../settings/control-center.json'
const CCFile = require(CC)
const GUILD = bot.guilds.get(CCFile.GUILD)
const STARTUP = bot.channels.get(CCFile.STARTUP)
const INFO = bot.channels.get(CCFile.INFO)
const ERRORS = bot.channels.get(CCFile.ERRORS)
const RESTART = bot.channels.get(CCFile.RESTART)
const EXECUTABLES = bot.channels.get(CCFile.EXECUTABLES)
class Control {
/**
* Implement control center logging
* @param {string} message - What to send to the startup channel
* @return {string} The final product being sent to the startup channel
*/
STARTUP(message) {
return STARTUP.send(`${message}`)
}
}
module.exports = Control
我希望内部可以全局使用这个module/the函数,这样我的代码可以更紧凑。那么我怎样才能让这个代码只在机器人登录后加载?
在您的模块代码中,您正在创建一个新的 Discord 客户端实例,并且从不调用登录方法。
更好的方法是在您的方法中传递 bot 对象
模块文件
const CC = '../settings/control-center.json';
const CCFile = require(CC);
const GUILD = CCFile.GUILD;
const STARTUP = CCFile.STARTUP;
const INFO = CCFile.INFO;
const ERRORS = CCFile.ERRORS;
const RESTART = CCFile.RESTART;
const EXECUTABLES = CCFile.EXECUTABLES;
class Control {
startup(bot, message) {
return bot.channels.get(STARTUP).send(message);
}
}
module.exports = Control
应用程序文件
// Use the bot here
const Discord = require('discord.js')
const bot = new Discord.Client()
const control = require('path/to/control.js');
[...]
// to send a message when ready, try something like this
bot.on('ready', () => {
control.startup(bot, 'bot is ready');
});
// don't forget to login
bot.login('YOUR-TOKEN-HERE');
我知道要填充公会和频道等集合,机器人必须已经登录,即它可以在命令文件和事件中使用。我拥有的是一个模块,它将在我的控制 discord 服务器中显示我的日志,我希望能够在我的事件和命令中引用这个模块。
我已经尝试在事件中导入模块,以及其他有意义的选项。
这是我模块中的代码
const Discord = require('discord.js')
const bot = new Discord.Client()
const CC = '../settings/control-center.json'
const CCFile = require(CC)
const GUILD = bot.guilds.get(CCFile.GUILD)
const STARTUP = bot.channels.get(CCFile.STARTUP)
const INFO = bot.channels.get(CCFile.INFO)
const ERRORS = bot.channels.get(CCFile.ERRORS)
const RESTART = bot.channels.get(CCFile.RESTART)
const EXECUTABLES = bot.channels.get(CCFile.EXECUTABLES)
class Control {
/**
* Implement control center logging
* @param {string} message - What to send to the startup channel
* @return {string} The final product being sent to the startup channel
*/
STARTUP(message) {
return STARTUP.send(`${message}`)
}
}
module.exports = Control
我希望内部可以全局使用这个module/the函数,这样我的代码可以更紧凑。那么我怎样才能让这个代码只在机器人登录后加载?
在您的模块代码中,您正在创建一个新的 Discord 客户端实例,并且从不调用登录方法。 更好的方法是在您的方法中传递 bot 对象
模块文件
const CC = '../settings/control-center.json';
const CCFile = require(CC);
const GUILD = CCFile.GUILD;
const STARTUP = CCFile.STARTUP;
const INFO = CCFile.INFO;
const ERRORS = CCFile.ERRORS;
const RESTART = CCFile.RESTART;
const EXECUTABLES = CCFile.EXECUTABLES;
class Control {
startup(bot, message) {
return bot.channels.get(STARTUP).send(message);
}
}
module.exports = Control
应用程序文件
// Use the bot here
const Discord = require('discord.js')
const bot = new Discord.Client()
const control = require('path/to/control.js');
[...]
// to send a message when ready, try something like this
bot.on('ready', () => {
control.startup(bot, 'bot is ready');
});
// don't forget to login
bot.login('YOUR-TOKEN-HERE');