feat: add tests and other such features (#3)

* converted import syntax to ES modules

removed unused methods

* got test sort of working (jest set up is not crashing but also not mocking correctly)

* adjusted beforeeach/beforeall so more pass

* more correct test setup

* converted import syntax to commonJS

removed unused methods

* got test sort of working (jest set up is not crashing but also not mocking correctly)

* adjusted beforeeach/beforeall so more pass

* more correct test setup

* more correct dockerfile and compose.yaml

* Revert "converted import syntax to commonJS"

This reverts commit 5ab0d62b

* updated jest to sort of work with es6

* separating out enum return from method return

* mostly working except for the weirdest error

* nevermind it wasn't actually working, gonna move on for now

* added babel to convert es modules to cjs

* finally figured out issue with tests (referencing the method directly in the test.each calls the real method not the mock in beforeEach())

* setup fixed more

* added error handling parseMemberCommand test

* renamed db to database
more tests and fixing logic for memberhelper

* upgraded fluxer.js

* moved import to helpers folder

* moved import to helpers folder

* more tests for member helper

* think i fixed weird error with webhook sending error when a user has no members

* simplified sendMessageAsAttachment

* added return to addFullMember so that addNewMember can reference it properly in strings

* test setup for messagehelper and webhookhelper

* readded line i shouldn't have removed in sendMessageAsMember

* fixed test and logic

* added test for memberHelper

* updated sendMessageAsAttachment to returnBufferFromText and updated commands/webhookHelper accordingly

* added tests for parseProxyTags and updated logic

* added "return" so tests dont terminate on failure and deleted env.jest

* finished tests for messageHelper!

* more cases for messageHelper just in case

* updating docstring for messageHelper parseProxyTags

* more tests for webhookhelper

* deleted extra file added during merge

* removed confusing brackets from enum docs

* finally mocking correctly

* adding more cases to messageHelper tests

* updating enums

* removed error response when proxy is sent without content

* , updated tests for webhookHelper and removed error response when proxy is sent without content

* added debounce to count guilds properly

* added todo note

* added tests for updateDisplayName

* edited help message trigger for updatePropic

* update message helper test to include space case

* update bot to suppress errors from API

* fixed bug for import not sending help text, added help text if you type a unrecognized command

* updated to be enum

* updated member helper and tests

* edit enums, tweak import content command

* removed unnecessary await and console.log

* made it easier to make a member

* added nicer error listing to importHelper

* updated documentation

* Merge branch 'main' of https://github.com/pieartsy/PluralFlux into add-tests

---------

Co-authored-by: Aster Fialla <asterfialla@gmail.com>
This commit is contained in:
2026-02-19 21:45:10 -05:00
committed by GitHub
parent d24bcc8438
commit 8fc590c062
18 changed files with 1337 additions and 212 deletions

1
.gitignore vendored
View File

@@ -5,3 +5,4 @@ config.json
coverage
log.txt
.env
oya.png

10
Dockerfile Normal file
View File

@@ -0,0 +1,10 @@
FROM node:20-alpine AS builder
WORKDIR /app
COPY package*.json ./
RUN npm ci --omit=dev
FROM node:20-alpine
WORKDIR /app
COPY --from=builder /app/node_modules ./node_modules
COPY . .
CMD ["node", "src/bot.js"]

View File

@@ -14,11 +14,20 @@ All commands are prefixed by `pf;`. Currently only a few are implemented.
- `pf;help` - Sends the current list of commands.
- `pf;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 (the stuff above), not anything else like birthdays or system handles (yet?).
- `pf;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.**"
- `pf;member` - Accesses the sub-commands related to editing proxy members. The available subcommands are:
- `new` - Creates a new member to proxy with, for example: `pf;member new jane`. The member name should ideally be short so you can write other commands with it easily.
You can optionally add a display name after the member name, for example: `pf;member new jane "Jane Doe | ze/hir"`. If it has spaces, put it in __double quotes__. The length limit is 32 characters.
- `new` - Creates a new member to proxy with, for example: `pf;member new jane`. The member name should ideally be short so you can write other commands with it easily. The order of values is `pf;member new [name] [displayname] [proxy] [propic]`, _without brackets_. The name is **required**, but the rest are optional.
Usage notes:
- If anything has spaces, put it in quotes: `"Jane Doe"`
- If anything is unset, and you want to set something after it (for ex: you haven't set a display name, but you want to add a proxy), put the unset value in empty quotes in the same position: "" If you leave it out, the bot will set things wrong.
- The maximum length of a display name is 32 characters.
- You can't use the same proxy for two different members.
- You can also upload an image directly instead of using a url.
Examples:
- Full example: `pf;member new jane "Jane Doe" J:text https://cdn.pixabay.com/photo/2023/10/20/19/07/aster-8330078_1280.jpg`
- Example with gaps: `pf;member new bob "Bob he/him" "" https://cdn.pixabay.com/photo/2016/05/09/11/09/tennis-1381230_1280.jpg
- `remove` - Removes a member based on their name, for example: `pf;member remove jane`.
- `name` - Updates the name for a specific member based on their current name, for ex: `pf;member john name jane`. The member name should ideally be short so you can write other commands with it easily.
- `list` - Lists all members in the system.
@@ -27,7 +36,7 @@ You can optionally add a display name after the member name, for example: `pf;me
1. Pass in a direct remote image URL, for example: `pf;member jane propic <https://cdn.pixabay.com/photo/2020/05/02/02/54/animal-5119676_1280.jpg>`. You can upload images on sites like <https://imgbb.com/>.
2. Upload an attachment directly.
**NOTE:** Fluxer does not save your attachments forever, so option #1 is recommended.
- `proxy` Updates the proxy tag for a specific member based on their name. The proxy must be formatted with the tags surrounding the word 'text', for example: `pf;member jane proxy Jane:text` or `pf;member amal proxy [text]` This is so the bot can detect what the proxy tags are. Only one proxy can be set per member currently.
- `proxy` Updates the proxy tag for a specific member based on their name. The proxy must be formatted with the tags surrounding the word 'text', for example: `pf;member jane proxy Jane:text` or `pf;member amal proxy [text]` This is so the bot can detect what the proxy tags are. **Only one proxy can be set per member currently.**
## Notes
- Attaching files to messages with the proxy does not work, due to either the limitations of Fluxer itself :(

8
babel.config.js Normal file
View File

@@ -0,0 +1,8 @@
// babel.config.js
module.exports = {
env: {
test: {
plugins: ["@babel/plugin-transform-modules-commonjs"]
}
}
};

View File

@@ -2,6 +2,9 @@ services:
main:
build: .
container_name: pluralflux
restart: unless-stopped
networks:
- pluralflux-net
postgres:
image: postgres:latest
container_name: pluralflux-postgres
@@ -13,20 +16,27 @@ services:
- pgdata:/var/lib/postgresql
ports:
- "5432:5432"
# pgadmin:
# image: dpage/pgadmin4:latest
# ports:
# - 5050:80
# environment:
# # Required by pgAdmin
# PGADMIN_DEFAULT_EMAIL: pieartsy@pm.me
# PGADMIN_DEFAULT_PASSWORD_FILE: /run/secrets/postgres_pwd
# # Don't require the user to login
# PGADMIN_CONFIG_SERVER_MODE: 'False'
# # Don't require a "master" password after logging in
# PGADMIN_CONFIG_MASTER_PASSWORD_REQUIRED: 'False'
# secrets:
# - postgres_pwd
networks:
- pluralflux-net
pgadmin:
image: dpage/pgadmin4:latest
container_name: pluralflux-pgadmin
ports:
- "5050:80"
environment:
PGADMIN_DEFAULT_EMAIL: code@asterfialla.com
PGADMIN_DEFAULT_PASSWORD_FILE: /run/secrets/postgres_pwd
PGADMIN_CONFIG_SERVER_MODE: 'False'
PGADMIN_CONFIG_MASTER_PASSWORD_REQUIRED: 'False'
secrets:
- postgres_pwd
depends_on:
- postgres
networks:
- pluralflux-net
networks:
pluralflux-net:
volumes:
pgdata:

10
jest.config.js Normal file
View File

@@ -0,0 +1,10 @@
// jest.config.js
module.exports = {
clearMocks: true,
collectCoverage: true,
coverageDirectory: "coverage",
verbose: true,
transform: {
"^.+\\.[t|j]sx?$": require.resolve('babel-jest')
},
};

View File

@@ -3,14 +3,13 @@
"version": "1.0.0",
"description": "",
"main": "src/bot.js",
"type": "module",
"repository": {
"type": "git",
"url": "https://github.com/pieartsy/PluralFlux.git"
},
"private": true,
"dependencies": {
"@fluxerjs/core": "^1.0.9",
"@fluxerjs/core": "^1.1.5",
"dotenv": "^17.3.1",
"pg": "^8.18.0",
"pg-hstore": "^2.3.4",
@@ -19,6 +18,10 @@
"tmp": "^0.2.5"
},
"devDependencies": {
"@babel/core": "^7.29.0",
"@babel/plugin-transform-modules-commonjs": "^7.28.6",
"@babel/preset-env": "^7.29.0",
"babel-jest": "^30.2.0",
"jest": "^30.2.0"
},
"scripts": {

View File

@@ -1,4 +1,4 @@
import { Client, Events, GatewayOpcodes } from '@fluxerjs/core';
import { Client, Events } from '@fluxerjs/core';
import { messageHelper } from "./helpers/messageHelper.js";
import {enums} from "./enums.js";
import {commands} from "./commands.js";
@@ -34,7 +34,7 @@ client.on(Events.MessageCreate, async (message) => {
const commandName = content.slice(messageHelper.prefix.length).split(" ")[0];
// If there's no command name (ie just the prefix)
if (!commandName) await message.reply(enums.help.SHORT_DESC_PLURALFLUX);
if (!commandName) return await message.reply(enums.help.SHORT_DESC_PLURALFLUX);
const args = messageHelper.parseCommandArgs(content, commandName);
@@ -44,18 +44,40 @@ client.on(Events.MessageCreate, async (message) => {
throw e
});
}
else {
await message.reply(enums.err.COMMAND_NOT_RECOGNIZED);
}
}
catch(error) {
console.error(error);
return await message.reply(error.message);
// return await message.reply(error.message);
}
});
client.on(Events.Ready, () => {
console.log(`Logged in as ${client.user?.username}`);
console.log(`Serving ${client.guilds.size} guild(s)`);
});
let guildCount = 0;
client.on(Events.GuildCreate, () => {
guildCount++;
callback();
});
function printGuilds() {
console.log(`Serving ${client.guilds.size} guild(s)`);
}
const callback = Debounce(printGuilds, 2000);
function Debounce(func, delay) {
let timeout = null;
return function (...args) {
clearTimeout(timeout);
timeout = setTimeout(() => func(...args), delay);
};
}
try {
await client.login(token);
// await db.check_connection();

View File

@@ -2,7 +2,7 @@ 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 "./import.js";
import {importHelper} from "./helpers/importHelper.js";
const cmds = new Map();
@@ -12,13 +12,17 @@ cmds.set('member', {
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;
const reply = await memberHelper.parseMemberCommand(message.author.id, authorFull, args, attachmentUrl, attachmentExpires).catch(e =>{throw e});
const reply = await memberHelper.parseMemberCommand(message.author.id, authorFull, args, attachmentUrl, attachmentExpires).catch(async (e) =>{await message.reply(e.message);});
if (typeof reply === 'string') {
return await message.reply(reply);
}
else if (reply instanceof EmbedBuilder) {
await message.reply({embeds: [reply.toJSON()]})
}
else if (typeof reply === 'object') {
const errorsText = reply.errors.length > 0 ? reply.errors.join('\n- ') : null;
return await message.reply({content: `${reply.success} ${errorsText ? "\nThese errors occurred:\n" + errorsText : ""}`, embeds: [reply.embed.toJSON()]})
}
}
})
@@ -45,12 +49,11 @@ cmds.set('help', {
cmds.set('import', {
description: enums.help.SHORT_DESC_IMPORT,
async execute(message) {
if (message.content.includes('--help')) {
async execute(message, client, 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);
}
const attachmentUrl = message.attachments.size > 0 ? message.attachments.first().url : null;
return await importHelper.pluralKitImport(message.author.id, attachmentUrl).then(async (successfullyAdded) => {
await message.reply(successfullyAdded);
}).catch(async (error) => {
@@ -59,7 +62,9 @@ cmds.set('import', {
let errorsText = `${error.message}.\nThese errors occurred:\n${error.errors.join('\n')}`;
await message.reply(errorsText).catch(async () => {
await messageHelper.sendMessageAsAttachment(errorsText, message);
const returnedBuffer = messageHelper.returnBufferFromText(errorsText);
await message.reply({content: returnedBuffer.text, files: [{ name: 'text.pdf', data: returnedBuffer.file }]
})
});
}
// If just one error was returned.

View File

@@ -10,7 +10,7 @@ if (!password) {
process.exit(1);
}
const database = {};
const db = {};
const sequelize = new Sequelize('postgres', 'postgres', password, {
host: 'localhost',
@@ -18,10 +18,10 @@ const sequelize = new Sequelize('postgres', 'postgres', password, {
dialect: 'postgres'
});
database.sequelize = sequelize;
database.Sequelize = Sequelize;
db.sequelize = sequelize;
db.Sequelize = Sequelize;
database.members = sequelize.define('Member', {
db.members = sequelize.define('Member', {
userid: {
type: DataTypes.STRING,
allowNull: false,
@@ -41,7 +41,7 @@ database.members = sequelize.define('Member', {
}
});
database.systems = sequelize.define('System', {
db.systems = sequelize.define('System', {
userid: {
type: DataTypes.STRING,
},
@@ -59,8 +59,8 @@ database.systems = sequelize.define('System', {
/**
* Checks Sequelize database connection.
*/
database.check_connection = async function() {
await sequelize.authenticate().then(async (result) => {
db.check_connection = async function() {
await sequelize.authenticate().then(async () => {
console.log('Connection has been established successfully.');
await syncModels();
}).catch(err => {
@@ -81,4 +81,4 @@ async function syncModels() {
});
}
export const db = database;
export const database = db;

View File

@@ -7,7 +7,7 @@ helperEnums.err = {
ADD_ERROR: "Error adding member.",
MEMBER_EXISTS: "A member with that name already exists. Please pick a unique name.",
USER_NO_MEMBERS: "You have no members created.",
DISPLAY_NAME_TOO_LONG: "The display name is too long. Please limit it to 32 characters or less.",
DISPLAY_NAME_TOO_LONG: "The maximum length of a display name is 32 characters.",
PROXY_EXISTS: "A duplicate proxy already exists for one of your members. Please pick a new one, or change the old one first.",
NO_SUCH_COMMAND: "No such command exists.",
PROPIC_FAILS_REQUIREMENTS: "Profile picture must be in JPG, PNG, or WEBP format and less than 10MB.",
@@ -20,6 +20,8 @@ helperEnums.err = {
NOT_JSON_FILE: "Please attach a valid JSON file.",
NO_MEMBERS_IMPORTED: 'No members were imported.',
IMPORT_ERROR: "Please see attached file for logs on the member import process.",
COMMAND_NOT_RECOGNIZED: "Command not recognized. Try typing `pf;help` for command list.",
SET_TO_NULL: "It has been set to null instead."
}
helperEnums.help = {
@@ -29,14 +31,14 @@ helperEnums.help = {
SHORT_DESC_PLURALFLUX: "PluralFlux is a proxybot akin to PluralKit and Tupperbot, but for Fluxer. All commands are prefixed by `pf;`. Type `pf;help` for info on the bot itself.",
PLURALFLUX: "PluralFlux is a proxybot akin to PluralKit and Tupperbot, but for Fluxer. All commands are prefixed by `pf;`. Add ` --help` to the end of a command to find out more about it, or just send it without arguments.",
MEMBER: "Accesses the sub-commands related to editing proxy members. The available subcommands are `list`, `new`, `remove`, `displayname`, `proxy`, and `propic`. Add ` --help` to the end of a subcommand to find out more about it, or just send it without arguments.",
NEW: "Creates a new member to proxy with, for example: `pf;member new jane`. The member name should ideally be short so you can write other commands with it easily. \n\nYou can optionally add a display name after the member name, for example: `pf;member new jane \"Jane Doe | ze/hir\"`. If it has spaces, put it in __double quotes__. The length limit is 32 characters.",
NEW: "Creates a new member to proxy with, for example: `pf;member new jane`. The member name should ideally be short so you can write other commands with it easily. \n\nThe order of values is `pf;member new [name] [displayname] [proxy] [propic]`, _without brackets_. The name is **required**, but the rest are optional.\nUsage notes:\n- If anything has spaces, put it in quotes.\n- If anything is unset and you want to set something after it (for ex: you haven't set a display name but you want to add a proxy), put the unset value in empty quotes in the same position: \"\" If you leave it out, the bot will set things wrong.\n- The maximum length of a display name is 32 characters.\n- You can't use the same proxy for two different members.\n- You can also upload an image directly instead of using a url.\nExamples:\n- Everything filled out: `pf;member new jane \"Jane Doe\" J:text https://cdn.pixabay.com/photo/2023/10/20/19/07/aster-8330078_1280.jpg`\n- Example with gaps: `pf;member new bob \"Bob he/him\" \"\" https://cdn.pixabay.com/photo/2016/05/09/11/09/tennis-1381230_1280.jpg`",
REMOVE: "Removes a member based on their name, for example: `pf;member remove jane`.",
LIST: "Lists members in the system. Currently only lists the first 25.",
NAME: "Updates the name for a specific member based on their current name, for ex: `pf;member john name jane`. The member name should ideally be short so you can write other commands with it easily.",
DISPLAY_NAME: "Updates the display name for a specific member based on their name, for example: `pf;member jane \"Jane Doe | ze/hir\"`.This can be up to 32 characters long. If it has spaces, put it in __double quotes__.",
PROXY: "Updates the proxy tag for a specific member based on their name. The proxy must be formatted with the tags surrounding the word 'text', for example: `pf;member jane proxy Jane:text` or `pf;member amal proxy [text]` This is so the bot can detect what the proxy tags are. Only one proxy can be set per member currently.",
PROPIC: "Updates the profile picture for the member. Must be in JPG, PNG, or WEBP format and less than 10MB. The two options are:\n1. Pass in a direct remote image URL, for example: `pf;member jane propic <https://cdn.pixabay.com/photo/2020/05/02/02/54/animal-5119676_1280.jpg>`. You can upload images on sites like <https://imgbb.com/>.\n2. Upload an attachment directly.\n\n**NOTE:** Fluxer does not save your attachments forever, so option #1 is recommended.",
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 (the stuff above), not anything else like birthdays or system handles (yet?)."
PROXY: "Updates the proxy tag for a specific member based on their name. The proxy must be formatted with the tags surrounding the word 'text', for example: `pf;member jane proxy Jane:text` or `pf;member amal proxy [text]` This is so the bot can detect what the proxy tags are. **Only one proxy can be set per member currently.**",
PROPIC: "Updates the profile picture for the member. Must be in JPG, PNG, or WEBP format and less than 10MB. The two options are:\n1. Pass in a direct remote image URL, for example: `pf;member jane propic https://cdn.pixabay.com/photo/2020/05/02/02/54/animal-5119676_1280.jpg`. You can upload images on sites like https://imgbb.com/.\n2. Upload an attachment directly.\n\n**NOTE:** Fluxer does not save your attachments forever, so option #1 is recommended.",
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.**"
}
helperEnums.misc = {

View File

@@ -1,6 +1,5 @@
import {enums} from "./enums.js";
import {memberHelper} from "./helpers/memberHelper.js";
import {messageHelper} from "./helpers/messageHelper.js";
import {enums} from "../enums.js";
import {memberHelper} from "./memberHelper.js";
const ih = {};
@@ -19,17 +18,19 @@ ih.pluralKitImport = async function (authorId, attachmentUrl) {
}
return fetch(attachmentUrl).then((res) => res.json()).then(async(pkData) => {
const pkMembers = pkData.members;
const errors = [];
let errors = [];
const addedMembers = [];
for (let pkMember of pkMembers) {
const proxy = pkMember.proxy_tags[0] ? `${pkMember.proxy_tags[0].prefix ?? ''}text${pkMember.proxy_tags[0].suffix ?? ''}` : null;
await memberHelper.addFullMember(authorId, pkMember.name, pkMember.display_name, proxy, pkMember.avatar_url, true).then((member) => {
addedMembers.push(member.name);
await memberHelper.addFullMember(authorId, pkMember.name, pkMember.display_name, proxy, pkMember.avatar_url).then((memberObj) => {
addedMembers.push(memberObj.member.name);
if (memberObj.errors.length > 0) {
errors.push(`\n**${pkMember.name}:** `)
errors = errors.concat(memberObj.errors);
}
}).catch(e => {
errors.push(`${pkMember.name}: ${e.message}`);
errors.push(e.message);
});
await memberHelper.checkImageFormatValidity(pkMember.avatar_url).catch(e => {
errors.push(`${pkMember.name}: ${e.message}`)});
}
const aggregatedText = addedMembers.length > 0 ? `Successfully added members: ${addedMembers.join(', ')}` : enums.err.NO_MEMBERS_IMPORTED;
if (errors.length > 0) {

View File

@@ -1,4 +1,4 @@
import {db} from '../db.js';
import {database} from '../database.js';
import {enums} from "../enums.js";
import {EmptyResultError, Op} from "sequelize";
import {EmbedBuilder} from "@fluxerjs/core";
@@ -17,22 +17,28 @@ const commandList = ['--help', 'new', 'remove', 'name', 'list', 'displayName', '
* @param {string[]} args - The message arguments
* @param {string | null} attachmentUrl - The message attachment url.
* @param {string | null} attachmentExpiration - The message attachment expiration (if uploaded via Fluxer)
* @returns {Promise<string> | Promise <EmbedBuilder>} A message, or an informational embed.
* @returns {Promise<string>} A success message.
* @returns {Promise <EmbedBuilder>} A list of 25 members as an embed.
* @returns {Promise<{EmbedBuilder, [], string}>} A member info embed + info/errors.
* @throws {Error}
*/
mh.parseMemberCommand = async function(authorId, authorFull, args, attachmentUrl = null, attachmentExpiration = null){
mh.parseMemberCommand = async function (authorId, authorFull, args, attachmentUrl = null, attachmentExpiration = null) {
let member;
// checks whether command is in list, otherwise assumes it's a name
if(!commandList.includes(args[0])) {
if (!commandList.includes(args[0]) && !args[1]) {
member = await mh.getMemberInfo(authorId, args[0]);
}
switch(args[0]) {
switch (args[0]) {
case '--help':
return enums.help.MEMBER;
case 'new':
return await mh.addNewMember(authorId, args).catch((e) =>{throw e});
return await mh.addNewMember(authorId, args, attachmentUrl).catch((e) => {
throw e
});
case 'remove':
return await mh.removeMember(authorId, args).catch((e) =>{throw e});
return await mh.removeMember(authorId, args).catch((e) => {
throw e
});
case 'name':
return enums.help.NAME;
case 'displayname':
@@ -45,20 +51,32 @@ mh.parseMemberCommand = async function(authorId, authorFull, args, attachmentUrl
if (args[1] && args[1] === "--help") {
return enums.help.LIST;
}
return await mh.getAllMembersInfo(authorId, authorFull).catch((e) =>{throw e});
return await mh.getAllMembersInfo(authorId, authorFull).catch((e) => {
throw e
});
case '':
return enums.help.MEMBER;
}
switch(args[1]) {
switch (args[1]) {
case 'name':
return await mh.updateName(authorId, args).catch((e) =>{throw e});
return await mh.updateName(authorId, args).catch((e) => {
throw e
});
case 'displayname':
return await mh.updateDisplayName(authorId, args).catch((e) =>{throw e});
return await mh.updateDisplayName(authorId, args).catch((e) => {
throw e
});
case 'proxy':
if (!args[2]) return await mh.getProxyByMember(authorId, args[0]).catch((e) => {throw e});
return await mh.updateProxy(authorId, args).catch((e) =>{throw e});
if (!args[2]) return await mh.getProxyByMember(authorId, args[0]).catch((e) => {
throw e
});
return await mh.updateProxy(authorId, args).catch((e) => {
throw e
});
case 'propic':
return await mh.updatePropic(authorId, args, attachmentUrl, attachmentExpiration).catch((e) =>{throw e});
return await mh.updatePropic(authorId, args, attachmentUrl, attachmentExpiration).catch((e) => {
throw e
});
default:
return member;
}
@@ -70,20 +88,22 @@ mh.parseMemberCommand = async function(authorId, authorFull, args, attachmentUrl
* @async
* @param {string} authorId - The author of the message
* @param {string[]} args - The message arguments
* @param {string | null} attachmentURL - The attachment URL, if any exists
* @returns {Promise<string>} A successful addition.
* @throws {Error} When the member exists, or creating a member doesn't work.
*/
mh.addNewMember = async function(authorId, args) {
mh.addNewMember = async function (authorId, args, attachmentURL = null) {
if (args[1] && args[1] === "--help" || !args[1]) {
return enums.help.NEW;
}
const memberName = args[1];
const displayName = args[2];
const proxy = args[3];
const propic = args[4] ?? attachmentURL;
return await mh.addFullMember(authorId, memberName, displayName).then((member) => {
let success = `Member was successfully added.\nName: ${member.dataValues.name}`
success += displayName ? `\nDisplay name: ${member.dataValues.displayname}` : "";
return success;
return await mh.addFullMember(authorId, memberName, displayName, proxy, propic).then(async(response) => {
const memberInfoEmbed = await mh.getMemberInfo(authorId, memberName).catch((e) => {throw e})
return {embed: memberInfoEmbed, errors: response.errors, success: `${memberName} has been added successfully.`};
}).catch(e => {
throw e;
})
@@ -99,16 +119,21 @@ mh.addNewMember = async function(authorId, args) {
* @throws {RangeError} When the name doesn't exist.
*/
mh.updateName = async function (authorId, args) {
if (args[1] && args[1] === "--help" || !args[1]) {
return enums.help.DISPLAY_NAME;
if (args[2] && args[2] === "--help") {
return enums.help.NAME;
}
const name = args[2];
const trimmedName = name ? name.trim() : null;
if (!name || trimmedName === null) {
throw new RangeError(`Display name ${enums.err.NO_VALUE}`);
if (!name) {
return `The name for ${args[0]} is ${args[0]}, but you probably knew that!`;
}
return await mh.updateMemberField(authorId, args).catch((e) =>{throw e});
const trimmedName = name.trim();
if (trimmedName === '') {
throw new RangeError(`Name ${enums.err.NO_VALUE}`);
}
return await mh.updateMemberField(authorId, args).catch((e) => {
throw e
});
}
/**
@@ -120,8 +145,8 @@ mh.updateName = async function (authorId, args) {
* @returns {Promise<string>} A successful update.
* @throws {RangeError} When the display name is too long or doesn't exist.
*/
mh.updateDisplayName = async function(authorId, args) {
if (args[1] && args[1] === "--help" || !args[1]) {
mh.updateDisplayName = async function (authorId, args) {
if (args[2] && args[2] === "--help") {
return enums.help.DISPLAY_NAME;
}
@@ -129,20 +154,23 @@ mh.updateDisplayName = async function(authorId, args) {
const displayName = args[2];
const trimmedName = displayName ? displayName.trim() : null;
if (!displayName || trimmedName === null ) {
if (!displayName) {
return await mh.getMemberByName(authorId, memberName).then((member) => {
if (member && member.displayname) {
return `Display name for ${memberName} is: \"${member.displayname}\".`;
}
else if (member) {
throw new RangeError(`Display name ${enums.err.NO_VALUE}`);
} else if (member) {
throw new Error(`Display name ${enums.err.NO_VALUE}`);
}
});
}
else if (displayName.length > 32) {
} else if (displayName.length > 32) {
throw new RangeError(enums.err.DISPLAY_NAME_TOO_LONG);
}
return await mh.updateMemberField(authorId, args).catch((e) =>{throw e});
else if (trimmedName === '') {
throw new RangeError(`Display name ${enums.err.NO_VALUE}`);
}
return await mh.updateMemberField(authorId, args).catch((e) => {
throw e
});
}
/**
@@ -154,15 +182,19 @@ mh.updateDisplayName = async function(authorId, args) {
* @returns {Promise<string> } A successful update.
* @throws {RangeError | Error} When an empty proxy was provided, or no proxy exists.
*/
mh.updateProxy = async function(authorId, args) {
mh.updateProxy = async function (authorId, args) {
if (args[2] && args[2] === "--help") {
return enums.help.PROXY;
}
const proxyExists = await mh.checkIfProxyExists(authorId, args[2]).then((proxyExists) => {
return proxyExists;
}).catch((e) =>{throw e});
}).catch((e) => {
throw e
});
if (!proxyExists) {
return await mh.updateMemberField(authorId, args).catch((e) =>{throw e});
return await mh.updateMemberField(authorId, args).catch((e) => {
throw e
});
}
}
@@ -177,8 +209,8 @@ mh.updateProxy = async function(authorId, args) {
* @returns {Promise<string>} A successful update.
* @throws {Error} When loading the profile picture from a URL doesn't work.
*/
mh.updatePropic = async function(authorId, args, attachmentUrl, attachmentExpiry= null) {
if (args[1] && args[1] === "--help") {
mh.updatePropic = async function (authorId, args, attachmentUrl, attachmentExpiry = null) {
if (args[2] && args[2] === "--help") {
return enums.help.PROPIC;
}
let img;
@@ -192,9 +224,13 @@ mh.updatePropic = async function(authorId, args, attachmentUrl, attachmentExpiry
if (updatedArgs[2]) {
img = updatedArgs[2];
}
const isValidImage = await mh.checkImageFormatValidity(img).catch((e) =>{throw e});
const isValidImage = await mh.checkImageFormatValidity(img).catch((e) => {
throw e
});
if (isValidImage) {
return await mh.updateMemberField(authorId, updatedArgs).catch((e) =>{throw e});
return await mh.updateMemberField(authorId, updatedArgs).catch((e) => {
throw e
});
}
}
@@ -206,7 +242,7 @@ mh.updatePropic = async function(authorId, args, attachmentUrl, attachmentExpiry
* @returns {Promise<boolean>} - If the image is a valid format.
* @throws {Error} When loading the profile picture from a URL doesn't work, or it fails requirements.
*/
mh.checkImageFormatValidity = async function(imageUrl) {
mh.checkImageFormatValidity = async function (imageUrl) {
const acceptableImages = ['image/png', 'image/jpg', 'image/jpeg', 'image/webp'];
return await fetch(imageUrl).then(r => r.blob()).then(blobFile => {
if (blobFile.size > 1000000 || !acceptableImages.includes(blobFile.type)) throw new Error(enums.err.PROPIC_FAILS_REQUIREMENTS);
@@ -225,13 +261,18 @@ mh.checkImageFormatValidity = async function(imageUrl) {
* @returns {Promise<string>} A successful removal.
* @throws {EmptyResultError} When there is no member to remove.
*/
mh.removeMember = async function(authorId, args) {
mh.removeMember = async function (authorId, args) {
if (args[1] && args[1] === "--help" || !args[1]) {
return enums.help.REMOVE;
}
const memberName = args[1];
return await db.members.destroy({ where: { name: {[Op.iLike]: memberName}, userid: authorId } }).then((result) => {
return await database.members.destroy({
where: {
name: {[Op.iLike]: memberName},
userid: authorId
}
}).then((result) => {
if (result) {
return `Member "${memberName}" has been deleted.`;
}
@@ -250,49 +291,157 @@ mh.removeMember = async function(authorId, args) {
* @param {string | null} displayName - The display name of the member.
* @param {string | null} proxy - The proxy tag of the member.
* @param {string | null} propic - The profile picture URL of the member.
* @param {boolean} isImport - Whether calling from the import function or not.
* @returns {Promise<model>} A successful addition.
* @throws {Error | RangeError} When the member already exists, there are validation errors, or adding a member doesn't work.
* @returns {Promise<{model, []}>} 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, isImport = false) {
mh.addFullMember = async function (authorId, memberName, displayName = null, proxy = null, propic = null) {
await mh.getMemberByName(authorId, memberName).then((member) => {
if (member) {
throw new Error(`Can't add ${memberName}. ${enums.err.MEMBER_EXISTS}`);
}
});
if (displayName) {
const errors = [];
let isValidDisplayName;
if (displayName && displayName.length > 0) {
const trimmedName = displayName ? displayName.trim() : null;
if (trimmedName && trimmedName.length > 32) {
throw new RangeError(`Can't add ${memberName}. ${enums.err.DISPLAY_NAME_TOO_LONG}`);
errors.push(`Tried to set displayname to \"${displayName}\". ${enums.err.DISPLAY_NAME_TOO_LONG}. ${enums.err.SET_TO_NULL}`);
isValidDisplayName = false;
}
else {
isValidDisplayName = true;
}
}
if (proxy) {
await mh.checkIfProxyExists(authorId, proxy).catch((e) =>{throw e});
}
let validPropic;
if (propic) {
validPropic = await mh.checkImageFormatValidity(propic).then((valid) => {
return valid;
}).catch((e) =>{
if (!isImport) {
throw (e);
}
return false;
let isValidProxy;
if (proxy && proxy.length > 0) {
await mh.checkIfProxyExists(authorId, proxy).then(() => {
isValidProxy = true;
}).catch((e) => {
errors.push(`Tried to set proxy to \"${proxy}\". ${e.message}. ${enums.err.SET_TO_NULL}`);
isValidProxy = false;
});
}
const member = await db.members.create({
name: memberName,
userid: authorId,
displayname: displayName,
proxy: proxy,
propic: validPropic ? propic : null,
let isValidPropic;
if (propic && propic.length > 0) {
await mh.checkImageFormatValidity(propic).then(() => {
isValidPropic = true;
}).catch((e) => {
errors.push(`Tried to set profile picture to \"${propic}\". ${e.message}. ${enums.err.SET_TO_NULL}`);
isValidPropic = false;
});
if (!member) {
new Error(`${enums.err.ADD_ERROR}: ${e.message}`);
}
const member = await database.members.create({
name: memberName, userid: authorId, displayname: isValidDisplayName ? displayName : null, proxy: isValidProxy ? proxy : null, propic: isValidPropic ? propic : null
});
return {member: member, errors: errors};
}
// mh.mergeFullMember = async function (authorId, memberName, displayName = null, proxy = null, propic = null) {
// await mh.getMemberByName(authorId, memberName).then((member) => {
// if (member) {
// throw new Error(`Can't add ${memberName}. ${enums.err.MEMBER_EXISTS}`);
// }
// });
//
// let isValidDisplayName;
// if (displayName) {
// const trimmedName = displayName ? displayName.trim() : null;
// if (trimmedName && trimmedName.length > 32) {
// if (!isImport) {
// throw new RangeError(`Can't add ${memberName}. ${enums.err.DISPLAY_NAME_TOO_LONG}`);
// }
// isValidDisplayName = false;
// }
// }
//
// let isValidProxy;
// if (proxy) {
// isValidProxy = await mh.checkIfProxyExists(authorId, proxy).then((res) => {
// return res;
// }).catch((e) => {
// if (!isImport) {
// throw e
// }
// return false;
// });
// }
//
// let isValidPropic;
// if (propic) {
// isValidPropic = await mh.checkImageFormatValidity(propic).then((valid) => {
// return valid;
// }).catch((e) => {
// if (!isImport) {
// throw (e);
// }
// return false;
// });
// }
//
// const member = await database.members.create({
// name: memberName, userid: authorId, displayname: isValidDisplayName ? displayName: null, proxy: isValidProxy ? proxy : null, propic: isValidPropic ? propic : null,
// });
// if (!member) {
// new Error(`${enums.err.ADD_ERROR}`);
// }
// return member;
// }
//
// mh.overwriteFullMemberFromImport = async function (authorId, memberName, displayName = null, proxy = null, propic = null) {
// await mh.getMemberByName(authorId, memberName).then((member) => {
// if (member) {
// throw new Error(`Can't add ${memberName}. ${enums.err.MEMBER_EXISTS}`);
// }
// });
//
// let isValidDisplayName;
// if (displayName) {
// const trimmedName = displayName ? displayName.trim() : null;
// if (trimmedName && trimmedName.length > 32) {
// if (!isImport) {
// throw new RangeError(`Can't add ${memberName}. ${enums.err.DISPLAY_NAME_TOO_LONG}`);
// }
// isValidDisplayName = false;
// }
// }
//
// let isValidProxy;
// if (proxy) {
// isValidProxy = await mh.checkIfProxyExists(authorId, proxy).then((res) => {
// return res;
// }).catch((e) => {
// if (!isImport) {
// throw e
// }
// return false;
// });
// }
//
// let isValidPropic;
// if (propic) {
// isValidPropic = await mh.checkImageFormatValidity(propic).then((valid) => {
// return valid;
// }).catch((e) => {
// if (!isImport) {
// throw (e);
// }
// return false;
// });
// }
//
// const member = await database.members.create({
// name: memberName, userid: authorId, displayname: isValidDisplayName ? displayName: null, proxy: isValidProxy ? proxy : null, propic: isValidPropic ? propic : null,
// });
// if (!member) {
// new Error(`${enums.err.ADD_ERROR}`);
// }
// return member;
// }
/**
* Updates one fields for a member in the database.
*
@@ -302,7 +451,7 @@ mh.addFullMember = async function(authorId, memberName, displayName = null, prox
* @returns {Promise<string>} A successful update.
* @throws {EmptyResultError | Error} When the member is not found, or catchall error.
*/
mh.updateMemberField = async function(authorId, args) {
mh.updateMemberField = async function (authorId, args) {
const memberName = args[0];
const columnName = args[1];
const value = args[2];
@@ -312,16 +461,18 @@ mh.updateMemberField = async function(authorId, args) {
if (columnName === "propic" && args[3]) {
fluxerPropicWarning = mh.setExpirationWarning(args[3]);
}
return await db.members.update({[columnName]: value}, { where: { name: {[Op.iLike]: memberName}, userid: authorId } }).then(() => {
return await database.members.update({[columnName]: value}, {
where: {
name: {[Op.iLike]: memberName},
userid: authorId
}
}).then((res) => {
if (res[0] === 0) {
throw new EmptyResultError(`Can't update ${memberName}. ${enums.err.NO_MEMBER}.`);
} else {
return `Updated ${columnName} for ${memberName} to ${value}${fluxerPropicWarning ?? ''}.`;
}).catch(e => {
if (e === EmptyResultError) {
throw new EmptyResultError(`Can't update ${memberName}. ${enums.err.NO_MEMBER}: ${e.message}`);
}
else {
throw new Error(`Can't update ${memberName}. ${e.message}`);
}
});
})
}
/**
@@ -330,7 +481,7 @@ mh.updateMemberField = async function(authorId, args) {
* @param {string} expirationString - An expiration date string.
* @returns {string} A description of the expiration, interpolating the expiration string.
*/
mh.setExpirationWarning = function(expirationString) {
mh.setExpirationWarning = function (expirationString) {
let expirationDate = new Date(expirationString);
if (!isNaN(expirationDate.valueOf())) {
expirationDate = expirationDate.toDateString();
@@ -352,10 +503,11 @@ mh.getMemberInfo = async function (authorId, memberName) {
return new EmbedBuilder()
.setTitle(member.name)
.setDescription(`Details for ${member.name}`)
.addFields(
{name: 'Display name: ', value: member.displayname ?? 'unset', inline: true},
{name: 'Proxy tag: ', value: member.proxy ?? 'unset', inline: true},
)
.addFields({
name: 'Display name: ',
value: member.displayname ?? 'unset',
inline: true
}, {name: 'Proxy tag: ', value: member.proxy ?? 'unset', inline: true},)
.setImage(member.propic);
}
});
@@ -370,13 +522,11 @@ mh.getMemberInfo = async function (authorId, memberName) {
* @returns {Promise<EmbedBuilder>} The info for all members.
* @throws {Error} When there are no members for an author.
*/
mh.getAllMembersInfo = async function(authorId, authorName) {
mh.getAllMembersInfo = async function (authorId, authorName) {
const members = await mh.getMembersByAuthor(authorId);
if (members == null) throw Error(enums.err.USER_NO_MEMBERS);
const fields = [...members.entries()].map(([name, member]) => ({
name: member.name,
value: `(Proxy: \`${member.proxy ?? "unset"}\`)`,
inline: true,
name: member.name, value: `(Proxy: \`${member.proxy ?? "unset"}\`)`, inline: true,
}));
return new EmbedBuilder()
.setTitle(`${fields > 25 ? "First 25 m" : "M"}embers for ${authorName}`)
@@ -392,8 +542,8 @@ mh.getAllMembersInfo = async function(authorId, authorName) {
* @returns {Promise<model>} The member object.
* @throws { EmptyResultError } When the member is not found.
*/
mh.getMemberByName = async function(authorId, memberName) {
return await db.members.findOne({ where: { userid: authorId, name: {[Op.iLike]: memberName}}});
mh.getMemberByName = async function (authorId, memberName) {
return await database.members.findOne({where: {userid: authorId, name: {[Op.iLike]: memberName}}});
}
/**
@@ -405,10 +555,10 @@ mh.getMemberByName = async function(authorId, memberName) {
* @returns {Promise<string>} The member object.
* @throws { EmptyResultError } When the member is not found.
*/
mh.getProxyByMember = async function(authorId, memberName) {
mh.getProxyByMember = async function (authorId, memberName) {
return await mh.getMemberByName(authorId, memberName).then((member) => {
if (member) {
return member.dataValues.proxy;
return member.proxy;
}
throw new EmptyResultError(enums.err.NO_MEMBER);
})
@@ -422,8 +572,8 @@ mh.getProxyByMember = async function(authorId, memberName) {
* @param {string} proxy - The proxy tag
* @returns {Promise<model>} The member object.
*/
mh.getMemberByProxy = async function(authorId, proxy) {
return await db.members.findOne({ where: { userid: authorId, proxy: proxy } });
mh.getMemberByProxy = async function (authorId, proxy) {
return await db.members.findOne({where: {userid: authorId, proxy: proxy}});
}
/**
@@ -433,8 +583,8 @@ mh.getMemberByProxy = async function(authorId, proxy) {
* @param {string} authorId - The author of the message
* @returns {Promise<model[] | null>} The member object array.
*/
mh.getMembersByAuthor = async function(authorId) {
return await db.members.findAll({ where: { userid: authorId } });
mh.getMembersByAuthor = async function (authorId) {
return await database.members.findAll({where: {userid: authorId}});
}
@@ -446,18 +596,20 @@ 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) {
mh.checkIfProxyExists = async function (authorId, proxy) {
if (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);
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);
await mh.getMembersByAuthor(authorId).then((memberList) => {
const proxyExists = memberList.some(member => member.proxy === proxy);
if (proxyExists) {
throw new Error(enums.err.PROXY_EXISTS);
}
}).catch(e =>{throw e});
}).catch(e => {
throw e
});
}
}

View File

@@ -21,7 +21,7 @@ msgh.parseCommandArgs = function(content, commandName) {
const message = content.slice(msgh.prefix.length + commandName.length).trim();
return message.match(/\\?.|^$/g).reduce((accumulator, chara) => {
if (chara === '"') {
if (chara === '\"' || chara === '\'') {
// checks whether string is within quotes or not
accumulator.quote ^= 1;
} else if (!accumulator.quote && chara === ' '){
@@ -41,10 +41,10 @@ msgh.parseCommandArgs = function(content, commandName) {
* @param {string} authorId - The author of the message.
* @param {string} content - The full message content
* @param {string | null} attachmentUrl - The url for an attachment to the message, if any exists.
* @returns {Object} The proxy message object.
* @returns {{model, string, bool}} The proxy message object.
* @throws {Error} If a proxy message is sent with no message within it.
*/
msgh.parseProxyTags = async function (authorId, content, attachmentUrl= null){
msgh.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) {
@@ -57,14 +57,12 @@ msgh.parseProxyTags = async function (authorId, content, attachmentUrl= null){
const splitProxy = member.proxy.split("text");
if(content.startsWith(splitProxy[0]) && content.endsWith(splitProxy[1])) {
proxyMessage.member = member;
if (attachmentUrl) return proxyMessage.message = enums.misc.ATTACHMENT_SENT_BY;
proxyMessage.hasAttachment = !!attachmentUrl;
let escapedPrefix = splitProxy[0].replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
let escapedSuffix = splitProxy[1].replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
escapedPrefix = new RegExp("^" + escapedPrefix);
escapedSuffix = new RegExp(escapedSuffix + "$")
proxyMessage.message = content.replace(escapedPrefix, "").replace(escapedSuffix, "");
if (proxyMessage.message.length === 0) throw new Error(enums.err.NO_MESSAGE_SENT_WITH_PROXY);
}
}
})
@@ -72,24 +70,20 @@ msgh.parseProxyTags = async function (authorId, content, attachmentUrl= null){
}
/**
* Sends a message as an attachment if it's too long.NOT CURRENTLY IN USE
* Returns a text message that's too long as its text plus a file with the remaining text.
*
* @async
* @param {string} text - The text of the message.
* @param {Message} message - The message object.
* @throws {Error} If a proxy message is sent with no message within it.
* @returns {{text: string, file: Buffer<ArrayBuffer> | undefined}} The text and buffer object
*
*/
msgh.sendMessageAsAttachment = async function(text, message) {
msgh.returnBufferFromText = function (text) {
if (text.length > 2000) {
tmp.file(async (err, path, fd, cleanupCallback) => {
fs.writeFile(path, text, (err) => {
if (err) throw err;
})
if (err) throw err;
await message.reply({content: enums.err.IMPORT_ERROR, attachments: [path]});
});
const truncated = text.substring(0, 2000);
const restOfText = text.substring(2000);
const file = Buffer.from(restOfText, 'utf-8');
return {text: truncated, file: file}
}
return {text: text, file: undefined}
}
export const messageHelper = msgh;

View File

@@ -1,6 +1,5 @@
import {messageHelper} from "./messageHelper.js";
import {memberHelper} from "./memberHelper.js";
import {Webhook, Channel, Message, EmbedBuilder} from '@fluxerjs/core';
import {Webhook, Channel, Message, Client} from '@fluxerjs/core';
import {enums} from "../enums.js";
const wh = {};
@@ -9,6 +8,7 @@ const name = 'PluralFlux Proxy Webhook';
/**
* Replaces a proxied message with a webhook using the member information.
* @async
* @param {Client} client - The fluxer.js client.
* @param {Message} message - The full message object.
* @throws {Error} When the proxy message is not in a server.
@@ -17,71 +17,62 @@ wh.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).catch(e =>{throw e});
// If the message doesn't match a proxy, just return.
if (!proxyMatch.member) {
if (!proxyMatch || !proxyMatch.member || (proxyMatch.message.length === 0 && !proxyMatch.hasAttachment) ) {
return;
}
// If the message does match a proxy but is not in a guild server (ex: in the Bot's DMs
// If the message does match a proxy but is not in a guild server (ex: in the Bot's DMs)
if (!message.guildId) {
throw new Error(enums.err.NOT_IN_SERVER);
}
if (proxyMatch.message === enums.misc.ATTACHMENT_SENT_BY) {
return await message.reply(`${enums.misc.ATTACHMENT_SENT_BY} ${proxyMatch.member.displayname}`)
if (proxyMatch.hasAttachment) {
return await message.reply(`${enums.misc.ATTACHMENT_SENT_BY} ${proxyMatch.member.displayname ?? proxyMatch.member.name}`)
}
await replaceMessage(client, message, proxyMatch.message, proxyMatch.member).catch(e =>{throw e});
await wh.replaceMessage(client, message, proxyMatch.message, proxyMatch.member).catch(e =>{throw e});
}
/**
* Replaces a proxied message with a webhook using the member information.
* @async
* @param {Client} client - The fluxer.js client.
* @param {Message} message - The message to be deleted.
* @param {string} text - The text to send via the webhook.
* @param {model} member - A member object from the database.
* @throws {Error} When there's no message to send.
*/
async function replaceMessage(client, message, text, member) {
wh.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 getOrCreateWebhook(client, channel).catch((e) =>{throw e});
const webhook = await wh.getOrCreateWebhook(client, channel).catch((e) =>{throw e});
const username = member.displayname ?? member.name;
await webhook.send({content: text, username: username, avatar_url: member.propic});
if (text.length > 0) {
await webhook.send({content: text, username: username, avatar_url: member.propic}).catch(async(e) => {
const returnedBuffer = messageHelper.returnBufferFromText(text);
await webhook.send({content: returnedBuffer.text, username: username, avatar_url: member.propic, files: [{ name: 'text.txt', data: returnedBuffer.file }]
})
console.error(e);
});
}
if (message.attachments.size > 0) {
// Not implemented yet
}
await message.delete();
}
else {
throw new Error(enums.err.NO_MESSAGE_SENT_WITH_PROXY);
}
}
/**
* Creates attachment embeds for the webhook since right now sending images is not supported.
*
* @param {Object[]} attachments - The attachments.
* @returns {Object[]} A series of embeds.
*/
function createAttachmentEmbedsForWebhook(attachments) {
let embeds = [];
attachments.forEach(attachment => {
const embed = new EmbedBuilder()
.setTitle(attachment.filename)
.setImage(attachment.url).toJSON()
embeds.push(embed);
});
return embeds;
}
/**
* Gets or creates a webhook.
*
* @async
* @param {Client} client - The fluxer.js client.
* @param {Channel} channel - The channel the message was sent in.
* @returns {Webhook} A webhook object.
* @throws {Error} When no webhooks are allowed in the channel.
*/
async function getOrCreateWebhook(client, channel) {
wh.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 getWebhook(client, channel).catch((e) =>{throw e});
let webhook = await wh.getWebhook(client, channel).catch((e) =>{throw e});
if (!webhook) {
webhook = await channel.createWebhook({name: name});
}
@@ -90,12 +81,12 @@ async function getOrCreateWebhook(client, channel) {
/**
* Gets an existing webhook.
*
* @async
* @param {Client} client - The fluxer.js client.
* @param {Channel} channel - The channel the message was sent in.
* @returns {Webhook} A webhook object.
*/
async function getWebhook(client, channel) {
wh.getWebhook = async function(client, channel) {
const channelWebhooks = await channel?.fetchWebhooks() ?? [];
if (channelWebhooks.length === 0) {
return;

View File

@@ -0,0 +1,510 @@
const {EmbedBuilder} = require("@fluxerjs/core");
const {database} = require('../../src/database.js');
const {enums} = require('../../src/enums.js');
const {EmptyResultError, Op} = require('sequelize');
const {memberHelper} = require("../../src/helpers/memberHelper.js");
jest.mock('@fluxerjs/core', () => jest.fn());
jest.mock('../../src/database.js', () => {
return {
database: {
members: {
create: jest.fn().mockResolvedValue(),
update: jest.fn().mockResolvedValue(),
destroy: jest.fn().mockResolvedValue(),
}
}
}
});
jest.mock('sequelize', () => jest.fn());
describe('MemberHelper', () => {
const authorId = "0001";
const authorFull = "author#0001";
const attachmentUrl = "../oya.png";
const attachmentExpiration = new Date('2026-01-01T00.00.00.0000Z')
beforeEach(() => {
jest.resetModules();
jest.clearAllMocks();
})
describe('parseMemberCommand', () => {
beforeEach(() => {
jest.spyOn(memberHelper, 'getMemberInfo').mockResolvedValue("member info");
jest.spyOn(memberHelper, 'addNewMember').mockResolvedValue("new member");
jest.spyOn(memberHelper, 'removeMember').mockResolvedValue("remove member");
jest.spyOn(memberHelper, 'getAllMembersInfo').mockResolvedValue("all member info");
jest.spyOn(memberHelper, 'updateName').mockResolvedValue("update name");
jest.spyOn(memberHelper, 'updateDisplayName').mockResolvedValue("update display name");
jest.spyOn(memberHelper, 'updateProxy').mockResolvedValue("update proxy");
jest.spyOn(memberHelper, 'updatePropic').mockResolvedValue("update propic");
jest.spyOn(memberHelper, 'getProxyByMember').mockResolvedValue("get proxy");
});
test.each([
[['remove'], 'remove member', 'removeMember', ['remove']],
[['list'], 'all member info', 'getAllMembersInfo', authorFull],
[['somePerson', 'name'], 'update name', 'updateName', ['somePerson', 'name']],
[['somePerson', 'displayname'], 'update display name', 'updateDisplayName', ['somePerson', 'displayname']],
[['somePerson', 'proxy'], 'get proxy', 'getProxyByMember', 'somePerson'],
[['somePerson', 'proxy', 'test'], 'update proxy', 'updateProxy', ['somePerson', 'proxy', 'test']],
[['somePerson'], 'member info', 'getMemberInfo', 'somePerson'],
])('%s calls %s and returns correct values', async (args, expectedResult, method, passedIn) => {
// Act
return memberHelper.parseMemberCommand(authorId, authorFull, args).then((result) => {
// Assert
expect(result).toEqual(expectedResult);
expect(memberHelper[method]).toHaveBeenCalledTimes(1);
expect(memberHelper[method]).toHaveBeenCalledWith(authorId, passedIn)
});
});
test.each([
[['new'], attachmentUrl],
[['new'], null,]
])('%s returns correct values and calls addNewMember', (args, attachmentUrl) => {
// Act
return memberHelper.parseMemberCommand(authorId, authorFull, args, attachmentUrl).then((result) => {
// Assert
expect(result).toEqual("new member");
expect(memberHelper.addNewMember).toHaveBeenCalledTimes(1);
expect(memberHelper.addNewMember).toHaveBeenCalledWith(authorId, args, attachmentUrl);
});
})
test('["somePerson", "propic"] returns correct values and updatePropic', () => {
// Arrange
const args = ['somePerson', 'propic'];
// Act
return memberHelper.parseMemberCommand(authorId, authorFull, args, attachmentUrl, attachmentExpiration).then((result) => {
// Assert
expect(result).toEqual("update propic");
expect(memberHelper['updatePropic']).toHaveBeenCalledTimes(1);
expect(memberHelper['updatePropic']).toHaveBeenCalledWith(authorId, args, attachmentUrl, attachmentExpiration)
});
})
test.each([
[['--help'], enums.help.MEMBER],
[['name'], enums.help.NAME],
[['displayname'], enums.help.DISPLAY_NAME],
[['proxy'], enums.help.PROXY],
[['propic'], enums.help.PROPIC],
[['list', '--help'], enums.help.LIST],
[[''], enums.help.MEMBER],
])('%s returns correct enums', async (args, expectedResult) => {
// Arrange
const authorId = '1';
const authorFull = 'somePerson#0001';
// Act
return memberHelper.parseMemberCommand(authorId, authorFull, args).then((result) => {
expect(result).toEqual(expectedResult);
});
});
describe('errors', () => {
beforeEach(() => {
jest.resetModules();
jest.clearAllMocks();
jest.spyOn(memberHelper, 'getMemberInfo').mockImplementation(() => { throw new Error('member info error')});
jest.spyOn(memberHelper, 'addNewMember').mockImplementation(() => { throw new Error('new member error')});
jest.spyOn(memberHelper, 'removeMember').mockImplementation(() => { throw new Error('remove member error')});
jest.spyOn(memberHelper, 'getAllMembersInfo').mockImplementation(() => { throw new Error('all member info error')});
jest.spyOn(memberHelper, 'updateName').mockImplementation(() => { throw new Error('update name error')});
jest.spyOn(memberHelper, 'updateDisplayName').mockImplementation(() => { throw new Error('update display name error')});
jest.spyOn(memberHelper, 'updateProxy').mockImplementation(() => { throw new Error('update proxy error')});
jest.spyOn(memberHelper, 'updatePropic').mockImplementation(() => { throw new Error('update propic error')});
jest.spyOn(memberHelper, 'getProxyByMember').mockImplementation(() => { throw new Error('get proxy error')});
})
test.each([
[['remove'], 'remove member error', 'removeMember', ['remove']],
[['list'], 'all member info error', 'getAllMembersInfo', authorFull],
[['somePerson', 'name'], 'update name error', 'updateName', ['somePerson', 'name']],
[['somePerson', 'displayname'], 'update display name error', 'updateDisplayName', ['somePerson', 'displayname']],
[['somePerson', 'proxy'], 'get proxy error', 'getProxyByMember', 'somePerson'],
[['somePerson', 'proxy', 'test'], 'update proxy error', 'updateProxy', ['somePerson', 'proxy', 'test']],
[['somePerson'], 'member info error', 'getMemberInfo', 'somePerson'],
])('%s calls methods and throws correct values', async (args, expectedError, method, passedIn) => {
// Act
return memberHelper.parseMemberCommand(authorId, authorFull, args).catch((result) => {
// Assert
expect(result).toEqual(new Error(expectedError));
expect(memberHelper[method]).toHaveBeenCalledTimes(1);
expect(memberHelper[method]).toHaveBeenCalledWith(authorId, passedIn)
});
});
test.each([
[['new'], attachmentUrl],
[['new'], null,]
])('%s throws correct error when addNewMember returns error', (args, attachmentUrl) => {
// Act
return memberHelper.parseMemberCommand(authorId, authorFull, args, attachmentUrl).catch((result) => {
// Assert
expect(result).toEqual(new Error("new member error"));
expect(memberHelper.addNewMember).toHaveBeenCalledTimes(1);
expect(memberHelper.addNewMember).toHaveBeenCalledWith(authorId, args, attachmentUrl);
});
})
test('["somePerson", "propic"] throws correct error when updatePropic returns error', () => {
// Arrange
const args = ['somePerson', 'propic'];
// Act
return memberHelper.parseMemberCommand(authorId, authorFull, args, attachmentUrl, attachmentExpiration).catch((result) => {
// Assert
expect(result).toEqual(new Error("update propic error"));
expect(memberHelper['updatePropic']).toHaveBeenCalledTimes(1);
expect(memberHelper['updatePropic']).toHaveBeenCalledWith(authorId, args, attachmentUrl, attachmentExpiration)
});
})
})
})
describe('addNewMember', () => {
test('returns help if --help passed in', async() => {
// Arrange
const args = ['new', '--help'];
const expected = enums.help.NEW;
//Act
return memberHelper.addNewMember(authorId, args).then((result) => {
// Assert
expect(result).toEqual(expected);
})
})
test('calls getMemberInfo when successful and returns result', async () => {
// Arrange
const args = ['new', 'some person'];
const memberObject = { name: args[1] }
jest.spyOn(memberHelper, 'addFullMember').mockResolvedValue(memberObject);
jest.spyOn(memberHelper, 'getMemberInfo').mockResolvedValue(memberObject);
//Act
return memberHelper.addNewMember(authorId, args).then((result) => {
// Assert
expect(result).toEqual(memberObject);
expect(memberHelper.getMemberInfo).toHaveBeenCalledTimes(1);
expect(memberHelper.getMemberInfo).toHaveBeenCalledWith(authorId, args[1]);
})
})
test('throws expected error when getMemberInfo throws error', async () => {
// Arrange
const args = ['new', 'some person'];
const memberObject = { name: args[1] }
jest.spyOn(memberHelper, 'addFullMember').mockResolvedValue(memberObject);
jest.spyOn(memberHelper, 'getMemberInfo').mockImplementation(() => { throw new Error('getMemberInfo error') });
//Act
return memberHelper.addNewMember(authorId, args).catch((result) => {
// Assert
expect(result).toEqual(new Error('getMemberInfo error'));
})
})
test('throws expected error when addFullMember throws error', async () => {
// Arrange
const args = ['new', 'somePerson'];
const expected = 'add full member error';
jest.spyOn(memberHelper, 'addFullMember').mockImplementation(() => { throw new Error(expected)});
//Act
return memberHelper.addNewMember(authorId, args).catch((result) => {
// Assert
expect(result).toEqual(new Error(expected));
})
})
})
describe('updateName', () => {
test('sends help message when --help parameter passed in', async () => {
// Arrange
const args = ['somePerson', 'name', '--help'];
// Act
return memberHelper.updateName(authorId, args).then((result) => {
// Assert
expect(result).toEqual(enums.help.NAME);
})
})
test('Sends string when no name', async () => {
// Arrange
const args = ['somePerson', 'name'];
const expected = `The name for ${args[0]} is ${args[0]}, but you probably knew that!`;
// Act
return memberHelper.updateName(authorId, args).then((result) => {
expect(result).toEqual(expected);
})
})
test('throws error when name is empty', async () => {
// Arrange
const args = ['somePerson', 'name', " "];
// Act
return memberHelper.updateName(authorId, args).catch((result) => {
// Assert
expect(result).toEqual(new RangeError("Name " + enums.err.NO_VALUE));
})
})
test('throws error when updateMemberField returns error', async () => {
// Arrange
const expected = 'update error';
const args = ['somePerson', "name", "someNewPerson"];
jest.spyOn(memberHelper, 'updateMemberField').mockImplementation(() => {
throw new Error(expected)
});
// Act
return memberHelper.updateName(authorId, args).catch((result) => {
// Assert
expect(result).toEqual(new Error(expected));
})
});
test('sends string when updateMemberField returns successfully', async () => {
// Arrange
const args = ['somePerson', 'name', 'someNewPerson'];
jest.spyOn(memberHelper, 'updateMemberField').mockResolvedValue("Updated");
// Act
return memberHelper.updateName(authorId, args).then((result) => {
// Assert
expect(result).toEqual("Updated");
})
})
})
describe('updateDisplayName', () => {
test('sends help message when --help parameter passed in', async () => {
// Arrange
const args = ['somePerson', 'displayname', '--help'];
jest.spyOn(memberHelper, 'updateMemberField').mockResolvedValue();
// Act
return memberHelper.updateDisplayName(authorId, args).then((result) => {
// Assert
expect(result).toEqual(enums.help.DISPLAY_NAME);
expect(memberHelper.updateMemberField).not.toHaveBeenCalled();
})
})
test('Sends string of current displayname when it exists and no displayname passed in', async () => {
// Arrange
const args = ['somePerson', 'displayname'];
const displayname = "Some Person";
const member = {
displayname: displayname,
}
jest.spyOn(memberHelper, 'getMemberByName').mockResolvedValue(member);
jest.spyOn(memberHelper, 'updateMemberField').mockResolvedValue();
// Act
return memberHelper.updateDisplayName(authorId, args).then((result) => {
// Assert
expect(result).toEqual(`Display name for ${args[0]} is: "${member.displayname}".`);
expect(memberHelper.updateMemberField).not.toHaveBeenCalled();
})
})
test('Sends error when no displayname passed in', async () => {
// Arrange
const args = ['somePerson', 'displayname'];
const member = {}
jest.spyOn(memberHelper, 'getMemberByName').mockResolvedValue(member);
jest.spyOn(memberHelper, 'updateMemberField').mockResolvedValue();
// Act
return memberHelper.updateDisplayName(authorId, args).catch((result) => {
// Assert
expect(result).toEqual(new Error(`Display name ${enums.err.NO_VALUE}`));
expect(memberHelper.updateMemberField).not.toHaveBeenCalled();
})
})
test('Sends error when display name is too long', async () => {
// Arrange
const displayname = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
const args = ['somePerson', 'displayname', displayname];
const member = {};
jest.spyOn(memberHelper, 'getMemberByName').mockResolvedValue(member);
jest.spyOn(memberHelper, 'updateMemberField').mockResolvedValue();
// Act
return memberHelper.updateDisplayName(authorId, args).catch((result) => {
// Assert
expect(result).toEqual(new RangeError(enums.err.DISPLAY_NAME_TOO_LONG));
expect(memberHelper.updateMemberField).not.toHaveBeenCalled();
})
})
test('Sends error when display name is blank', async () => {
// Arrange
const displayname = " ";
const args = ['somePerson', 'displayname', displayname];
const member = {};
jest.spyOn(memberHelper, 'getMemberByName').mockResolvedValue(member);
jest.spyOn(memberHelper, 'updateMemberField').mockResolvedValue();
// Act
return memberHelper.updateDisplayName(authorId, args).catch((result) => {
// Assert
expect(result).toEqual(new Error(`Display name ${enums.err.NO_VALUE}`));
expect(memberHelper.updateMemberField).not.toHaveBeenCalled();
})
})
test('call updateMemberField with correct arguments when displayname passed in correctly', async() => {
// Arrange
const args = ['somePerson', 'displayname', "Some Person"];
const member = {};
jest.spyOn(memberHelper, 'updateMemberField').mockResolvedValue(member);
// Act
return memberHelper.updateDisplayName(authorId, args).then((result) => {
// Assert
expect(memberHelper.updateMemberField).toHaveBeenCalledWith(authorId, args);
expect(memberHelper.updateMemberField).toHaveBeenCalledTimes(1);
})
})
})
describe('addFullMember', () => {
const memberName = "somePerson";
const displayName = "Some Person";
const proxy = "--text";
const propic = "oya.png";
beforeEach(() => {
database.members.create = jest.fn().mockResolvedValue();
jest.spyOn(memberHelper, 'getMemberByName').mockResolvedValue();
})
test('calls getMemberByName', async() => {
// Act
return await memberHelper.addFullMember(authorId, memberName).then(() => {
// Assert
expect(memberHelper.getMemberByName).toHaveBeenCalledWith(authorId, memberName);
expect(memberHelper.getMemberByName).toHaveBeenCalledTimes(1);
})
})
test('if getMemberByName returns member, throw error', async() => {
memberHelper.getMemberByName.mockResolvedValue({name: memberName});
// Act
return await memberHelper.addFullMember(authorId, memberName).catch((e) => {
// Assert
expect(e).toEqual(new Error(`Can't add ${memberName}. ${enums.err.MEMBER_EXISTS}`))
expect(database.members.create).not.toHaveBeenCalled();
})
})
test('if displayname is over 32 characters, call database.member.create with null value', async() => {
// Arrange
const displayName = "Some person with a very very very long name that can't be processed";
const expectedMemberArgs = {name: memberName, userid: authorId, displayname: null, proxy: null, propic: null}
database.members.create = jest.fn().mockResolvedValue(expectedMemberArgs);
const expectedReturn = {member: expectedMemberArgs, errors: [`Tried to set displayname to \"${displayName}\". ${enums.err.DISPLAY_NAME_TOO_LONG}. ${enums.err.SET_TO_NULL}`]}
// Act
return await memberHelper.addFullMember(authorId, memberName, displayName, null, null).then((res) => {
// Assert
expect(res).toEqual(expectedReturn);
expect(database.members.create).toHaveBeenCalledWith(expectedMemberArgs);
expect(database.members.create).toHaveBeenCalledTimes(1);
})
})
test('if proxy, call checkIfProxyExists', async() => {
// Arrange
jest.spyOn(memberHelper, 'checkIfProxyExists').mockResolvedValue();
const expectedMemberArgs = {name: memberName, userid: authorId, displayname: null, proxy: proxy, propic: null}
database.members.create = jest.fn().mockResolvedValue(expectedMemberArgs);
const expectedReturn = {member: expectedMemberArgs, errors: []}
// Act
return await memberHelper.addFullMember(authorId, memberName, null, proxy).then((res) => {
// Assert
expect(res).toEqual(expectedReturn);
expect(memberHelper.checkIfProxyExists).toHaveBeenCalledWith(authorId, proxy);
expect(memberHelper.checkIfProxyExists).toHaveBeenCalledTimes(1);
expect(database.members.create).toHaveBeenCalledWith(expectedMemberArgs);
expect(database.members.create).toHaveBeenCalledTimes(1);
})
})
test('if checkProxyExists throws error, call database.member.create with null value', async() => {
// Arrange
jest.spyOn(memberHelper, 'checkIfProxyExists').mockImplementation(() => {throw new Error('error')});
const expectedMemberArgs = {name: memberName, userid: authorId, displayname: null, proxy: null, propic: null}
database.members.create = jest.fn().mockResolvedValue(expectedMemberArgs);
const expectedReturn = {member: expectedMemberArgs, errors: [`Tried to set proxy to \"${proxy}\". error. ${enums.err.SET_TO_NULL}`]}
// Act
return await memberHelper.addFullMember(authorId, memberName, null, proxy, null).then((res) => {
// Assert
expect(res).toEqual(expectedReturn);
expect(database.members.create).toHaveBeenCalledWith(expectedMemberArgs);
expect(database.members.create).toHaveBeenCalledTimes(1);
})
})
test('if propic, call checkImageFormatValidity', async() => {
// Arrange
jest.spyOn(memberHelper, 'checkImageFormatValidity').mockResolvedValue();
const expectedMemberArgs = {name: memberName, userid: authorId, displayname: null, proxy: null, propic: propic}
database.members.create = jest.fn().mockResolvedValue(expectedMemberArgs);
const expectedReturn = {member: expectedMemberArgs, errors: []}
// Act
return await memberHelper.addFullMember(authorId, memberName, null, null, propic).then((res) => {
// Assert
expect(res).toEqual(expectedReturn);
expect(memberHelper.checkImageFormatValidity).toHaveBeenCalledWith(propic);
expect(memberHelper.checkImageFormatValidity).toHaveBeenCalledTimes(1);
expect(database.members.create).toHaveBeenCalledWith(expectedMemberArgs);
expect(database.members.create).toHaveBeenCalledTimes(1);
})
})
test('if checkImageFormatValidity throws error, call database.member.create with null value', async() => {
// Arrange
jest.spyOn(memberHelper, 'checkImageFormatValidity').mockImplementation(() => {throw new Error('error')});
const expectedMemberArgs = {name: memberName, userid: authorId, displayname: null, proxy: null, propic: null}
database.members.create = jest.fn().mockResolvedValue(expectedMemberArgs);
const expectedReturn = {member: expectedMemberArgs, errors: [`Tried to set profile picture to \"${propic}\". error. ${enums.err.SET_TO_NULL}`]}
// Act
return await memberHelper.addFullMember(authorId, memberName, null, null, propic).then((res) => {
// Assert
expect(res).toEqual(expectedReturn);
expect(database.members.create).toHaveBeenCalledWith(expectedMemberArgs);
expect(database.members.create).toHaveBeenCalledTimes(1);
})
})
test('if all values are valid, call database.members.create', async() => {
// Arrange
jest.spyOn(memberHelper, 'checkIfProxyExists').mockResolvedValue();
jest.spyOn(memberHelper, 'checkImageFormatValidity').mockResolvedValue();
const expectedMemberArgs = {name: memberName, userid: authorId, displayname: displayName, proxy: proxy, propic: propic}
database.members.create = jest.fn().mockResolvedValue(expectedMemberArgs);
const expectedReturn = {member: expectedMemberArgs, errors: []}
// Act
// Act
return await memberHelper.addFullMember(authorId, memberName, displayName, proxy, propic).then((res) => {
// Assert
expect(res).toEqual(expectedReturn);
expect(database.members.create).toHaveBeenCalledWith(expectedMemberArgs);
expect(database.members.create).toHaveBeenCalledTimes(1);
})
})
})
afterEach(() => {
// restore the spy created with spyOn
jest.restoreAllMocks();
});
})

View File

@@ -0,0 +1,128 @@
const env = require('dotenv');
env.config();
const {memberHelper} = require("../../src/helpers/memberHelper.js");
const {Message} = require("@fluxerjs/core");
const {fs} = require('fs');
const {enums} = require('../../src/enums');
const {tmp, setGracefulCleanup} = require('tmp');
jest.mock('../../src/helpers/memberHelper.js', () => {
return {memberHelper: {
getMembersByAuthor: jest.fn()
}}
})
jest.mock('tmp');
jest.mock('fs');
jest.mock('@fluxerjs/core');
const {messageHelper} = require("../../src/helpers/messageHelper.js");
describe('messageHelper', () => {
beforeEach(() => {
jest.resetModules();
jest.clearAllMocks();
})
describe('parseCommandArgs', () => {
test.each([
['pk;member', ['']],
['pk;member add somePerson "Some Person"', ['add', 'somePerson', 'Some Person']],
['pk;member add \"Some Person\"', ['add', 'Some Person']],
['pk;member add somePerson \'Some Person\'', ['add', 'somePerson', 'Some Person']],
['pk;member add somePerson \"\'Some\' Person\"', ['add', 'somePerson', 'Some Person']],
])('%s returns correct arguments', (content, expected) => {
// Arrange
const command = "member";
const result = messageHelper.parseCommandArgs(content, command);
expect(result).toEqual(expected);
})
})
describe(`parseProxyTags`, () => {
const membersFor1 = [
{name: "somePerson", proxy: "--text"},
{name: "someSecondPerson", proxy: undefined},
{name: "someOtherPerson", proxy: "?text}"},
{name: "someLastPerson", proxy: "{text}"},
{name: "someEmojiPerson", proxy: "⭐text"},
{name: "someSpacePerson", proxy: "! text"},
]
const membersFor2 = []
const membersFor3 = [
{name: "someOtherThirdPerson", proxy: undefined}
]
const attachmentUrl = "../oya.png"
beforeEach(() => {
memberHelper.getMembersByAuthor = jest.fn().mockImplementation((specificAuthorId) => {
if (specificAuthorId === "1") return membersFor1;
if (specificAuthorId === "2") return membersFor2;
if (specificAuthorId === "3") return membersFor3;
})
});
test.each([
['1', 'hello', null, {}],
['1', '--hello', null, {member: membersFor1[0], message: 'hello', hasAttachment: false}],
['1', 'hello', attachmentUrl, {}],
['1', '--hello', attachmentUrl, {member: membersFor1[0], message: 'hello', hasAttachment: true}],
['1', '--', attachmentUrl, {member: membersFor1[0], message: '', hasAttachment: true}],
['1', '?hello}', null, {member: membersFor1[2], message: 'hello', hasAttachment: false}],
['1', '{hello}', null, {member: membersFor1[3], message: 'hello', hasAttachment: false}],
['1', '⭐hello', null, {member: membersFor1[4], message: 'hello', hasAttachment: false}],
['1', '! hello', null, {member: membersFor1[5], message: 'hello', hasAttachment: false}],
['2', 'hello', null, undefined],
['2', '--hello', null, undefined],
['2', 'hello', attachmentUrl, undefined],
['2', '--hello', attachmentUrl,undefined],
['3', 'hello', null, {}],
['3', '--hello', null, {}],
['3', 'hello', attachmentUrl, {}],
['3', '--hello', attachmentUrl,{}],
])('ID %s with string %s returns correct proxy', async(specificAuthorId, content, attachmentUrl, expected) => {
// Act
return messageHelper.parseProxyTags(specificAuthorId, content, attachmentUrl).then((res) => {
// Assert
expect(res).toEqual(expected);
})
});
})
describe('returnBufferFromText', () => {
const charas2000 = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
test('returns truncated text and buffer file when text is more than 2000 characters', () => {
// Arrange
const charasOver2000 = "bbbbb"
const expectedBuffer = Buffer.from(charasOver2000, 'utf-8');
const expected = {text: charas2000, file: expectedBuffer};
// Act
const result = messageHelper.returnBufferFromText(`${charas2000}${charasOver2000}`);
// Assert
expect(result).toEqual(expected);
})
test('returns text when text is 2000 characters or less', () => {
// Arrange
const expected = {text: charas2000, file: undefined};
// Act
const result = messageHelper.returnBufferFromText(`${charas2000}`);
// Assert
expect(result).toEqual(expected);
})
})
afterEach(() => {
// restore the spy created with spyOn
jest.restoreAllMocks();
});
})

View File

@@ -0,0 +1,269 @@
jest.mock('../../src/helpers/messageHelper.js')
const {messageHelper} = require("../../src/helpers/messageHelper.js");
jest.mock('../../src/helpers/messageHelper.js', () => {
return {messageHelper: {
parseProxyTags: jest.fn(),
returnBuffer: jest.fn(),
returnBufferFromText: jest.fn(),
}}
})
const {webhookHelper} = require("../../src/helpers/webhookHelper.js");
const {enums} = require("../../src/enums");
describe('webhookHelper', () => {
beforeEach(() => {
jest.resetModules();
jest.clearAllMocks();
})
describe(`sendMessageAsMember`, () => {
const client = {};
const content = "hi"
const attachments = {
size: 0,
first: () => {}
}
const message = {
client,
content: content,
attachments: attachments,
author: {
id: '123'
},
guild: {
guildId: '123'
},
reply: jest.fn()
}
const member = {proxy: "--text", name: 'somePerson', displayname: "Some Person"};
const proxyMessage = {message: content, member: member}
beforeEach(() => {
jest.spyOn(webhookHelper, 'replaceMessage');
})
test('calls parseProxyTags and returns if proxyMatch is empty object', async() => {
// Arrange
messageHelper.parseProxyTags.mockResolvedValue({});
// Act
return webhookHelper.sendMessageAsMember(client, message).then((res) => {
expect(res).toBeUndefined();
expect(messageHelper.parseProxyTags).toHaveBeenCalledTimes(1);
expect(messageHelper.parseProxyTags).toHaveBeenCalledWith(message.author.id, content, null);
expect(webhookHelper.replaceMessage).not.toHaveBeenCalled();
})
})
test('calls parseProxyTags and returns if proxyMatch is undefined', async() => {
// Arrange
messageHelper.parseProxyTags.mockResolvedValue(undefined);
// Act
return webhookHelper.sendMessageAsMember(client, message).then((res) => {
// Assert
expect(res).toBeUndefined();
expect(messageHelper.parseProxyTags).toHaveBeenCalledTimes(1);
expect(messageHelper.parseProxyTags).toHaveBeenCalledWith(message.author.id, content, null);
expect(webhookHelper.replaceMessage).not.toHaveBeenCalled();
})
})
test('calls parseProxyTags with attachmentUrl', async() => {
// Arrange
message.attachments = {
size: 1,
first: () => {
return {url: 'oya.png'}
}
}
// message.attachments.set('attachment', {url: 'oya.png'})
// message.attachments.set('first', () => {return {url: 'oya.png'}})
messageHelper.parseProxyTags.mockResolvedValue(undefined);
// Act
return webhookHelper.sendMessageAsMember(client, message).then((res) => {
// Assert
expect(res).toBeUndefined();
expect(messageHelper.parseProxyTags).toHaveBeenCalledTimes(1);
expect(messageHelper.parseProxyTags).toHaveBeenCalledWith(message.author.id, content, 'oya.png');
})
})
test('if message matches member proxy but is not sent from a guild, throw an error', async() => {
// Arrange
messageHelper.parseProxyTags.mockResolvedValue(proxyMessage);
// Act
return webhookHelper.sendMessageAsMember(client, message).catch((res) => {
// Assert
expect(res).toEqual(new Error(enums.err.NOT_IN_SERVER));
expect(webhookHelper.replaceMessage).not.toHaveBeenCalled();
})
})
test('if message matches member proxy and sent in a guild and has an attachment, reply to message with ping', async() => {
// Arrange
message.guildId = '123'
proxyMessage.hasAttachment = true;
messageHelper.parseProxyTags.mockResolvedValue(proxyMessage);
const expected = `${enums.misc.ATTACHMENT_SENT_BY} ${proxyMessage.member.displayname}`
// Act
return webhookHelper.sendMessageAsMember(client, message).then((res) => {
// Assert
expect(message.reply).toHaveBeenCalledTimes(1);
expect(message.reply).toHaveBeenCalledWith(expected);
expect(webhookHelper.replaceMessage).not.toHaveBeenCalled();
})
})
test('if message matches member proxy and sent in a guild channel and no attachment, calls replace message', async() => {
// Arrange
message.guildId = '123';
proxyMessage.hasAttachment = false;
messageHelper.parseProxyTags.mockResolvedValue(proxyMessage);
jest.spyOn(webhookHelper, 'replaceMessage').mockResolvedValue();
// Act
return webhookHelper.sendMessageAsMember(client, message).then((res) => {
// Assert
expect(message.reply).not.toHaveBeenCalled();
expect(webhookHelper.replaceMessage).toHaveBeenCalledTimes(1);
expect(webhookHelper.replaceMessage).toHaveBeenCalledWith(client, message, proxyMessage.message, proxyMessage.member);
})
})
test('if replace message throws error, throw same error', async() => {
// Arrange
message.guildId = '123';
messageHelper.parseProxyTags.mockResolvedValue(proxyMessage);
jest.spyOn(webhookHelper, 'replaceMessage').mockImplementation(() => {throw new Error("error")});
// Act
return webhookHelper.sendMessageAsMember(client, message).catch((res) => {
// Assert
expect(message.reply).not.toHaveBeenCalled();
expect(webhookHelper.replaceMessage).toHaveBeenCalledTimes(1);
expect(webhookHelper.replaceMessage).toHaveBeenCalledWith(client, message, proxyMessage.message, proxyMessage.member);
expect(res).toEqual(new Error('error'));
})
})
})
describe(`replaceMessage`, () => {
const channelId = '123';
const authorId = '456';
const guildId = '789';
const text = "hello";
const client = {
channels: {
get: jest.fn().mockReturnValue(channelId)
}
}
const member = {proxy: "--text", name: 'somePerson', displayname: "Some Person", propic: 'oya.png'};
const attachments= {
size: 1,
first: () => {return channelId;}
};
const message = {
client,
channelId: channelId,
content: text,
attachments: attachments,
author: {
id: authorId
},
guild: {
guildId: guildId
},
reply: jest.fn(),
delete: jest.fn()
}
const webhook = {
send: async() => jest.fn().mockResolvedValue()
}
test('does not call anything if text is 0 or message has no attachments', async() => {
// Arrange
const emptyText = ''
const noAttachments = {
size: 0,
first: () => {}
}
message.attachments = noAttachments;
jest.spyOn(webhookHelper, 'getOrCreateWebhook').mockResolvedValue(webhook);
// Act
return webhookHelper.replaceMessage(client, message, emptyText, member).then(() => {
expect(webhookHelper.getOrCreateWebhook).not.toHaveBeenCalled();
expect(message.delete).not.toHaveBeenCalled();
})
})
test('calls getOrCreateWebhook and message.delete with correct arguments if text >= 0', async() => {
// Arrange
message.attachments = {
size: 0,
first: () => {
}
};
jest.spyOn(webhookHelper, 'getOrCreateWebhook').mockResolvedValue(webhook);
// Act
return webhookHelper.replaceMessage(client, message, text, member).then((res) => {
// Assert
expect(webhookHelper.getOrCreateWebhook).toHaveBeenCalledTimes(1);
expect(webhookHelper.getOrCreateWebhook).toHaveBeenCalledWith(client, channelId);
expect(message.delete).toHaveBeenCalledTimes(1);
expect(message.delete).toHaveBeenCalledWith();
})
})
// TODO: flaky for some reason
test('calls getOrCreateWebhook and message.delete with correct arguments if attachments exist', async() => {
// Arrange
const emptyText = ''
jest.spyOn(webhookHelper, 'getOrCreateWebhook').mockResolvedValue(webhook);
// Act
return webhookHelper.replaceMessage(client, message, emptyText, member).then((res) => {
// Assert
expect(webhookHelper.getOrCreateWebhook).toHaveBeenCalledTimes(1);
expect(webhookHelper.getOrCreateWebhook).toHaveBeenCalledWith(client, channelId);
expect(message.delete).toHaveBeenCalledTimes(1);
expect(message.delete).toHaveBeenCalledWith();
})
})
test('calls returnBufferFromText and console error if webhook.send returns error', async() => {
// Arrange
const file = Buffer.from(text, 'utf-8');
const returnedBuffer = {text: text, file: file};
const expected2ndSend = {content: returnedBuffer.text, username: member.displayname, avatar_url: member.propic, files: [{name: 'text.txt', data: returnedBuffer.file}]};
jest.mock('console', () => ({error: jest.fn()}));
jest.spyOn(webhookHelper, 'getOrCreateWebhook').mockResolvedValue(webhook);
webhook.send = jest.fn().mockImplementationOnce(async() => {throw new Error('error')});
messageHelper.returnBufferFromText = jest.fn().mockResolvedValue(returnedBuffer);
// Act
return webhookHelper.replaceMessage(client, message, text, member).catch((res) => {
// Assert
expect(messageHelper.returnBufferFromText).toHaveBeenCalledTimes(1);
expect(messageHelper.returnBufferFromText).toHaveBeenCalledWith(text);
expect(webhook.send).toHaveBeenCalledTimes(2);
expect(webhook.send).toHaveBeenNthCalledWith(2, expected2ndSend);
expect(console.error).toHaveBeenCalledTimes(1);
expect(console.error).toHaveBeenCalledWith(new Error('error'));
})
})
})
describe(`getOrCreateWebhook`, () => {
})
describe(`getWebhook`, () => {
})
afterEach(() => {
// restore the spy created with spyOn
jest.restoreAllMocks();
});
})