diff --git a/enums.js b/enums.js index 850deaa..baa81eb 100644 --- a/enums.js +++ b/enums.js @@ -9,10 +9,13 @@ helperEnums.err = { 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.", 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" + NO_SUCH_COMMAND: "No such command exists.", + PROPIC_FAILS_REQUIREMENTS: "Profile picture must be in JPG or PNG format.", + PROPIC_CANNOT_LOAD: "Profile picture could not be loaded from URL." } helperEnums.help = { + PROPIC: "test", 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.\n\nThe current commands are: `pf;member` and `pf;help`.", MEMBER: "You can shorten this command to `pf;m`. The available subcommands for `pf;member` are `add`, `remove`, `displayname`, and `proxy`. Add ` --help` to the end of a subcommand to find out more about it, or just send it without arguments.", ADD: "Creates a new member to proxy with, for example: `pf;member jane`. The member name should ideally be short so you can write other commands with it. \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.", diff --git a/helpers/memberHelper.js b/helpers/memberHelper.js index cba6910..a649ede 100644 --- a/helpers/memberHelper.js +++ b/helpers/memberHelper.js @@ -1,5 +1,6 @@ import { db } from '../sequelize.js'; import {enums} from "../enums.js"; +import { loadImage, Image } from "canvas"; const mh = {}; @@ -11,16 +12,17 @@ const commandList = ['--help', 'add', 'remove', 'displayName', 'proxy', '']; * * @param {string} authorId - The author of the message * @param {string[]} args - The message arguments - * @returns {string} A message. + * @param {string} attachment - The message attachments + * @returns {Promise} A message. */ -mh.parseMemberCommand = async function(authorId, args){ +mh.parseMemberCommand = async function(authorId, args, attachment){ console.log(authorId, args); let member; // checks whether command is in list, otherwise assumes it's a name if(!commandList.includes(args[0])) { member = await getMemberInfo(authorId, args[0]); - if (!member) { - return enums.err.NO_SUCH_COMMAND; + if (member === enums.err.NO_MEMBER) { + return member; } } switch(args[0]) { @@ -44,8 +46,8 @@ mh.parseMemberCommand = async function(authorId, args){ return await updateDisplayName(authorId, args); case 'proxy': return await updateProxy(authorId, args); - // case 'avatar': - // return await set_avatar(authorId, args) + case 'propic': + return await updatePropic(authorId, args, attachment) default: return member; } @@ -56,7 +58,7 @@ mh.parseMemberCommand = async function(authorId, args){ * * @param {string} authorId - The author of the message * @param {string[]} args - The message arguments - * @returns {string} A successful addition, or an error message. + * @returns {Promise} A successful addition, or an error message. */ async function addNewMember(authorId, args) { if (args[1] && args[1] === "--help" || !args[1]) { @@ -88,7 +90,7 @@ async function addNewMember(authorId, args) { * * @param {string} authorId - The author of the message * @param {string[]} args - The message arguments - * @returns {string} A successful update, or an error message. + * @returns {Promise} A successful update, or an error message. */ async function updateDisplayName(authorId, args) { if (args[1] && args[1] === "--help" || !args[1]) { @@ -118,7 +120,7 @@ async function updateDisplayName(authorId, args) { * * @param {string} authorId - The author of the message * @param {string[]} args - The message arguments - * @returns {string} A successful update, or an error message. + * @returns {Promise } A successful update, or an error message. */ async function updateProxy(authorId, args) { if (args[1] && args[1] === "--help" || !args[1]) { @@ -128,7 +130,7 @@ async function updateProxy(authorId, args) { const trimmedProxy = proxy ? proxy.replaceAll(' ', '') : null; if (trimmedProxy == null) { - return; + return `Proxy ${enums.err.NO_VALUE}`; } const members = await mh.getMembersByAuthor(authorId); @@ -139,12 +141,35 @@ async function updateProxy(authorId, args) { return updateMember(authorId, args); } +async function updatePropic(authorId, args, attachment) { + if (args[1] && args[1] === "--help") { + return enums.help.PROPIC; + } + let img; + const updatedArgs = args; + if (!updatedArgs[1] && !attachment) { + return enums.help.PROPIC; + } else if (attachment) { + updatedArgs[2] = attachment.url; + } + if (updatedArgs[2]) { + img = updatedArgs[2]; + } + + return await loadImage(img).then(() => { + return updateMember(authorId, updatedArgs); + }).catch((err) => { + return `${enums.err.PROPIC_CANNOT_LOAD}: ${err.message}`; + }); + +} + /** * Removes a member. * * @param {string} authorId - The author of the message * @param {string[]} args - The message arguments - * @returns {string} A successful removal, or an error message. + * @returns {Promise} A successful removal, or an error message. */ async function removeMember(authorId, args) { if (args[1] && args[1] === "--help") { @@ -162,14 +187,14 @@ async function removeMember(authorId, args) { }); } -/* non-commands */ +/*======Non-Subcommands======*/ /** * Updates a member's fields in the database. * * @param {string} authorId - The author of the message * @param {string[]} args - The message arguments - * @returns {string} A successful update, or an error message. + * @returns {Promise} A successful update, or an error message. */ async function updateMember(authorId, args) { const memberName = args[0]; @@ -187,7 +212,7 @@ async function updateMember(authorId, args) { * * @param {string} authorId - The author of the message * @param {string} memberName - The message arguments - * @returns {string} The member's info, or an error message. + * @returns {Promise} The member's info, or an error message. */ async function getMemberInfo(authorId, memberName) { let member = await db.members.findOne({ where: { name: memberName, userid: authorId } }); @@ -206,7 +231,7 @@ async function getMemberInfo(authorId, memberName) { * * @param {string} authorId - The author of the message. * @param {string} name - The member's name. - * @returns {model | string} The member object, or an error message. + * @returns {Promise | Promise} The member object, or an error message. */ mh.getMemberByName = async function(authorId, name) { return await db.members.findOne({ where: { userid: authorId, name: name } }).catch(e => { @@ -219,7 +244,7 @@ mh.getMemberByName = async function(authorId, name) { * * @param {string} authorId - The author of the message * @param {string} proxy - The proxy tag - * @returns {model | string} The member object, or an error message. + * @returns {Promise | Promise} The member object, or an error message. */ mh.getMemberByProxy = async function(authorId, proxy) { return await db.members.findOne({ where: { userid: authorId, proxy: proxy } }).catch(e => { @@ -231,7 +256,7 @@ mh.getMemberByProxy = async function(authorId, proxy) { * Gets all members belonging to the author. * * @param {string} authorId - The author of the message - * @returns {model[] | string} The member object, or an error message. + * @returns {Promise | Promise} The member object, or an error message. */ mh.getMembersByAuthor = async function(authorId) { return await db.members.findAll({ where: { userid: authorId } }).catch(e => { diff --git a/helpers/webhookHelper.js b/helpers/webhookHelper.js index d3c52ff..45bcc14 100644 --- a/helpers/webhookHelper.js +++ b/helpers/webhookHelper.js @@ -49,7 +49,7 @@ async function getWebhook(api, channelId, name) { wh.replaceMessage = async function (api, data, text, member) { if (text.length > 0) { const webhook = await wh.getOrCreateWebhook(api, data.channel_id); - await api.webhooks.execute(webhook.id, webhook.token, {content: text, username: member.displayname ?? member.name, propic: member.propic}); + await api.webhooks.execute(webhook.id, webhook.token, {content: text, username: member.displayname ?? member.name, avatar_url: member.propic}); await api.channels.deleteMessage(data.channel_id, data.id); } else {