UNPKG

@ayanaware/bentocord

Version:

Bentocord is a Bento plugin designed to rapidly build fully functional Discord Bots.

112 lines 5.34 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Discord = void 0; const bento_1 = require("@ayanaware/bento"); const logger_api_1 = require("@ayanaware/logger-api"); const eris_1 = require("eris"); const BentocordInterface_1 = require("../BentocordInterface"); const BentocordVariable_1 = require("../BentocordVariable"); const DiscordEvent_1 = require("./constants/DiscordEvent"); const log = logger_api_1.Logger.get(null); class Discord { constructor() { this.name = '@ayanaware/bentocord:Discord'; } async onUnload() { return this.disconnect(); } async onVerify() { return this.connect(); } async connect(tokenOverride, optionsOverride) { const token = tokenOverride || this.api.getVariable({ name: BentocordVariable_1.BentocordVariable.BENTOCORD_TOKEN, default: null }); if (!token) { throw new Error(`Failed to find token: Variable "${BentocordVariable_1.BentocordVariable.BENTOCORD_TOKEN}" and param tokenOverride empty`); } if (this.client) await this.disconnect(); let clientOptions = this.api.getVariable({ name: BentocordVariable_1.BentocordVariable.BENTOCORD_CLIENT_OPTIONS, default: {} }); let { shardIds, shardCount } = await this.interface.getShardData(); clientOptions.maxShards = shardCount || 1; // resolve firstShardID & lastShardID if (!Array.isArray(shardIds) || shardIds.length === 0) shardIds = [0, 1]; shardIds.sort((a, b) => a - b); // just in case, for that special person if (shardIds.length < 2) clientOptions = { ...clientOptions, firstShardID: shardIds[0], lastShardID: shardIds[0] }; else clientOptions = { ...clientOptions, firstShardID: shardIds[0], lastShardID: shardIds[shardIds.length - 1] }; // merge options & overrides clientOptions = { ...clientOptions, ...optionsOverride }; clientOptions.autoreconnect = true; clientOptions.restMode = true; this.clientOptions = clientOptions; log.info(`ClientOptions = ${JSON.stringify(clientOptions)}`); this.client = new eris_1.Client(`Bot ${token}`, this.clientOptions); this.api.forwardEvents(this.client, Object.values(DiscordEvent_1.DiscordEvent)); // refresh application object // https://discord.com/developers/docs/resources/application#application-object this.application = await this.client.getOAuthApplication(); log.info(`Discord Application: ${this.application.name} (${this.application.id}).`); await this.client.connect(); } // eslint-disable-next-line @typescript-eslint/require-await async disconnect() { this.client.disconnect({ reconnect: false }); this.client.removeAllListeners(); } handleShardError(e, id) { log.error(`Shard "${id}": ${e}`); } handleShardReady(id) { log.info(`Shard "${id}" ready`); } handleShardResume(id) { log.info(`Shard "${id}" resume`); } handleShardDisconnect(e, id) { let message = `Shard "${id}" disconnect`; if (e) message = `${message}, ${e}`; log.info(message); } } __decorate([ (0, bento_1.Inject)(), __metadata("design:type", BentocordInterface_1.BentocordInterface) ], Discord.prototype, "interface", void 0); __decorate([ (0, bento_1.Subscribe)(Discord, DiscordEvent_1.DiscordEvent.ERROR), __metadata("design:type", Function), __metadata("design:paramtypes", [Error, Number]), __metadata("design:returntype", void 0) ], Discord.prototype, "handleShardError", null); __decorate([ (0, bento_1.Subscribe)(Discord, DiscordEvent_1.DiscordEvent.SHARD_READY), __metadata("design:type", Function), __metadata("design:paramtypes", [Number]), __metadata("design:returntype", void 0) ], Discord.prototype, "handleShardReady", null); __decorate([ (0, bento_1.Subscribe)(Discord, DiscordEvent_1.DiscordEvent.SHARD_RESUME), __metadata("design:type", Function), __metadata("design:paramtypes", [Number]), __metadata("design:returntype", void 0) ], Discord.prototype, "handleShardResume", null); __decorate([ (0, bento_1.Subscribe)(Discord, DiscordEvent_1.DiscordEvent.SHARD_DISCONNECT), __metadata("design:type", Function), __metadata("design:paramtypes", [Error, Number]), __metadata("design:returntype", void 0) ], Discord.prototype, "handleShardDisconnect", null); exports.Discord = Discord; //# sourceMappingURL=Discord.js.map