UNPKG

@ayanaware/bentocord

Version:

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

68 lines 3.55 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.SetAvatarCommand = void 0; const https = require("https"); const bento_1 = require("@ayanaware/bento"); const Discord_1 = require("../../discord/Discord"); const CommandManager_1 = require("../CommandManager"); const OptionType_1 = require("../constants/OptionType"); const SuppressorType_1 = require("../constants/SuppressorType"); class SetAvatarCommand { constructor() { this.name = '@ayanaware/bentocord:SetAvatarCommand'; this.parent = CommandManager_1.CommandManager; this.replaceable = true; this.definition = { name: ['setavatar', { key: 'BENTOCORD_COMMAND_SETAVATAR' }], description: { key: 'BENTOCORD_COMMAND_SETAVATAR_DESCRIPTION', backup: 'Set the bot\'s avatar' }, options: [ { type: OptionType_1.OptionType.STRING, name: 'url', description: { key: 'BENTOCORD_OPTION_URL', backup: 'The avatar to set' } }, ], hidden: true, registerSlash: false, permissionDefaults: { user: false, admin: false }, suppressors: [SuppressorType_1.SuppressorType.BOT_OWNER], }; } async execute(ctx, { url }) { // TODO: Check if the url is a valid image url await ctx.createTranslatedResponse('BENTOCORD_AVATAR_UPDATING', {}, 'Updating avatar...'); return new Promise((resolve, reject) => { https.get(url, res => { if (res.statusCode !== 200) return reject(new Error(`Invalid status code: ${res.statusCode}`)); // expected to be binary image data res.setEncoding('binary'); const data = []; res.on('data', chunk => { // eslint-disable-next-line @typescript-eslint/no-unsafe-argument data.push(Buffer.from(chunk, 'binary')); }); res.on('end', () => { const type = res.headers['content-type'] ?? 'image/png'; const buffer = Buffer.concat(data); const avatar = `data:${type};base64,${buffer.toString('base64')}`; this.discord.client.editSelf({ avatar }).then(() => { ctx.createTranslatedResponse('BENTOCORD_AVATAR_UPDATED', {}, 'Avatar updated!') .then(() => resolve).catch(reject); }).catch(reject); }); }).on('error', e => reject(e)); }); } } __decorate([ (0, bento_1.Inject)(), __metadata("design:type", Discord_1.Discord) ], SetAvatarCommand.prototype, "discord", void 0); exports.SetAvatarCommand = SetAvatarCommand; //# sourceMappingURL=SetAvatar.js.map