UNPKG

psyduck-plugin-joingate

Version:
92 lines (91 loc) 3.91 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const command_line_args_1 = __importDefault(require("command-line-args")); const optionsDefinition = [ { name: 'help', alias: 'h', type: Boolean } ]; const userFormatRegex = new RegExp(/^(<@.+>)|(<!.+>)$/gm); class PsyduckExample { constructor(options) { this.gateCommand = options.gateCommand; this.gateChannelId = options.gateChannelId; this.roleId = options.roleId; } /** * This event is emitted from Psyduck when a message is recieved in a discord channel. From there you may do as you with with it in your plugin. */ onMessage(message) { // If the message came from a bot, ignore. if (message.author.bot) { return; } // If the message is too small to be a command, ignore. if (message.content.length <= 1) { return; } // If the message is a string of repeated characters, ignore. For example !!!!!!! const dupe = RegExp(/^(.)\1*$/gm); if (dupe.test(message.content)) { return; } if (message.channel.id !== this.gateChannelId) { return; } // Extract the command code, if it is in the list of commands we want to handle, continue. if (message.content.startsWith('!')) { const commandCode = message.content.slice(1).trim(); if (this.gateCommand === commandCode) { this.updateUser(message); } } } updateUser(message) { return __awaiter(this, void 0, void 0, function* () { try { const serverRole = message.guild.roles.find('id', this.roleId); if (serverRole) { const guildUser = yield message.guild.fetchMember(message.author.id); if (guildUser && !guildUser.roles.get(serverRole.id)) { guildUser.addRole(serverRole); } } } catch (error) { } }); } /** * If you command will use the command-line-args package, you can use this RegEx to extract the parts in to an array for use with hydrateOptions. */ parseParameters(messageContent) { return messageContent.match(/ (?=\S)[^'\s]*(?:'[^\\']*(?:\\[\s\S][^\\']*)*'[^'\s]*)*/g) || []; } /** * Hydrate an object with the shape if your options definition. */ hydrateOptions(parameters) { return command_line_args_1.default(optionsDefinition, { argv: parameters.map(p => p.trim()), partial: true }); } /** * If you need to extract a userId from a mention for later use, you can pass the raw mention to this method. <@1234567890> => 1234567890 | undefined */ extractUserId(userId) { const matches = userId.match(/(?<=@|!)(.*)(?=>)/gm); if (matches && matches.length === 1) { return matches[0]; } return undefined; } } exports.default = PsyduckExample;