Browse Source

Converting the rest of the modules

develop
Drew Short 5 years ago
parent
commit
5f75745256
  1. 8
      src/module/admin.ts
  2. 4
      src/module/example.ts
  3. 10
      src/module/giphy.ts
  4. 10
      src/module/help.ts
  5. 43
      src/module/szurubooru/client.ts
  6. 6
      src/module/szurubooru/index.ts

8
src/module/admin.js → 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<string>) {
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();
let module = new AdminModule();
export { module };

4
src/module/example.js → src/module/example.ts

@ -34,4 +34,6 @@ class ExampleModule extends AbstractModule {
}
}
exports.module = new ExampleModule();
let module = new ExampleModule();
export { module };

10
src/module/giphy.js → src/module/giphy.ts

@ -19,11 +19,11 @@ class GiphyModule extends AbstractModule {
this.defaultCommand = 'search';
}
getConfigSensitiveFields() {
getConfigSensitiveFields(): Array<string> {
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<string>) {
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();
let module = new GiphyModule();
export { module };

10
src/module/help.js → 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<string, CallableFunction>) {
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 <command>`;
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<string>) {
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<string, CallableFunction>) {
return new HelpModule(commandMap);
}
exports.create = create;
export { create };

43
src/module/szurubooru/client.js → 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<string> = []) {
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<number> {
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<string> = []) {
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;
export { SzurubooruClient as Client };

6
src/module/szurubooru/index.js → 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<string>) {
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();
let module = new SzurubooruModule();
export { module };
Loading…
Cancel
Save