From 5f757452562133b446c0ffaeb45bdb8445aad83b Mon Sep 17 00:00:00 2001 From: Drew Short Date: Fri, 10 Jan 2020 14:45:42 -0600 Subject: [PATCH] Converting the rest of the modules --- src/module/{admin.js => admin.ts} | 8 ++-- src/module/{example.js => example.ts} | 4 +- src/module/{giphy.js => giphy.ts} | 10 +++-- src/module/{help.js => help.ts} | 10 ++--- .../szurubooru/{client.js => client.ts} | 43 +++++++++++-------- src/module/szurubooru/{index.js => index.ts} | 6 ++- 6 files changed, 48 insertions(+), 33 deletions(-) rename src/module/{admin.js => admin.ts} (89%) rename src/module/{example.js => example.ts} (95%) rename src/module/{giphy.js => giphy.ts} (86%) rename src/module/{help.js => help.ts} (84%) rename src/module/szurubooru/{client.js => client.ts} (78%) rename src/module/szurubooru/{index.js => index.ts} (95%) diff --git a/src/module/admin.js b/src/module/admin.ts similarity index 89% rename from src/module/admin.js rename to src/module/admin.ts index 106a78f..a181d2a 100644 --- a/src/module/admin.js +++ b/src/module/admin.ts @@ -23,7 +23,7 @@ class AdminModule extends AbstractModule { /** * Override to only permit recognized admin users to access the plugin */ - handleMessage(event, room, callback) { + handleMessage(event: any, room: any, callback: CallableFunction) { if (this.getGlobal("admins", []).includes(event.sender.userId)) { logger.debug("Authorized %s for admin action", event.sender.userId); super.handleMessage(event, room, callback); @@ -42,7 +42,7 @@ class AdminModule extends AbstractModule { * * @param {...any} args */ - cmd_config(event, ...args) { + cmd_config(event: any, ...args: Array) { if (this._global_config != null) { let configBody = JSON.stringify(sanitizeConfig(this._global_config), null, 2) return message.createMarkdown(configBody, "```" + configBody + "```"); @@ -51,4 +51,6 @@ class AdminModule extends AbstractModule { } } -exports.module = new AdminModule(); \ No newline at end of file +let module = new AdminModule(); + +export { module }; \ No newline at end of file diff --git a/src/module/example.js b/src/module/example.ts similarity index 95% rename from src/module/example.js rename to src/module/example.ts index bebb944..46d11f6 100644 --- a/src/module/example.js +++ b/src/module/example.ts @@ -34,4 +34,6 @@ class ExampleModule extends AbstractModule { } } -exports.module = new ExampleModule(); \ No newline at end of file +let module = new ExampleModule(); + +export { module }; \ No newline at end of file diff --git a/src/module/giphy.js b/src/module/giphy.ts similarity index 86% rename from src/module/giphy.js rename to src/module/giphy.ts index e5f0244..82628c4 100644 --- a/src/module/giphy.js +++ b/src/module/giphy.ts @@ -19,11 +19,11 @@ class GiphyModule extends AbstractModule { this.defaultCommand = 'search'; } - getConfigSensitiveFields() { + getConfigSensitiveFields(): Array { return ["apiKey"]; } - getGiphySearch(term) { + getGiphySearch(term: string) { let url = this.get("endpoint") + '/gifs/search?api_key=' + this.get("apiKey") + '&q=' + term + '&limit=1'; logger.debug("Requesting: %s", url.replace(this.get("apiKey"), '******')); return axios.get(url); @@ -38,7 +38,7 @@ class GiphyModule extends AbstractModule { * * @param {...any} args */ - cmd_search(event, ...args) { + cmd_search(event: any, ...args: Array) { return this.getGiphySearch(args[0]) .then((response) => { // logger.debug("Giphy response: %o", response.data.data[0].url); @@ -47,4 +47,6 @@ class GiphyModule extends AbstractModule { } } -exports.module = new GiphyModule(); \ No newline at end of file +let module = new GiphyModule(); + +export { module }; \ No newline at end of file diff --git a/src/module/help.js b/src/module/help.ts similarity index 84% rename from src/module/help.js rename to src/module/help.ts index ab3d9dd..9196963 100644 --- a/src/module/help.js +++ b/src/module/help.ts @@ -6,7 +6,7 @@ let { AbstractModule } = require('./abstract'); let { logger } = require('../logging'); class HelpModule extends AbstractModule { - constructor(commandMap) { + constructor(commandMap: Map) { super( "Help", "Provide helpful information about other modules.", @@ -17,7 +17,7 @@ class HelpModule extends AbstractModule { this.defaultCommand = 'help'; } - _default_help_message() { + _default_help_message(): string { let help = `!help `; for (let command of this._commandList) { help += "\n!help " + command + " : " + this._commandMap.get(command).description; @@ -27,7 +27,7 @@ class HelpModule extends AbstractModule { /* Commands */ - cmd_help(event, ...args) { + cmd_help(event: any, ...args: Array) { logger.debug("%o", args) if (args.length < 1) { return this._default_help_message(); @@ -45,8 +45,8 @@ class HelpModule extends AbstractModule { } } -function create(commandMap) { +function create(commandMap: Map) { return new HelpModule(commandMap); } -exports.create = create; \ No newline at end of file +export { create }; \ No newline at end of file diff --git a/src/module/szurubooru/client.js b/src/module/szurubooru/client.ts similarity index 78% rename from src/module/szurubooru/client.js rename to src/module/szurubooru/client.ts index 5cb4ab3..31aa723 100644 --- a/src/module/szurubooru/client.js +++ b/src/module/szurubooru/client.ts @@ -13,10 +13,17 @@ function getRandomInt(min, max) { class SzurubooruClient { - _randomSearchQueryValues = "type:image,animated,video sort:random"; - _randomSearchUrlTemplate = "%s/posts/?offset=%s&limit=%d&query=%s"; + url: string; + baseUrl: string; + username: string; + token: string; - constructor(url, username, token) { + randomImageCount: number; + + _randomSearchQueryValues: string = "type:image,animated,video sort:random"; + _randomSearchUrlTemplate: string = "%s/posts/?offset=%s&limit=%d&query=%s"; + + constructor(url: string, username: string, token: string) { this.url = url; this.baseUrl = url.replace('/api', ''); this.username = username; @@ -24,7 +31,7 @@ class SzurubooruClient { this.updateRandomScope(); } - getRandomSearchUrl(offset, limit, tags = []) { + getRandomSearchUrl(offset: number, limit: number, tags: Array = []) { var searchQuery = this._randomSearchQueryValues; if (tags !== null && tags.length > 0) { searchQuery = this._randomSearchQueryValues + tags.join(" "); @@ -38,7 +45,7 @@ class SzurubooruClient { * @param {*} limit The content limit of a single page * @param {*} total The number of objects to search through */ - getValidOffsetRange(limit, total) { + getValidOffsetRange(limit: number, total: number): Array { let offsetOverflow = total % limit; var offsetCeiling = (Math.round(total / limit)); if (offsetOverflow === 0) { @@ -58,12 +65,12 @@ class SzurubooruClient { * @param {*} self * @param {*} imageCount */ - getRandomSearchParameters(imageCount = null) { + getRandomSearchParameters(imageCount?: number) { let resultLimit = 10; var offsetRange = [0, 1]; if (imageCount === null) { offsetRange = this.getValidOffsetRange(resultLimit, this.randomImageCount); - } else { + } else { offsetRange = this.getValidOffsetRange(resultLimit, imageCount); } return [resultLimit, getRandomInt(offsetRange[0], offsetRange[1])]; @@ -73,27 +80,26 @@ class SzurubooruClient { async updateRandomScope() { let client = this; let target = this.getRandomSearchUrl(0, 1); - await this.makeGetRequest(target).then((result) => { + await this.makeGetRequest(target).then((result: any) => { client.randomImageCount = result.total; }); - + } - makeGetRequest(url) { + makeGetRequest(url: string) { logger.debug("Making get request to %s", url); return axios.get(url, { headers: { "Authorization": "Token " + Buffer.from(this.username + ':' + this.token).toString('base64'), "Accept": "application/json" } - }).then((response) => { + }).then((response: any) => { logger.debug("Recieved: %o", response.data); return response.data; - }, (err) => { + }, (err: any) => { logger.error("Unexpected client error: %o", err); return null; }); - return self._handle_response("GET", url, None, response) } /** @@ -102,7 +108,7 @@ class SzurubooruClient { * @param {*} self * @param {*} tags */ - getRandomPost(tags = []) { + getRandomPost(tags: Array = []) { let client = this; var resultLimit = 0; var randomSearchOffset = 0; @@ -113,7 +119,7 @@ class SzurubooruClient { } let searchUrl = this.getRandomSearchUrl(randomSearchOffset, resultLimit, tags); return this.makeGetRequest(searchUrl) - .then((data) => { + .then((data: any) => { if (data !== null) { let limitSpace = Math.min(resultLimit, data.results.length - 1); let randomPost = data.results[getRandomInt(0, limitSpace)]; @@ -121,10 +127,11 @@ class SzurubooruClient { } else { return null } - }, (err) => { - + }, (err: any) => { + logger.error("Unexpected error: %o", err); + return null; }) } } -exports.Client = SzurubooruClient; \ No newline at end of file +export { SzurubooruClient as Client }; \ No newline at end of file diff --git a/src/module/szurubooru/index.js b/src/module/szurubooru/index.ts similarity index 95% rename from src/module/szurubooru/index.js rename to src/module/szurubooru/index.ts index 3ac6cda..72a1e6c 100644 --- a/src/module/szurubooru/index.js +++ b/src/module/szurubooru/index.ts @@ -51,7 +51,7 @@ class SzurubooruModule extends AbstractModule { * All methods starting with cmd_ will be parsed at initialization to expose those methods as commdands to the user */ - cmd_random(event, ...args) { + cmd_random(event: any, ...args: Array) { return this.client.getRandomPost().then((data) => { logger.info("Random post: %o", data); return data; @@ -59,4 +59,6 @@ class SzurubooruModule extends AbstractModule { } } -exports.module = new SzurubooruModule(); \ No newline at end of file +let module = new SzurubooruModule(); + +export { module }; \ No newline at end of file