From 51be142c1395f7a092f443dd529cd0e5d8c6e80f Mon Sep 17 00:00:00 2001 From: Drew Short Date: Fri, 10 Jan 2020 15:02:32 -0600 Subject: [PATCH] Converted the remaining source files to TS --- src/{bot.js => bot.ts} | 16 +++++++++++----- src/{engine.js => engine.ts} | 17 +++++++++++++++-- src/{utility.js => utility.ts} | 31 +++++++++++++++---------------- 3 files changed, 41 insertions(+), 23 deletions(-) rename src/{bot.js => bot.ts} (95%) rename src/{engine.js => engine.ts} (92%) rename src/{utility.js => utility.ts} (74%) diff --git a/src/bot.js b/src/bot.ts similarity index 95% rename from src/bot.js rename to src/bot.ts index 0e3e4e3..a4143ba 100644 --- a/src/bot.js +++ b/src/bot.ts @@ -5,6 +5,12 @@ let utility = require('./utility'); let { logger } = require('./logging'); class Bot { + + client: any; + config: any; + buildInfo: string; + connected: boolean; + constructor(config, buildInfo) { this.config = config; this.buildInfo = buildInfo; @@ -14,14 +20,14 @@ class Bot { /** * Initialize the bot connection */ - init(messageCallback) { + init(messageCallback: any) { logger.info("Creating Matrix Client") this.client = sdk.createClient({ baseUrl: this.config.baseUrl, userId: this.config.userId }); - this.client.on("sync", async (state, previousState, data) => { + this.client.on("sync", async (state: any, previousState: any, data: any) => { switch (state) { case "PREPARED": this.connected = true; @@ -44,9 +50,9 @@ class Bot { } }); - this.client.on("RoomMember.membership", (event, member) => { + this.client.on("RoomMember.membership", (event: any, member: any) => { if (member.membership === "invite" - && this.config.admin.indexOf(ember.userId) >= 0) { + && this.config.admin.indexOf(member.userId) >= 0) { this.client.joinRoom(member.roomId).done(() => { logger.info("Auto-joined %s", member.roomId); }); @@ -184,4 +190,4 @@ function create(config) { return new Bot(config, buildInfo); } -exports.create = create; \ No newline at end of file +export { create }; \ No newline at end of file diff --git a/src/engine.js b/src/engine.ts similarity index 92% rename from src/engine.js rename to src/engine.ts index cbcdb83..74f955c 100644 --- a/src/engine.js +++ b/src/engine.ts @@ -1,3 +1,5 @@ +import { AbstractModule } from "./module/abstract"; + let { logger } = require('./logging'); let { modules } = require('./module/index'); let trie = require('trie-prefix-tree'); @@ -11,6 +13,16 @@ let { sanitizeConfig } = require('./config'); let commandPrefix = '!'; class Engine { + + config: any; + bot: any; + modules: Array; + moduleMap: Map; + commands: Array; + commandMap: Map; + commandRadixTree: any; + helpModule: any; + constructor(config, bot, modules) { this.config = config; this.bot = bot; @@ -92,7 +104,8 @@ class Engine { /* Capture Exit Conditions */ - ["SIGINT", "SIGTERM"].forEach((signature) => { + let signals: NodeJS.Signals[] = ["SIGINT", "SIGTERM"]; + signals.forEach((signature: NodeJS.Signals) => { process.on(signature, async () => { await this.bot.sendStatusShutdown() .then(() => { @@ -156,4 +169,4 @@ function create(config, bot) { return new Engine(config, bot, modules) } -exports.create = create; \ No newline at end of file +export { create }; \ No newline at end of file diff --git a/src/utility.js b/src/utility.ts similarity index 74% rename from src/utility.js rename to src/utility.ts index 00f962d..9984587 100644 --- a/src/utility.js +++ b/src/utility.ts @@ -1,7 +1,7 @@ let fs = require('fs'); let { logger } = require('./logging'); -function getShortestPrefix(radixTree, key, sliceSize) { +function getShortestPrefix(radixTree: any, key: string, sliceSize: number) { let shortKey = key.substring(0, sliceSize); let keyCount = radixTree.countPrefix(shortKey); if (keyCount < 1) { @@ -16,8 +16,8 @@ function getShortestPrefix(radixTree, key, sliceSize) { return getShortestPrefix(radixTree, key, sliceSize + 1); } -function toISODateString(d) { - function pad(n) { return n < 10 ? '0' + n : n } +function toISODateString(d: Date) { + function pad(n: number) { return n < 10 ? '0' + n : n } return d.getUTCFullYear() + '-' + pad(d.getUTCMonth() + 1) + '-' + pad(d.getUTCDate()) + 'T' @@ -39,17 +39,17 @@ function getBuildInfo() { } } -function sleep(ms) { +function sleep(ms: number) { return new Promise(resolve => { setTimeout(resolve, ms) }) } -function isString(s) { +function isString(s: any) { return typeof (s) === 'string' || s instanceof String; } -function isFunction(f) { +function isFunction(f: any) { return f && {}.toString.call(f) === '[object Function]'; } @@ -61,26 +61,25 @@ function isFunction(f) { * * @param {*} initialObj The starting object to derive the from * @param {*} sentinelPrototype The prototype that represents the end of the line - * @param {*} filterFunc A fioltering function for the return names + * @param {*} filterFunc A filtering function for the return names */ -function getObjectKeysToPrototype(initialObj, sentinelPrototype, filterFunc = (e) => true) { +function getObjectKeysToPrototype(initialObj: any, sentinelPrototype: any, filterFunc: any = (e: any) => true) { let prototypeChain = [] var targetPrototype = initialObj; while (Object.getPrototypeOf(targetPrototype) && targetPrototype !== sentinelPrototype) { targetPrototype = Object.getPrototypeOf(targetPrototype); prototypeChain.push(targetPrototype); } - // console.log("Prototype chain: %s", prototypeChain); let completePropertyNames = prototypeChain.map((obj) => { return Object.getOwnPropertyNames(obj); }) return [Object.getOwnPropertyNames(initialObj)].concat.apply([], completePropertyNames).filter(filterFunc); } -exports.getShortestPrefix = getShortestPrefix; -exports.toISODateString = toISODateString; -exports.getBuildInfo = getBuildInfo; -exports.sleep = sleep; -exports.isString = isString; -exports.isFunction = isFunction; -exports.getObjectKeysToPrototype = getObjectKeysToPrototype; \ No newline at end of file +export { getShortestPrefix }; +export { toISODateString }; +export { getBuildInfo }; +export { sleep }; +export { isString }; +export { isFunction }; +export { getObjectKeysToPrototype }; \ No newline at end of file