@dexare/dbots
Version:
A Dexare module for the NPM package dbots
98 lines (97 loc) • 3.7 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const dexare_1 = require("dexare");
const dbots_1 = require("dbots");
class DBotsModule extends dexare_1.DexareModule {
constructor(client) {
super(client, {
name: 'dbots',
description: 'A module for the NPM package dbots'
});
this.filePath = __filename;
}
/** @hidden */
load() {
this.registerEvent('ready', this.onReady.bind(this));
}
/** @hidden */
unload() {
this.unregisterAllEvents();
if (this.poster)
this.poster.stopInterval();
}
/** The keys for the poster. Defaults to `dbots.keys` in the config. */
get keys() {
if (!this._keys)
this._keys = (this.client.config.dbots && this.client.config.dbots.keys) || {};
return this._keys;
}
set keys(keys) {
this._keys = keys;
if (this.poster)
this.poster.apiKeys = keys;
}
/** The keys for the poster. Defaults to `dbots.customServices` in the config. */
get customServices() {
if (!this._customServices)
this._customServices = (this.client.config.dbots && this.client.config.dbots.customServices) || [];
return this._customServices;
}
set customServices(customServices) {
this._customServices = customServices;
if (this.poster)
this.poster.customServices = customServices;
}
/** The interval for the poster. Defaults to `dbots.interval` in the config. */
get interval() {
if (this._interval === undefined)
this._interval = (this.client.config.dbots && this.client.config.dbots.interval) || 1800000;
return this._interval;
}
set interval(interval) {
this._interval = interval;
if (this.poster)
this.poster.startInterval(interval);
}
/** Short-hand for poster.startInterval */
startInterval(interval) {
return this.poster?.startInterval(interval || this.interval);
}
onReady() {
if (!this.poster) {
this.poster = new dbots_1.Poster({
client: this.client.bot,
apiKeys: this.keys,
useSharding: false,
clientLibrary: 'eris'
});
// Logging
this.poster.addHandler('autopostSuccess', (result) => {
let hostname;
if (Array.isArray(result))
hostname = result.map((r) => r.request.socket.servername).join(', ');
else
hostname = result.request.socket.servername;
this.logger.log(`Auto-posted to ${hostname}.`);
});
this.poster.addHandler('autopostFail', (errors) => {
const errs = Array.isArray(errors) ? errors : [errors];
for (const err of errs) {
const url = err.config?.url;
const hostname = url ? new URL(url).hostname : '[unknown]';
this.logger.error(`Failed to auto-post to ${hostname}.`, err);
}
});
this.logger.log('Initialized poster.');
}
const autopost = this.client.config.dbots?.autopost !== undefined ? this.client.config.dbots.autopost : true;
if (Object.keys(this.keys).length && autopost) {
this.poster.startInterval(this.interval);
this.poster
.post()
.then((r) => this.poster.runHandlers('autopostSuccess', r))
.catch((e) => this.poster.runHandlers('autopostFail', e));
}
}
}
exports.default = DBotsModule;