diff --git a/i18n/bot/en.json b/i18n/bot/en.json index a07fbf3c..18537c10 100644 --- a/i18n/bot/en.json +++ b/i18n/bot/en.json @@ -416,6 +416,7 @@ } }, "interactiveConfig": { + "useWeb": "Please use the {{{ configCmd }}} command or our website at {{{ link }}} to configure your bot.", "add": "Enter a value or list of values to add.", "change": "**Current value**\n{{{ current }}}\n\n**Possible values**\n{{{ possible }}}", "current": { diff --git a/src/RequestHandler.ts b/src/RequestHandler.ts new file mode 100644 index 00000000..f47dd972 --- /dev/null +++ b/src/RequestHandler.ts @@ -0,0 +1,47 @@ +import { IMClient } from './client'; + +// tslint:disable-next-line: variable-name +const RequestHandler = require('eris/lib/rest/RequestHandler'); + +export interface RequestStat { + total: number; + succeeded: number; + errors: number; +} + +export class IMRequestHandler extends RequestHandler { + public requestStats: Map = new Map(); + + public constructor(client: IMClient, forceQueueing?: boolean) { + super(client, forceQueueing); + } + + public request(method: string, url: string, auth: boolean, body: any, file: any, _route: string, short: string) { + // This is similar to https://github.com/abalabahaha/eris/blob/master/lib/rest/RequestHandler.js#L46 + // but we don't actually care about rate limits, so no exceptions in grouping + const route = url + .replace(/\/([a-z-]+)\/(?:[0-9]{17,19})/g, (_, p) => `/${p}/:id`) + .replace(/\/reactions\/[^/]+/g, '/reactions/:id') + .replace(/^\/webhooks\/(\d+)\/[A-Za-z0-9-_]{64,}/, '/webhooks/$1/:token'); + + const statKey = `${method}:${route}`; + let info = this.requestStats.get(statKey); + if (!info) { + info = { total: 0, succeeded: 0, errors: 0 }; + this.requestStats.set(statKey, info); + } + + info.total++; + + return super + .request(method, url, auth, body, file, _route, short) + .then((res: any) => { + info.succeeded++; + return res; + }) + .catch((err: any) => { + info.errors++; + throw err; + }); + } +} diff --git a/src/client.ts b/src/client.ts index a1ecd38c..083a8984 100644 --- a/src/client.ts +++ b/src/client.ts @@ -31,6 +31,7 @@ import { StrikesCache } from './moderation/cache/StrikesCache'; import { ModerationService } from './moderation/services/Moderation'; import { MusicCache } from './music/cache/MusicCache'; import { MusicService } from './music/services/MusicService'; +import { IMRequestHandler } from './RequestHandler'; import { botDefaultSettings, BotSettingsObject, guildDefaultSettings } from './settings'; import { BotType, ChannelType, LavaPlayerManager } from './types'; @@ -108,6 +109,7 @@ export class IMClient extends Client { public shardId: number; public shardCount: number; + public requestHandler: IMRequestHandler; public service: ClientServiceObject; private startingServices: IMService[]; public cache: ClientCacheObject; @@ -137,7 +139,6 @@ export class IMClient extends Client { wsErrors: number; cmdProcessed: number; cmdErrors: number; - cmdHttpErrors: Map; }; public disabledGuilds: Set = new Set(); @@ -168,10 +169,10 @@ export class IMClient extends Client { wsWarnings: 0, wsErrors: 0, cmdProcessed: 0, - cmdErrors: 0, - cmdHttpErrors: new Map() + cmdErrors: 0 }; + this.requestHandler = new IMRequestHandler(this); this.version = version; this.type = type; this.instance = instance; diff --git a/src/framework/services/Commands.ts b/src/framework/services/Commands.ts index c10317d3..8071ff00 100644 --- a/src/framework/services/Commands.ts +++ b/src/framework/services/Commands.ts @@ -480,11 +480,6 @@ export class CommandsService extends IMService { if (error) { this.client.stats.cmdErrors++; - if (error.code) { - const num = this.client.stats.cmdHttpErrors.get(error.code) || 0; - this.client.stats.cmdHttpErrors.set(error.code, num + 1); - } - console.error(error); withScope((scope) => { diff --git a/src/framework/services/RabbitMq.ts b/src/framework/services/RabbitMq.ts index 89f46eac..da16b993 100644 --- a/src/framework/services/RabbitMq.ts +++ b/src/framework/services/RabbitMq.ts @@ -195,7 +195,7 @@ export class RabbitMqService extends IMService { public async sendToManager(message: { id: string; [x: string]: any }, isResend: boolean = false) { if (!this.conn) { - console.log('Send message to RabbitMQ', message); + console.log('Send message to RabbitMQ', JSON.stringify(message, null, 2)); return; } @@ -499,7 +499,7 @@ export class RabbitMqService extends IMService { }; } private getMetrics() { - const req = (this.client as any).requestHandler; + const req = this.client.requestHandler; return { wsEvents: this.client.stats.wsEvents, @@ -507,7 +507,7 @@ export class RabbitMqService extends IMService { wsErrors: this.client.stats.wsErrors, cmdProcessed: this.client.stats.cmdProcessed, cmdErrors: this.client.stats.cmdErrors, - cmdHttpErrors: [...this.client.stats.cmdHttpErrors.entries()].map(([code, count]) => ({ code, count })), + httpRequests: [...req.requestStats.entries()].map(([url, stats]) => ({ url, stats })), httpRequestsQueued: Object.keys(req.ratelimits) .filter((endpoint) => req.ratelimits[endpoint]._queue.length > 0) .reduce((acc, endpoint) => acc.concat([{ endpoint, count: req.ratelimits[endpoint]._queue.length }]), [])