UNPKG

@ayanaware/bentocord

Version:

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

164 lines 9.11 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.SetGameCommand = void 0; const bento_1 = require("@ayanaware/bento"); const logger_api_1 = require("@ayanaware/logger-api"); const BentocordVariable_1 = require("../../BentocordVariable"); const Discord_1 = require("../../discord/Discord"); const DiscordEvent_1 = require("../../discord/constants/DiscordEvent"); const CommandManager_1 = require("../CommandManager"); const OptionType_1 = require("../constants/OptionType"); const SuppressorType_1 = require("../constants/SuppressorType"); const log = logger_api_1.Logger.get(); class SetGameCommand { constructor() { this.name = '@ayanaware/bentocord:SetGameCommand'; this.parent = CommandManager_1.CommandManager; this.replaceable = true; this.definition = { name: ['setgame', { key: 'BENTOCORD_COMMAND_SETGAME' }], description: { key: 'BENTOCORD_COMMAND_SETGAME_DESCRIPTION', backup: 'Set the bot\'s game status' }, options: [ { type: OptionType_1.OptionType.SUB_COMMAND, name: ['playing'], description: { key: 'BENTOCORD_COMMAND_SETGAME_PLAYING_DESCRIPTION', backup: 'Set the bot status to playing' }, options: [ { type: OptionType_1.OptionType.STRING, name: ['activity'], description: { key: 'BENTOCORD_OPTION_ACTIVITY_DESCRIPTION', backup: 'The activity to set' }, rest: true }, ] }, { type: OptionType_1.OptionType.SUB_COMMAND, name: ['streaming'], description: { key: 'BENTOCORD_COMMAND_SETGAME_STREAMING_DESCRIPTION', backup: 'Set the bot status to streaming' }, options: [ { type: OptionType_1.OptionType.STRING, name: ['url'], description: { key: 'BENTOCORD_OPTION_URL_DESCRIPTION', backup: 'The activity url' } }, { type: OptionType_1.OptionType.STRING, name: ['activity'], description: { key: 'BENTOCORD_OPTION_ACTIVITY_DESCRIPTION', backup: 'The activity to set' }, rest: true }, ] }, { type: OptionType_1.OptionType.SUB_COMMAND, name: ['listening'], description: { key: 'BENTOCORD_COMMAND_SETGAME_LISTENING_DESCRIPTION', backup: 'Set the bot status to listening' }, options: [ { type: OptionType_1.OptionType.STRING, name: ['activity'], description: { key: 'BENTOCORD_OPTION_ACTIVITY_DESCRIPTION', backup: 'The activity to set' }, rest: true }, ] }, { type: OptionType_1.OptionType.SUB_COMMAND, name: ['watching'], description: { key: 'BENTOCORD_COMMAND_SETGAME_WATCHING_DESCRIPTION', backup: 'Set the bot status to watching' }, options: [ { type: OptionType_1.OptionType.STRING, name: ['activity'], description: { key: 'BENTOCORD_OPTION_ACTIVITY_DESCRIPTION', backup: 'The activity to set' }, rest: true }, ] }, { type: OptionType_1.OptionType.SUB_COMMAND, name: ['competing'], description: { key: 'BENTOCORD_COMMAND_SETGAME_COMPETING_DESCRIPTION', backup: 'Set the botstatus to competing' }, options: [ { type: OptionType_1.OptionType.STRING, name: ['activity'], description: { key: 'BENTOCORD_OPTION_ACTIVITY_DESCRIPTION', backup: 'The activity to set' }, rest: true }, ] }, // support for custom status { type: OptionType_1.OptionType.SUB_COMMAND, name: ['custom'], description: { key: 'BENTOCORD_COMMAND_SETGAME_CUSTOM_DESCRIPTION', backup: 'Set the bot status to a custom type' }, options: [ { type: OptionType_1.OptionType.INTEGER, name: ['type'], description: { key: 'BENTOCORD_OPTION_TYPE_DESCRIPTION', backup: 'The type of custom status' } }, { type: OptionType_1.OptionType.STRING, name: ['activity'], description: { key: 'BENTOCORD_OPTION_ACTIVITY_DESCRIPTION', backup: 'The activity to set' }, rest: true }, ] }, // reset { type: OptionType_1.OptionType.SUB_COMMAND, name: ['reset'], description: { key: 'BENTOCORD_COMMAND_SETGAME_RESET_DESCRIPTION', backup: 'Reset the bot\'s game status' } }, ], hidden: true, registerSlash: false, permissionDefaults: { user: false, admin: false }, suppressors: [SuppressorType_1.SuppressorType.BOT_OWNER], }; } async execute(ctx, options) { if (options.reset) { await this.resetActivity(); return ctx.createTranslatedResponse('BENTOCORD_PRESENCE_RESET', {}, 'Presence Reset!'); } const activity = { name: null }; if (options.playing) { activity.type = 0; activity.name = options.playing.activity; } else if (options.streaming) { activity.type = 1; activity.name = options.streaming.activity; activity.url = options.streaming.url; } else if (options.listening) { activity.type = 2; activity.name = options.listening.activity; } else if (options.watching) { activity.type = 3; activity.name = options.watching.activity; } else if (options.competing) { activity.type = 5; activity.name = options.competing.activity; } else if (options.custom) { activity.type = options.custom.type; // trick eris go brr activity.name = options.custom.activity; } await this.setActivity(activity); return ctx.createTranslatedResponse('BENTOCORD_PRESENCE_UPDATED', {}, 'Presence Updated!'); } /** * Store the activity for the bot, and apply it to all ready shards. * @param activity The activity to set the bot to. */ async setActivity(activity) { this.activity = activity; for (const [, shard] of this.discord.client.shards) await this.restoreActivity(shard.id); } /** * Fetch activity for the bot * @returns The activity for the bot. */ async getActivity() { const type = Number(this.defaultType); if (!this.activity) return { type, name: this.defaultName }; return this.activity; } async resetActivity() { this.activity = null; for (const [, shard] of this.discord.client.shards) await this.restoreActivity(shard.id); } /** * Transform the activity before setting, useful for replacements * @param activity The activity to transform * @param shard The shard to transform the activity for * @returns The transformed activity */ async formatActivity(activity, shard) { return activity; } async restoreActivity(id) { // attempt to get shard const shard = this.discord.client.shards.get(id); if (!shard || !shard.ready) return; // get activity let activity = await this.getActivity(); if (!activity) return; // format activity & set activity = await this.formatActivity(activity, shard); shard.editStatus(activity); // log log.info(`Set activity: ${JSON.stringify(activity)}`, `Shard ${id}`); } } __decorate([ (0, bento_1.Inject)(), __metadata("design:type", Discord_1.Discord) ], SetGameCommand.prototype, "discord", void 0); __decorate([ (0, bento_1.Variable)({ name: BentocordVariable_1.BentocordVariable.BENTOCORD_ACTIVITY_TYPE, default: '0' }), __metadata("design:type", String) ], SetGameCommand.prototype, "defaultType", void 0); __decorate([ (0, bento_1.Variable)({ name: BentocordVariable_1.BentocordVariable.BENTOCORD_ACTIVITY_NAME, default: 'with Bentocord' }), __metadata("design:type", String) ], SetGameCommand.prototype, "defaultName", void 0); __decorate([ (0, bento_1.Subscribe)(Discord_1.Discord, DiscordEvent_1.DiscordEvent.SHARD_READY), (0, bento_1.Subscribe)(Discord_1.Discord, DiscordEvent_1.DiscordEvent.SHARD_RESUME), __metadata("design:type", Function), __metadata("design:paramtypes", [Number]), __metadata("design:returntype", Promise) ], SetGameCommand.prototype, "restoreActivity", null); exports.SetGameCommand = SetGameCommand; //# sourceMappingURL=SetGame.js.map