generated from Baipyrus/DiscordJS-Template
edit owned messages to include pair explainations
This commit is contained in:
parent
73de6dbcec
commit
3a2073f61f
24
shared.js
24
shared.js
@ -1,8 +1,11 @@
|
|||||||
import { join } from 'path';
|
import { join } from 'path';
|
||||||
import { Op } from 'sequelize';
|
import { Op } from 'sequelize';
|
||||||
|
import { config } from 'dotenv';
|
||||||
import { readdir } from 'fs/promises';
|
import { readdir } from 'fs/promises';
|
||||||
import { Message, RoleEmojiPair } from './database.js';
|
import { Message, RoleEmojiPair } from './database.js';
|
||||||
|
|
||||||
|
config();
|
||||||
|
|
||||||
export const removeSelfRoles = async (interaction, id) => {
|
export const removeSelfRoles = async (interaction, id) => {
|
||||||
// Try deleting message from database
|
// Try deleting message from database
|
||||||
const count = await Message.destroy({
|
const count = await Message.destroy({
|
||||||
@ -53,6 +56,23 @@ const saveMessageData = async (id, role, emoji) => {
|
|||||||
await RoleEmojiPair.create({ message: id, role: role.id, 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) => {
|
export const addSelfRoles = async (interaction, msgID, role, emoji) => {
|
||||||
const { channel } = interaction;
|
const { channel } = interaction;
|
||||||
|
|
||||||
@ -61,6 +81,10 @@ export const addSelfRoles = async (interaction, msgID, role, emoji) => {
|
|||||||
// Get message by id
|
// Get message by id
|
||||||
const message = await channel.messages.fetch(msgID);
|
const message = await channel.messages.fetch(msgID);
|
||||||
|
|
||||||
|
step = 'edit';
|
||||||
|
// Try editing message to explain pair
|
||||||
|
await editMessage(message, role, emoji);
|
||||||
|
|
||||||
step = 'save data from';
|
step = 'save data from';
|
||||||
await saveMessageData(msgID, role, emoji);
|
await saveMessageData(msgID, role, emoji);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user