@sodacore/discord
Version:
Sodacore Discord is a plugin that offers Discord SSO/OAuth2 support and the ability to create bots in a similar controller pattern.
109 lines (108 loc) • 4.08 kB
JavaScript
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);
};
import { BaseService, Service } from '@sodacore/core';
import { Inject } from '@sodacore/di';
import { Client } from 'discord.js';
import Router from './router';
/**
* The Discord service, will initialise a discord client
* that can then route interactions and messages to the
* appropriate handlers.
* @class DiscordService
* @extends {BaseService}
* @default
*/
let DiscordService = class DiscordService extends BaseService {
constructor() {
super(...arguments);
this.router = new Router(this.logger);
this.discordConfig = {
intents: [],
};
}
async init() {
if (!this.config.token)
return;
await this.router.init();
// Let's setup the discord client.
this.client = new Client(Object.assign(this.discordConfig, this.config.clientOptions));
this.client.on('ready', () => {
this.logger.info('[DISCORD]: Successfully connected and ready.');
});
// Handle the interaction create.
this.client.on('interactionCreate', this.handleInteraction.bind(this));
// Loop the other events.
const events = this.config.events || [];
events.forEach(event => {
this.client.on(event, (...args) => {
this.handleEvent(event, ...args);
});
});
}
async start() {
if (!this.config.token)
return;
this.logger.info('[DISCORD]: Connecting to Discord...');
this.client.login(this.config.token);
}
async stop() {
if (!this.config.token)
return;
if (!this.client)
return;
this.logger.info('[DISCORD]: Disconnected from Discord.');
this.client.destroy();
}
handleInteraction(interaction) {
// On command interaction.
if (interaction.isChatInputCommand()) {
// Check if we have a subcommand.
const subCommand = interaction.options.getSubcommand(false);
// If no subcommand, run the default.
if (!subCommand) {
return this.router.onCommand(interaction);
}
else {
return this.router.onSubCommand(interaction);
}
}
// On button interaction.
if (interaction.isButton()) {
return this.router.onButton(interaction);
}
// On select menu interaction.
if (interaction.isStringSelectMenu()) {
return this.router.onSelectMenu(interaction);
}
// On autocomplete interaction.
if (interaction.isAutocomplete()) {
return this.router.onAutocomplete(interaction);
}
// On context menu interaction.
if (interaction.isContextMenuCommand()) {
return this.router.onContextMenu(interaction);
}
// On modal interaction.
if (interaction.isModalSubmit()) {
return this.router.onModalSubmit(interaction);
}
}
handleEvent(event, ...args) {
return this.router.onEvent(event, ...args);
}
};
__decorate([
Inject('@discord:config'),
__metadata("design:type", Object)
], DiscordService.prototype, "config", void 0);
DiscordService = __decorate([
Service()
], DiscordService);
export default DiscordService;