event and timer helpers
This commit is contained in:
		
							parent
							
								
									feabf7de8c
								
							
						
					
					
						commit
						6501125b26
					
				
					 3 changed files with 82 additions and 0 deletions
				
			
		| 
						 | 
				
			
			@ -6,6 +6,8 @@ const {resolve} = require("path");
 | 
			
		|||
const config = require("../config.json");
 | 
			
		||||
const Command = require("./lib/command.js");
 | 
			
		||||
const CommandDispatcher = require("./lib/commandDispatcher.js");
 | 
			
		||||
const events = require("./lib/events.js");
 | 
			
		||||
const timer = require("./lib/timer.js");
 | 
			
		||||
 | 
			
		||||
const bot = new Eris(config.token, {
 | 
			
		||||
  defaultImageFormat: "png",
 | 
			
		||||
| 
						 | 
				
			
			@ -32,6 +34,8 @@ global.hf = {
 | 
			
		|||
  config,
 | 
			
		||||
  commands,
 | 
			
		||||
  registerCommand,
 | 
			
		||||
  events,
 | 
			
		||||
  timer,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
for (const file of fs.readdirSync(resolve(__dirname, "modules"))) {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										43
									
								
								src/lib/events.js
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										43
									
								
								src/lib/events.js
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,43 @@
 | 
			
		|||
const logger = require("npmlog");
 | 
			
		||||
 | 
			
		||||
const eventTable = {};
 | 
			
		||||
 | 
			
		||||
function getTable() {
 | 
			
		||||
  return eventTable;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function add(event, identifier, callback) {
 | 
			
		||||
  if (eventTable[event] == null) eventTable[event] = {};
 | 
			
		||||
 | 
			
		||||
  if (eventTable[event][identifier] != null) {
 | 
			
		||||
    hf.bot.off(event, eventTable[event][identifier]);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  function wrapper() {
 | 
			
		||||
    try {
 | 
			
		||||
      callback.apply(this, arguments);
 | 
			
		||||
    } catch (error) {
 | 
			
		||||
      logger.error(
 | 
			
		||||
        "hf:event",
 | 
			
		||||
        `Event "${event}:${identifier}" failed: ${error}`
 | 
			
		||||
      );
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  eventTable[event][identifier] = wrapper;
 | 
			
		||||
  hf.bot.on(event, wrapper);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function remove(event, identifier) {
 | 
			
		||||
  if (eventTable[event] == null) eventTable[event] = {};
 | 
			
		||||
 | 
			
		||||
  if (eventTable[event][identifier] != null) {
 | 
			
		||||
    hf.bot.off(event, eventTable[event][identifier]);
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
module.exports = {
 | 
			
		||||
  getTable,
 | 
			
		||||
  add,
 | 
			
		||||
  remove,
 | 
			
		||||
};
 | 
			
		||||
							
								
								
									
										35
									
								
								src/lib/timer.js
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										35
									
								
								src/lib/timer.js
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,35 @@
 | 
			
		|||
const logger = require("npmlog");
 | 
			
		||||
 | 
			
		||||
const timerTable = {};
 | 
			
		||||
 | 
			
		||||
function getTable() {
 | 
			
		||||
  return timerTable;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function add(identifier, callback, delay) {
 | 
			
		||||
  if (timerTable[identifier] != null) {
 | 
			
		||||
    clearInterval(timerTable[identifier]);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  function wrapper() {
 | 
			
		||||
    try {
 | 
			
		||||
      callback();
 | 
			
		||||
    } catch (error) {
 | 
			
		||||
      logger.error("hf:timer", `Timer "${identifier}" failed: ${error}`);
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  timerTable[identifier] = setInterval(wrapper, delay);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function remove(identifier) {
 | 
			
		||||
  if (timerTable[identifier] != null) {
 | 
			
		||||
    clearInterval(timerTable[identifier]);
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
module.exports = {
 | 
			
		||||
  getTable,
 | 
			
		||||
  add,
 | 
			
		||||
  remove,
 | 
			
		||||
};
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue