forked from PluralFlux/PluralFlux
Converting ES6 back to CJS (#25)
converting back to cjs Co-authored-by: Aster Fialla <asterfialla@gmail.com>
This commit is contained in:
@@ -7,6 +7,7 @@
|
||||
"type": "git",
|
||||
"url": "https://github.com/pieartsy/PluralFlux.git"
|
||||
},
|
||||
"type": "commonjs",
|
||||
"private": true,
|
||||
"dependencies": {
|
||||
"@fluxerjs/core": "^1.2.2",
|
||||
|
||||
26
src/bot.js
26
src/bot.js
@@ -1,12 +1,12 @@
|
||||
import { Client, Events, Message } from '@fluxerjs/core';
|
||||
import { messageHelper } from "./helpers/messageHelper.js";
|
||||
import {enums} from "./enums.js";
|
||||
import {commands} from "./commands.js";
|
||||
import {webhookHelper} from "./helpers/webhookHelper.js";
|
||||
import env from 'dotenv';
|
||||
import {utils} from "./helpers/utils.js";
|
||||
const {Client, Events, Message} = require('@fluxerjs/core');
|
||||
const messageHelper = require("./helpers/messageHelper.js");
|
||||
const enums = require("./enums.js");
|
||||
const commands = require("./commands.js");
|
||||
const webhookHelper = require("./helpers/webhookHelper.js");
|
||||
const env = require('dotenv');
|
||||
const utils = require("./helpers/utils.js");
|
||||
|
||||
env.config({path: './.env'});
|
||||
env.config();
|
||||
|
||||
const token = process.env.FLUXER_BOT_TOKEN;
|
||||
|
||||
@@ -15,7 +15,7 @@ if (!token) {
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
export const client = new Client({ intents: 0 });
|
||||
const client = new Client({ intents: 0 });
|
||||
|
||||
client.on(Events.MessageCreate, async (message) => {
|
||||
await handleMessageCreate(message);
|
||||
@@ -28,7 +28,7 @@ client.on(Events.MessageCreate, async (message) => {
|
||||
* @param {Message} message - The message object
|
||||
*
|
||||
**/
|
||||
export const handleMessageCreate = async function(message) {
|
||||
exports.handleMessageCreate = async function(message) {
|
||||
try {
|
||||
// Ignore bots
|
||||
if (message.author.bot) return;
|
||||
@@ -79,9 +79,9 @@ function printGuilds() {
|
||||
}
|
||||
|
||||
const debouncePrintGuilds = utils.debounce(printGuilds, 2000);
|
||||
export const debounceLogin = utils.debounce(client.login, 60000);
|
||||
// export const debounceLogin = utils.debounce(client.login, 60000);
|
||||
|
||||
export const login = async function() {
|
||||
exports.login = async function() {
|
||||
try {
|
||||
await client.login(token);
|
||||
// await db.check_connection();
|
||||
@@ -93,7 +93,7 @@ export const login = async function() {
|
||||
|
||||
function main()
|
||||
{
|
||||
login();
|
||||
exports.login();
|
||||
}
|
||||
|
||||
main();
|
||||
@@ -1,20 +1,20 @@
|
||||
import {messageHelper} from "./helpers/messageHelper.js";
|
||||
import {enums} from "./enums.js";
|
||||
import {memberHelper} from "./helpers/memberHelper.js";
|
||||
import {EmbedBuilder} from "@fluxerjs/core";
|
||||
import {importHelper} from "./helpers/importHelper.js";
|
||||
const messageHelper = require("./helpers/messageHelper.js");
|
||||
const enums = require("./enums.js");
|
||||
const memberHelper = require("./helpers/memberHelper.js");
|
||||
const {EmbedBuilder} = require("@fluxerjs/core");
|
||||
const importHelper = require("./helpers/importHelper.js");
|
||||
|
||||
const cmds = {
|
||||
const commands = {
|
||||
commandsMap: new Map(),
|
||||
aliasesMap: new Map()
|
||||
};
|
||||
|
||||
cmds.aliasesMap.set('m', {command: 'member'})
|
||||
commands.aliasesMap.set('m', {command: 'member'})
|
||||
|
||||
cmds.commandsMap.set('member', {
|
||||
commands.commandsMap.set('member', {
|
||||
description: enums.help.SHORT_DESC_MEMBER,
|
||||
async execute(message, args) {
|
||||
await cmds.memberCommand(message, args)
|
||||
await commands.memberCommand(message, args)
|
||||
}
|
||||
})
|
||||
|
||||
@@ -26,7 +26,7 @@ cmds.commandsMap.set('member', {
|
||||
* @param {string[]} args - The parsed arguments
|
||||
*
|
||||
**/
|
||||
cmds.memberCommand = async function (message, args) {
|
||||
commands.memberCommand = async function (message, args) {
|
||||
const authorFull = `${message.author.username}#${message.author.discriminator}`
|
||||
const attachmentUrl = message.attachments.size > 0 ? message.attachments.first().url : null;
|
||||
const attachmentExpires = message.attachments.size > 0 ? message.attachments.first().expires_at : null;
|
||||
@@ -53,10 +53,10 @@ cmds.memberCommand = async function (message, args) {
|
||||
}
|
||||
|
||||
|
||||
cmds.commandsMap.set('help', {
|
||||
commands.commandsMap.set('help', {
|
||||
description: enums.help.SHORT_DESC_HELP,
|
||||
async execute(message) {
|
||||
const fields = [...cmds.commandsMap.entries()].map(([name, cmd]) => ({
|
||||
const fields = [...commands.commandsMap.entries()].map(([name, cmd]) => ({
|
||||
name: `${messageHelper.prefix}${name}`,
|
||||
value: cmd.description,
|
||||
inline: true,
|
||||
@@ -73,10 +73,10 @@ cmds.commandsMap.set('help', {
|
||||
},
|
||||
})
|
||||
|
||||
cmds.commandsMap.set('import', {
|
||||
commands.commandsMap.set('import', {
|
||||
description: enums.help.SHORT_DESC_IMPORT,
|
||||
async execute(message, args) {
|
||||
await cmds.importCommand(message, args);
|
||||
await commands.importCommand(message, args);
|
||||
}
|
||||
})
|
||||
|
||||
@@ -88,7 +88,7 @@ cmds.commandsMap.set('import', {
|
||||
* @param {string[]} args - The parsed arguments
|
||||
*
|
||||
**/
|
||||
cmds.importCommand = async function (message, args) {
|
||||
commands.importCommand = async function (message, args) {
|
||||
const attachmentUrl = message.attachments.size > 0 ? message.attachments.first().url : null;
|
||||
if ((message.content.includes('--help') || (args[0] === '' && args.length === 1)) && !attachmentUrl) {
|
||||
return await message.reply(enums.help.IMPORT);
|
||||
@@ -119,4 +119,4 @@ cmds.importCommand = async function (message, args) {
|
||||
|
||||
}
|
||||
|
||||
export const commands = cmds;
|
||||
module.exports = commands;
|
||||
@@ -1,5 +1,5 @@
|
||||
import {DataTypes, Sequelize} from 'sequelize';
|
||||
import * as env from 'dotenv';
|
||||
const env = require('dotenv')
|
||||
const {Sequelize, DataTypes} = require('sequelize');
|
||||
|
||||
env.config();
|
||||
|
||||
@@ -10,7 +10,7 @@ if (!password) {
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const db = {};
|
||||
const database = {};
|
||||
|
||||
const sequelize = new Sequelize('postgres', 'postgres', password, {
|
||||
host: 'localhost',
|
||||
@@ -18,10 +18,10 @@ const sequelize = new Sequelize('postgres', 'postgres', password, {
|
||||
dialect: 'postgres'
|
||||
});
|
||||
|
||||
db.sequelize = sequelize;
|
||||
db.Sequelize = Sequelize;
|
||||
database.sequelize = sequelize;
|
||||
database.Sequelize = Sequelize;
|
||||
|
||||
db.members = sequelize.define('Member', {
|
||||
database.members = sequelize.define('Member', {
|
||||
userid: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: false,
|
||||
@@ -41,7 +41,7 @@ db.members = sequelize.define('Member', {
|
||||
}
|
||||
});
|
||||
|
||||
db.systems = sequelize.define('System', {
|
||||
database.systems = sequelize.define('System', {
|
||||
userid: {
|
||||
type: DataTypes.STRING,
|
||||
},
|
||||
@@ -59,7 +59,7 @@ db.systems = sequelize.define('System', {
|
||||
/**
|
||||
* Checks Sequelize database connection.
|
||||
*/
|
||||
db.check_connection = async function () {
|
||||
database.check_connection = async function () {
|
||||
try {
|
||||
await sequelize.authenticate();
|
||||
console.log('Connection has been established successfully.');
|
||||
@@ -83,4 +83,4 @@ async function syncModels() {
|
||||
}
|
||||
}
|
||||
|
||||
export const database = db;
|
||||
module.exports = database;
|
||||
10
src/enums.js
10
src/enums.js
@@ -1,6 +1,6 @@
|
||||
const helperEnums = {};
|
||||
const enums = {};
|
||||
|
||||
helperEnums.err = {
|
||||
enums.err = {
|
||||
NO_MEMBER: "No such member was found.",
|
||||
NO_NAME_PROVIDED: "No member name was provided for",
|
||||
NO_VALUE: "has not been set for this member.",
|
||||
@@ -26,7 +26,7 @@ helperEnums.err = {
|
||||
CANNOT_FETCH_RESOURCE: "Could not download the file at this time."
|
||||
}
|
||||
|
||||
helperEnums.help = {
|
||||
enums.help = {
|
||||
SHORT_DESC_HELP: "Lists available commands.",
|
||||
SHORT_DESC_MEMBER: "Accesses subcommands related to proxy members.",
|
||||
SHORT_DESC_IMPORT: "Imports from PluralKit.",
|
||||
@@ -43,11 +43,11 @@ helperEnums.help = {
|
||||
IMPORT: "Imports from PluralKit using the JSON file provided by their export command. Importing from other proxy bots is TBD. `pf;import` and attach your JSON file to the message. This will only save the fields that are present in the bot currently, not anything else like birthdays or system handles (yet?). **Only one proxy can be set per member currently.**\n\n**PRO TIP**: For privacy reasons, try DMing the bot with this command and your JSON file--it should still work the same."
|
||||
}
|
||||
|
||||
helperEnums.misc = {
|
||||
enums.misc = {
|
||||
ATTACHMENT_SENT_BY: "Attachment sent by:",
|
||||
ATTACHMENT_EXPIRATION_WARNING: "**NOTE:** Because this profile picture is hosted on Fluxer, it will expire. To avoid this, upload the picture to another website like <https://imgbb.com/> and link to it directly.",
|
||||
FLUXER_ATTACHMENT_URL: "https://fluxerusercontent.com/attachments/"
|
||||
|
||||
}
|
||||
|
||||
export const enums = helperEnums;
|
||||
module.exports = enums;
|
||||
@@ -1,7 +1,7 @@
|
||||
import {enums} from "../enums.js";
|
||||
import {memberHelper} from "./memberHelper.js";
|
||||
const enums = require("../enums.js");
|
||||
const memberHelper = require("./memberHelper.js");
|
||||
|
||||
const ih = {};
|
||||
const importHelper = {};
|
||||
|
||||
/**
|
||||
* Tries to import from Pluralkit.
|
||||
@@ -12,7 +12,7 @@ const ih = {};
|
||||
* @returns {Promise<string>} A successful addition of all members.
|
||||
* @throws {Error} When the member exists, or creating a member doesn't work.
|
||||
*/
|
||||
ih.pluralKitImport = async function (authorId, attachmentUrl= null) {
|
||||
importHelper.pluralKitImport = async function (authorId, attachmentUrl= null) {
|
||||
let fetchResult, pkData;
|
||||
if (!attachmentUrl) {
|
||||
throw new Error(enums.err.NOT_JSON_FILE);
|
||||
@@ -55,4 +55,4 @@ ih.pluralKitImport = async function (authorId, attachmentUrl= null) {
|
||||
return aggregatedText;
|
||||
}
|
||||
|
||||
export const importHelper = ih;
|
||||
module.exports = importHelper;
|
||||
@@ -1,10 +1,10 @@
|
||||
import {database} from '../database.js';
|
||||
import {enums} from "../enums.js";
|
||||
import {Op} from "sequelize";
|
||||
import {EmbedBuilder} from "@fluxerjs/core";
|
||||
import {utils} from "./utils.js";
|
||||
const database = require('../database.js');
|
||||
const enums = require("../enums.js");
|
||||
const {Op} = require("sequelize");
|
||||
const {EmbedBuilder} = require("@fluxerjs/core");
|
||||
const utils = require("./utils.js");
|
||||
|
||||
const mh = {};
|
||||
const memberHelper = {};
|
||||
|
||||
const commandList = ['new', 'remove', 'name', 'list', 'displayname', 'proxy', 'propic'];
|
||||
const newAndRemoveCommands = ['new', 'remove'];
|
||||
@@ -23,14 +23,14 @@ const newAndRemoveCommands = ['new', 'remove'];
|
||||
* @returns {Promise <EmbedBuilder>} A list of member commands and descriptions.
|
||||
* @returns {Promise<{EmbedBuilder, string[], string}>} A member info embed + info/errors.
|
||||
*/
|
||||
mh.parseMemberCommand = async function (authorId, authorFull, args, attachmentUrl = null, attachmentExpiration = null) {
|
||||
memberHelper.parseMemberCommand = async function (authorId, authorFull, args, attachmentUrl = null, attachmentExpiration = null) {
|
||||
let memberName, command, isHelp = false;
|
||||
// checks whether command is in list, otherwise assumes it's a name
|
||||
|
||||
// ex: pf;member remove, pf;member remove --help
|
||||
// ex: pf;member, pf;member --help
|
||||
if (args.length === 0 || args[0] === '--help' || args[0] === '') {
|
||||
return mh.getMemberCommandInfo();
|
||||
return memberHelper.getMemberCommandInfo();
|
||||
}
|
||||
// ex: pf;member remove somePerson
|
||||
if (commandList.includes(args[0])) {
|
||||
@@ -52,7 +52,7 @@ mh.parseMemberCommand = async function (authorId, authorFull, args, attachmentUr
|
||||
isHelp = true;
|
||||
}
|
||||
|
||||
return await mh.memberArgumentHandler(authorId, authorFull, isHelp, command, memberName, args, attachmentUrl, attachmentExpiration)
|
||||
return await memberHelper.memberArgumentHandler(authorId, authorFull, isHelp, command, memberName, args, attachmentUrl, attachmentExpiration)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -73,15 +73,15 @@ mh.parseMemberCommand = async function (authorId, authorFull, args, attachmentUr
|
||||
* @returns {Promise<{EmbedBuilder, [string], string}>} A member info embed + info/errors.
|
||||
* @throws {Error} When there's no member or a command is not recognized.
|
||||
*/
|
||||
mh.memberArgumentHandler = async function(authorId, authorFull, isHelp, command = null, memberName = null, args = [], attachmentUrl = null, attachmentExpiration = null) {
|
||||
memberHelper.memberArgumentHandler = async function(authorId, authorFull, isHelp, command = null, memberName = null, args = [], attachmentUrl = null, attachmentExpiration = null) {
|
||||
if (!command && !memberName && !isHelp) {
|
||||
throw new Error(enums.err.COMMAND_NOT_RECOGNIZED);
|
||||
}
|
||||
else if (isHelp) {
|
||||
return mh.sendHelpEnum(command);
|
||||
return memberHelper.sendHelpEnum(command);
|
||||
}
|
||||
else if (command === "list") {
|
||||
return await mh.getAllMembersInfo(authorId, authorFull);
|
||||
return await memberHelper.getAllMembersInfo(authorId, authorFull);
|
||||
}
|
||||
else if (!memberName && !isHelp) {
|
||||
throw new Error(enums.err.NO_MEMBER);
|
||||
@@ -92,10 +92,10 @@ mh.memberArgumentHandler = async function(authorId, authorFull, isHelp, command
|
||||
|
||||
// ex: pf;member blah blah
|
||||
if (command && memberName && (values.length > 0 || newAndRemoveCommands.includes(command) || attachmentUrl)) {
|
||||
return await mh.memberCommandHandler(authorId, command, memberName, values, attachmentUrl, attachmentExpiration);
|
||||
return await memberHelper.memberCommandHandler(authorId, command, memberName, values, attachmentUrl, attachmentExpiration);
|
||||
}
|
||||
else if (memberName && values.length === 0) {
|
||||
return await mh.sendCurrentValue(authorId, memberName, command);
|
||||
return await memberHelper.sendCurrentValue(authorId, memberName, command);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -112,12 +112,12 @@ mh.memberArgumentHandler = async function(authorId, authorFull, isHelp, command
|
||||
* @returns {Promise<{EmbedBuilder, string[], string}>} A member info embed + info/errors.
|
||||
* @throws {Error} When there's no member
|
||||
*/
|
||||
mh.sendCurrentValue = async function(authorId, memberName, command= null) {
|
||||
const member = await mh.getMemberByName(authorId, memberName);
|
||||
memberHelper.sendCurrentValue = async function(authorId, memberName, command= null) {
|
||||
const member = await memberHelper.getMemberByName(authorId, memberName);
|
||||
if (!member) throw new Error(enums.err.NO_MEMBER);
|
||||
|
||||
if (!command) {
|
||||
return mh.getMemberInfo(member);
|
||||
return memberHelper.getMemberInfo(member);
|
||||
}
|
||||
|
||||
switch (command) {
|
||||
@@ -138,7 +138,7 @@ mh.sendCurrentValue = async function(authorId, memberName, command= null) {
|
||||
* @param {string} command - The command being called.
|
||||
* @returns {string} - The help text associated with a command.
|
||||
*/
|
||||
mh.sendHelpEnum = function(command) {
|
||||
memberHelper.sendHelpEnum = function(command) {
|
||||
switch (command) {
|
||||
case 'new':
|
||||
return enums.help.NEW;
|
||||
@@ -172,20 +172,20 @@ mh.sendHelpEnum = function(command) {
|
||||
* @returns {Promise <EmbedBuilder>} A list of member commands and descriptions.
|
||||
* @returns {Promise<{EmbedBuilder, [string], string}>} A member info embed + info/errors.
|
||||
*/
|
||||
mh.memberCommandHandler = async function(authorId, command, memberName, values, attachmentUrl = null, attachmentExpiration = null) {
|
||||
memberHelper.memberCommandHandler = async function(authorId, command, memberName, values, attachmentUrl = null, attachmentExpiration = null) {
|
||||
switch (command) {
|
||||
case 'new':
|
||||
return await mh.addNewMember(authorId, memberName, values, attachmentUrl, attachmentExpiration);
|
||||
return await memberHelper.addNewMember(authorId, memberName, values, attachmentUrl, attachmentExpiration);
|
||||
case 'remove':
|
||||
return await mh.removeMember(authorId, memberName);
|
||||
return await memberHelper.removeMember(authorId, memberName);
|
||||
case 'name':
|
||||
return await mh.updateName(authorId, memberName, values[0]);
|
||||
return await memberHelper.updateName(authorId, memberName, values[0]);
|
||||
case 'displayname':
|
||||
return await mh.updateDisplayName(authorId, memberName, values[0]);
|
||||
return await memberHelper.updateDisplayName(authorId, memberName, values[0]);
|
||||
case 'proxy':
|
||||
return await mh.updateProxy(authorId, memberName, values[0]);
|
||||
return await memberHelper.updateProxy(authorId, memberName, values[0]);
|
||||
case 'propic':
|
||||
return await mh.updatePropic(authorId, memberName, values[0], attachmentUrl, attachmentExpiration);
|
||||
return await memberHelper.updatePropic(authorId, memberName, values[0], attachmentUrl, attachmentExpiration);
|
||||
default:
|
||||
throw new Error(enums.err.COMMAND_NOT_RECOGNIZED);
|
||||
}
|
||||
@@ -202,13 +202,13 @@ mh.memberCommandHandler = async function(authorId, command, memberName, values,
|
||||
* @param {string | null} [attachmentExpiration] - The attachment expiry date, if any
|
||||
* @returns {Promise<{EmbedBuilder, string[], string}>} A successful addition.
|
||||
*/
|
||||
mh.addNewMember = async function (authorId, memberName, values, attachmentUrl = null, attachmentExpiration = null) {
|
||||
memberHelper.addNewMember = async function (authorId, memberName, values, attachmentUrl = null, attachmentExpiration = null) {
|
||||
const displayName = values[0];
|
||||
const proxy = values[1];
|
||||
const propic = values[2] ?? attachmentUrl;
|
||||
|
||||
const memberObj = await mh.addFullMember(authorId, memberName, displayName, proxy, propic, attachmentExpiration);
|
||||
const memberInfoEmbed = mh.getMemberInfo(memberObj.member);
|
||||
const memberObj = await memberHelper.addFullMember(authorId, memberName, displayName, proxy, propic, attachmentExpiration);
|
||||
const memberInfoEmbed = memberHelper.getMemberInfo(memberObj.member);
|
||||
return {embed: memberInfoEmbed, errors: memberObj.errors, success: `${memberName} has been added successfully.`}
|
||||
}
|
||||
|
||||
@@ -222,12 +222,12 @@ mh.addNewMember = async function (authorId, memberName, values, attachmentUrl =
|
||||
* @returns {Promise<string>} A successful update.
|
||||
* @throws {RangeError} When the name doesn't exist.
|
||||
*/
|
||||
mh.updateName = async function (authorId, memberName, name) {
|
||||
memberHelper.updateName = async function (authorId, memberName, name) {
|
||||
const trimmedName = name.trim();
|
||||
if (trimmedName === '') {
|
||||
throw new RangeError(`Name ${enums.err.NO_VALUE}`);
|
||||
}
|
||||
return await mh.updateMemberField(authorId, memberName, "name", trimmedName);
|
||||
return await memberHelper.updateMemberField(authorId, memberName, "name", trimmedName);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -240,7 +240,7 @@ mh.updateName = async function (authorId, memberName, name) {
|
||||
* @returns {Promise<string>} A successful update.
|
||||
* @throws {RangeError} When the display name is too long or doesn't exist.
|
||||
*/
|
||||
mh.updateDisplayName = async function (authorId, membername, displayname) {
|
||||
memberHelper.updateDisplayName = async function (authorId, membername, displayname) {
|
||||
const trimmedName = displayname.trim();
|
||||
|
||||
if (trimmedName.length > 32) {
|
||||
@@ -249,7 +249,7 @@ mh.updateDisplayName = async function (authorId, membername, displayname) {
|
||||
else if (trimmedName === '') {
|
||||
throw new RangeError(`Display name ${enums.err.NO_VALUE}`);
|
||||
}
|
||||
return await mh.updateMemberField(authorId, membername, "displayname", trimmedName);
|
||||
return await memberHelper.updateMemberField(authorId, membername, "displayname", trimmedName);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -261,11 +261,11 @@ mh.updateDisplayName = async function (authorId, membername, displayname) {
|
||||
* @param {string} proxy - The proxy to set
|
||||
* @returns {Promise<string> } A successful update.
|
||||
*/
|
||||
mh.updateProxy = async function (authorId, memberName, proxy) {
|
||||
memberHelper.updateProxy = async function (authorId, memberName, proxy) {
|
||||
// Throws error if exists
|
||||
await mh.checkIfProxyExists(authorId, proxy);
|
||||
await memberHelper.checkIfProxyExists(authorId, proxy);
|
||||
|
||||
return await mh.updateMemberField(authorId, memberName, "proxy", proxy);
|
||||
return await memberHelper.updateMemberField(authorId, memberName, "proxy", proxy);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -279,12 +279,12 @@ mh.updateProxy = async function (authorId, memberName, proxy) {
|
||||
* @param {string | null} attachmentExpiration - The attachment expiry date, if any
|
||||
* @returns {Promise<string>} A successful update.
|
||||
*/
|
||||
mh.updatePropic = async function (authorId, memberName, values, attachmentUrl = null, attachmentExpiration = null) {
|
||||
memberHelper.updatePropic = async function (authorId, memberName, values, attachmentUrl = null, attachmentExpiration = null) {
|
||||
const imgUrl = values ?? attachmentUrl;
|
||||
// Throws error if invalid
|
||||
await utils.checkImageFormatValidity(imgUrl);
|
||||
const expirationWarning = utils.setExpirationWarning(imgUrl, attachmentExpiration);
|
||||
return await mh.updateMemberField(authorId, memberName, "propic", imgUrl, expirationWarning);
|
||||
return await memberHelper.updateMemberField(authorId, memberName, "propic", imgUrl, expirationWarning);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -296,7 +296,7 @@ mh.updatePropic = async function (authorId, memberName, values, attachmentUrl =
|
||||
* @returns {Promise<string>} A successful removal.
|
||||
* @throws {Error} When there is no member to remove.
|
||||
*/
|
||||
mh.removeMember = async function (authorId, memberName) {
|
||||
memberHelper.removeMember = async function (authorId, memberName) {
|
||||
const destroyed = await database.members.destroy({
|
||||
where: {
|
||||
name: {[Op.iLike]: memberName},
|
||||
@@ -325,8 +325,8 @@ mh.removeMember = async function (authorId, memberName) {
|
||||
* @returns {Promise<{model, string[]}>} A successful addition object, including errors if there are any.
|
||||
* @throws {Error} When the member already exists, there are validation errors, or adding a member doesn't work.
|
||||
*/
|
||||
mh.addFullMember = async function (authorId, memberName, displayName = null, proxy = null, propic = null, attachmentExpiration = null) {
|
||||
const existingMember = await mh.getMemberByName(authorId, memberName);
|
||||
memberHelper.addFullMember = async function (authorId, memberName, displayName = null, proxy = null, propic = null, attachmentExpiration = null) {
|
||||
const existingMember = await memberHelper.getMemberByName(authorId, memberName);
|
||||
if (existingMember) {
|
||||
throw new Error(`Can't add ${memberName}. ${enums.err.MEMBER_EXISTS}`);
|
||||
}
|
||||
@@ -356,7 +356,7 @@ mh.addFullMember = async function (authorId, memberName, displayName = null, pro
|
||||
let isValidProxy;
|
||||
if (proxy && proxy.length > 0) {
|
||||
try {
|
||||
const proxyExists = await mh.checkIfProxyExists(authorId, proxy);
|
||||
const proxyExists = await memberHelper.checkIfProxyExists(authorId, proxy);
|
||||
isValidProxy = !proxyExists;
|
||||
}
|
||||
catch(e) {
|
||||
@@ -398,7 +398,7 @@ mh.addFullMember = async function (authorId, memberName, displayName = null, pro
|
||||
* @returns {Promise<string>} A successful update.
|
||||
* @throws {Error} When no member row was updated.
|
||||
*/
|
||||
mh.updateMemberField = async function (authorId, memberName, columnName, value, expirationWarning = null) {
|
||||
memberHelper.updateMemberField = async function (authorId, memberName, columnName, value, expirationWarning = null) {
|
||||
const res = await database.members.update({[columnName]: value}, {
|
||||
where: {
|
||||
name: {[Op.iLike]: memberName},
|
||||
@@ -418,7 +418,7 @@ mh.updateMemberField = async function (authorId, memberName, columnName, value,
|
||||
* @param {model} member - The member object
|
||||
* @returns {EmbedBuilder} The member's info.
|
||||
*/
|
||||
mh.getMemberInfo = function (member) {
|
||||
memberHelper.getMemberInfo = function (member) {
|
||||
return new EmbedBuilder()
|
||||
.setTitle(member.name)
|
||||
.setDescription(`Details for ${member.name}`)
|
||||
@@ -439,8 +439,8 @@ mh.getMemberInfo = function (member) {
|
||||
* @returns {Promise<EmbedBuilder>} The info for all members.
|
||||
* @throws {Error} When there are no members for an author.
|
||||
*/
|
||||
mh.getAllMembersInfo = async function (authorId, authorName) {
|
||||
const members = await mh.getMembersByAuthor(authorId);
|
||||
memberHelper.getAllMembersInfo = async function (authorId, authorName) {
|
||||
const members = await memberHelper.getMembersByAuthor(authorId);
|
||||
if (members.length === 0) throw Error(enums.err.USER_NO_MEMBERS);
|
||||
const fields = [...members.entries()].map(([index, member]) => ({
|
||||
name: member.name, value: `(Proxy: \`${member.proxy ?? "unset"}\`)`, inline: true,
|
||||
@@ -458,7 +458,7 @@ mh.getAllMembersInfo = async function (authorId, authorName) {
|
||||
* @param {string} memberName - The member's name.
|
||||
* @returns {Promise<model>} The member object.
|
||||
*/
|
||||
mh.getMemberByName = async function (authorId, memberName) {
|
||||
memberHelper.getMemberByName = async function (authorId, memberName) {
|
||||
return await database.members.findOne({where: {userid: authorId, name: {[Op.iLike]: memberName}}});
|
||||
}
|
||||
|
||||
@@ -469,7 +469,7 @@ mh.getMemberByName = async function (authorId, memberName) {
|
||||
* @param {string} authorId - The author of the message
|
||||
* @returns {Promise<model[] | null>} The member object array.
|
||||
*/
|
||||
mh.getMembersByAuthor = async function (authorId) {
|
||||
memberHelper.getMembersByAuthor = async function (authorId) {
|
||||
return await database.members.findAll({where: {userid: authorId}});
|
||||
}
|
||||
|
||||
@@ -481,12 +481,12 @@ mh.getMembersByAuthor = async function (authorId) {
|
||||
* @returns {Promise<boolean> } Whether the proxy exists.
|
||||
* @throws {Error} When an empty proxy was provided, or no proxy exists.
|
||||
*/
|
||||
mh.checkIfProxyExists = async function (authorId, proxy) {
|
||||
memberHelper.checkIfProxyExists = async function (authorId, proxy) {
|
||||
const splitProxy = proxy.trim().split("text");
|
||||
if (splitProxy.length < 2) throw new Error(enums.err.NO_TEXT_FOR_PROXY);
|
||||
if (!splitProxy[0] && !splitProxy[1]) throw new Error(enums.err.NO_PROXY_WRAPPER);
|
||||
|
||||
const memberList = await mh.getMembersByAuthor(authorId);
|
||||
const memberList = await memberHelper.getMembersByAuthor(authorId);
|
||||
const proxyExists = memberList.some(member => member.proxy === proxy);
|
||||
if (proxyExists) {
|
||||
throw new Error(enums.err.PROXY_EXISTS);
|
||||
@@ -499,7 +499,7 @@ mh.checkIfProxyExists = async function (authorId, proxy) {
|
||||
*
|
||||
* @returns {EmbedBuilder } An embed of member commands.
|
||||
*/
|
||||
mh.getMemberCommandInfo = function() {
|
||||
memberHelper.getMemberCommandInfo = function() {
|
||||
const fields = [
|
||||
{name: `**new**`, value: enums.help.NEW, inline: false},
|
||||
{name: `**remove**`, value: enums.help.REMOVE, inline: false},
|
||||
@@ -516,4 +516,4 @@ mh.getMemberCommandInfo = function() {
|
||||
}
|
||||
|
||||
|
||||
export const memberHelper = mh;
|
||||
module.exports = memberHelper;
|
||||
@@ -1,8 +1,8 @@
|
||||
import {memberHelper} from "./memberHelper.js";
|
||||
const memberHelper = require('./memberHelper.js');
|
||||
|
||||
const msgh = {};
|
||||
const messageHelper = {};
|
||||
|
||||
msgh.prefix = "pf;"
|
||||
messageHelper.prefix = "pf;"
|
||||
|
||||
/**
|
||||
* Parses and slices up message arguments, retaining quoted strings.
|
||||
@@ -11,8 +11,8 @@ msgh.prefix = "pf;"
|
||||
* @param {string} commandName - The command name.
|
||||
* @returns {string[]} An array of arguments.
|
||||
*/
|
||||
msgh.parseCommandArgs = function(content, commandName) {
|
||||
const message = content.slice(msgh.prefix.length + commandName.length).trim();
|
||||
messageHelper.parseCommandArgs = function(content, commandName) {
|
||||
const message = content.slice(messageHelper.prefix.length + commandName.length).trim();
|
||||
|
||||
return message.match(/\\?.|^$/g).reduce((accumulator, chara) => {
|
||||
if (chara === '\"' || chara === '\'') {
|
||||
@@ -38,7 +38,7 @@ msgh.parseCommandArgs = function(content, commandName) {
|
||||
* @param {string | null} [attachmentUrl] - The url for an attachment to the message, if any exists.
|
||||
* @returns {Promise<{model, string, bool}>} The proxy message object.
|
||||
*/
|
||||
msgh.parseProxyTags = async function (authorId, content, attachmentUrl = null){
|
||||
messageHelper.parseProxyTags = async function (authorId, content, attachmentUrl = null){
|
||||
const members = await memberHelper.getMembersByAuthor(authorId);
|
||||
// If an author has no members, no sense in searching for proxy
|
||||
if (members.length === 0) {
|
||||
@@ -70,7 +70,7 @@ msgh.parseProxyTags = async function (authorId, content, attachmentUrl = null){
|
||||
* @returns {{text: string, file: Buffer<ArrayBuffer> | undefined}} The text and buffer object
|
||||
*
|
||||
*/
|
||||
msgh.returnBufferFromText = function (text) {
|
||||
messageHelper.returnBufferFromText = function (text) {
|
||||
if (text.length > 2000) {
|
||||
const truncated = text.substring(0, 2000);
|
||||
const restOfText = text.substring(2000);
|
||||
@@ -80,4 +80,4 @@ msgh.returnBufferFromText = function (text) {
|
||||
return {text: text, file: undefined}
|
||||
}
|
||||
|
||||
export const messageHelper = msgh;
|
||||
module.exports = messageHelper;
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import {enums} from '../enums.js'
|
||||
const enums = require('../enums');
|
||||
|
||||
const u = {};
|
||||
const utils = {};
|
||||
|
||||
u.debounce = function(func, delay) {
|
||||
utils.debounce = function(func, delay) {
|
||||
let timeout = null;
|
||||
return function (...args) {
|
||||
clearTimeout(timeout);
|
||||
@@ -18,7 +18,7 @@ u.debounce = function(func, delay) {
|
||||
* @returns {bool} - Whether the image is in a valid format
|
||||
* @throws {Error} When loading the profile picture from a URL doesn't work, or it fails requirements.
|
||||
*/
|
||||
u.checkImageFormatValidity = async function (imageUrl) {
|
||||
utils.checkImageFormatValidity = async function (imageUrl) {
|
||||
const acceptableImages = ['image/png', 'image/jpg', 'image/jpeg', 'image/webp'];
|
||||
let response, blobFile;
|
||||
try {
|
||||
@@ -41,7 +41,7 @@ u.checkImageFormatValidity = async function (imageUrl) {
|
||||
* @param {string | null} [expirationString] - An expiration date string.
|
||||
* @returns {string | null} A description of the expiration, or null.
|
||||
*/
|
||||
u.setExpirationWarning = function (imgUrl = null, expirationString = null) {
|
||||
utils.setExpirationWarning = function (imgUrl = null, expirationString = null) {
|
||||
if (imgUrl && imgUrl.startsWith(enums.misc.FLUXER_ATTACHMENT_URL)) {
|
||||
return enums.misc.ATTACHMENT_EXPIRATION_WARNING;
|
||||
}
|
||||
@@ -54,4 +54,4 @@ u.setExpirationWarning = function (imgUrl = null, expirationString = null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
export const utils = u;
|
||||
module.exports = utils;
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import {messageHelper} from "./messageHelper.js";
|
||||
import {Webhook, Channel, Message, Client} from '@fluxerjs/core';
|
||||
import {enums} from "../enums.js";
|
||||
const {messageHelper} = require("./messageHelper.js");
|
||||
const {Webhook, Channel, Message, Client} = require('@fluxerjs/core');
|
||||
const {enums} = require("../enums.js");
|
||||
|
||||
const wh = {};
|
||||
const webhookHelper = {};
|
||||
|
||||
const name = 'PluralFlux Proxy Webhook';
|
||||
|
||||
@@ -13,7 +13,7 @@ const name = 'PluralFlux Proxy Webhook';
|
||||
* @param {Message} message - The full message object.
|
||||
* @throws {Error} When the proxy message is not in a server.
|
||||
*/
|
||||
wh.sendMessageAsMember = async function(client, message) {
|
||||
webhookHelper.sendMessageAsMember = async function(client, message) {
|
||||
const attachmentUrl = message.attachments.size > 0 ? message.attachments.first().url : null;
|
||||
const proxyMatch = await messageHelper.parseProxyTags(message.author.id, message.content, attachmentUrl);
|
||||
// If the message doesn't match a proxy, just return.
|
||||
@@ -27,7 +27,7 @@ wh.sendMessageAsMember = async function(client, message) {
|
||||
if (proxyMatch.hasAttachment) {
|
||||
return await message.reply(`${enums.misc.ATTACHMENT_SENT_BY} ${proxyMatch.member.displayname ?? proxyMatch.member.name}`)
|
||||
}
|
||||
await wh.replaceMessage(client, message, proxyMatch.message, proxyMatch.member);
|
||||
await webhookHelper.replaceMessage(client, message, proxyMatch.message, proxyMatch.member);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -39,11 +39,11 @@ wh.sendMessageAsMember = async function(client, message) {
|
||||
* @param {model} member - A member object from the database.
|
||||
* @throws {Error} When there's no message to send.
|
||||
*/
|
||||
wh.replaceMessage = async function(client, message, text, member) {
|
||||
webhookHelper.replaceMessage = async function(client, message, text, member) {
|
||||
// attachment logic is not relevant yet, text length will always be over 0 right now
|
||||
if (text.length > 0 || message.attachments.size > 0) {
|
||||
const channel = client.channels.get(message.channelId);
|
||||
const webhook = await wh.getOrCreateWebhook(client, channel);
|
||||
const webhook = await webhookHelper.getOrCreateWebhook(client, channel);
|
||||
const username = member.displayname ?? member.name;
|
||||
if (text.length <= 2000) {
|
||||
await webhook.send({content: text, username: username, avatar_url: member.propic})
|
||||
@@ -68,10 +68,10 @@ wh.replaceMessage = async function(client, message, text, member) {
|
||||
* @returns {Webhook} A webhook object.
|
||||
* @throws {Error} When no webhooks are allowed in the channel.
|
||||
*/
|
||||
wh.getOrCreateWebhook = async function(client, channel) {
|
||||
webhookHelper.getOrCreateWebhook = async function(client, channel) {
|
||||
// If channel doesn't allow webhooks
|
||||
if (!channel?.createWebhook) throw new Error(enums.err.NO_WEBHOOKS_ALLOWED);
|
||||
let webhook = await wh.getWebhook(client, channel)
|
||||
let webhook = await webhookHelper.getWebhook(client, channel)
|
||||
if (!webhook) {
|
||||
webhook = await channel.createWebhook({name: name});
|
||||
}
|
||||
@@ -85,7 +85,7 @@ wh.getOrCreateWebhook = async function(client, channel) {
|
||||
* @param {Channel} channel - The channel the message was sent in.
|
||||
* @returns {Webhook} A webhook object.
|
||||
*/
|
||||
wh.getWebhook = async function(client, channel) {
|
||||
webhookHelper.getWebhook = async function(client, channel) {
|
||||
const channelWebhooks = await channel?.fetchWebhooks() ?? [];
|
||||
if (channelWebhooks.length === 0) {
|
||||
return;
|
||||
@@ -93,4 +93,4 @@ wh.getWebhook = async function(client, channel) {
|
||||
return channelWebhooks.find((webhook) => webhook.name === name);
|
||||
}
|
||||
|
||||
export const webhookHelper = wh;
|
||||
module.exports = webhookHelper;
|
||||
Reference in New Issue
Block a user