From 3a2073f61f0ac1683e47fcd1be3c8ca344287370 Mon Sep 17 00:00:00 2001 From: Baipyrus Date: Fri, 9 Feb 2024 19:39:49 +0100 Subject: [PATCH] edit owned messages to include pair explainations --- shared.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/shared.js b/shared.js index 5a09112..7bf028d 100644 --- a/shared.js +++ b/shared.js @@ -1,8 +1,11 @@ import { join } from 'path'; import { Op } from 'sequelize'; +import { config } from 'dotenv'; import { readdir } from 'fs/promises'; import { Message, RoleEmojiPair } from './database.js'; +config(); + export const removeSelfRoles = async (interaction, id) => { // Try deleting message from database const count = await Message.destroy({ @@ -53,6 +56,23 @@ const saveMessageData = async (id, role, emoji) => { await RoleEmojiPair.create({ message: id, role: role.id, emoji }); }; +const editMessage = async (message, role, emoji) => { + if (message.author.id !== process.env.CLIENT) return; + + // Find out whether to pad message or already present + let padding = '\n'; + const reps = await RoleEmojiPair.findAll({ where: { message: message.id } }); + if (reps.length === 0) padding += '\n'; + + // Get old and build new content of message + const current = message.content; + const next = current + padding + + `React with ${emoji} to receive <@&${role.id}>!`; + + // Set message by editing + await message.edit(next); +}; + export const addSelfRoles = async (interaction, msgID, role, emoji) => { const { channel } = interaction; @@ -61,6 +81,10 @@ export const addSelfRoles = async (interaction, msgID, role, emoji) => { // Get message by id const message = await channel.messages.fetch(msgID); + step = 'edit'; + // Try editing message to explain pair + await editMessage(message, role, emoji); + step = 'save data from'; await saveMessageData(msgID, role, emoji);