Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(/tag): Support for edits and deleting #15

Merged
merged 6 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 73 additions & 2 deletions src/commands/tag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,27 @@ import {
TextChannel,
} from 'discord.js';
import { contributorsRole, teamRole, devRole, supportCategory } from '../../config.json';
import { getTag, getTagNames } from '../functions/mongo';
import { deleteTag, getTag, getTagNames } from '../functions/mongo';
import { Tag as TagType } from '../types/main';
import { GuildMember } from 'discord.js';

export const data = new SlashCommandBuilder()
.setName('tag')
.setDescription('Tag preset texts')
.addSubcommand((subcommand) => subcommand.setName('add').setDescription('Add a new tag'))
.addSubcommand((subcommand) => subcommand.setName('edit').setDescription('Edit a tag'))
.addSubcommand((subcommand) =>
subcommand
.setName('delete')
.setDescription('Delete a tag')
.addStringOption((option) =>
option
.setName('name')
.setDescription('The name of the tag')
.setRequired(true)
.setAutocomplete(true)
)
)
.addSubcommand((subcommand) =>
subcommand
.setName('send')
Expand Down Expand Up @@ -46,7 +59,11 @@ export const autoComplete = async (interaction: AutocompleteInteraction) => {
const input = focusedOption.value;
const names = (await getTagNames()).names as string[];
let choices: string | any[] = [];
if (interaction.options.getSubcommand() === 'send' && focusedOption.name === 'name') {
if (
(interaction.options.getSubcommand() === 'send' ||
interaction.options.getSubcommand() === 'delete') &&
focusedOption.name === 'name'
) {
choices = names.filter((name) => name.includes(input));
}
const displayedChoices = choices.slice(0, 25);
Expand Down Expand Up @@ -90,6 +107,60 @@ export const execute = async (interaction: ChatInputCommandInteraction) => {
}
break;
}
case 'edit': {
if (memberRoles.some((role) => [contributorsRole, teamRole, devRole].includes(role))) {
Zickles marked this conversation as resolved.
Show resolved Hide resolved
const modal = new ModalBuilder()
.setCustomId('tagEditForm')
.setTitle('Please enter the updated tag information');

const tagFormName = new TextInputBuilder()
.setStyle(TextInputStyle.Short)
.setCustomId('tagFormUpdatedName')
.setRequired(true)
.setLabel('Name');

const tagFormContent = new TextInputBuilder()
.setStyle(TextInputStyle.Paragraph)
.setCustomId('tagFormUpdatedContent')
.setLabel('New Tag Content')
.setRequired(true);

const tagFormNameReason =
new ActionRowBuilder<ModalActionRowComponentBuilder>().addComponents(tagFormName);
const tagFormContentReason =
new ActionRowBuilder<ModalActionRowComponentBuilder>().addComponents(tagFormContent);
modal.addComponents(tagFormNameReason, tagFormContentReason);
await interaction.showModal(modal);
} else {
return await interaction.reply({
content: 'You do not have permission to use this command',
ephemeral: true,
});
}
break;
}
case 'delete': {
if (memberRoles.some((role) => [contributorsRole, teamRole, devRole].includes(role))) {
Zickles marked this conversation as resolved.
Show resolved Hide resolved
const inputTag = await deleteTag(interaction.options.getString('name') as string);
if (inputTag.success) {
return await interaction.reply({
content: 'Tag deleted successfully',
ephemeral: true,
});
} else {
return await interaction.reply({
content: 'Tag not found',
ephemeral: true,
});
}
} else {
return await interaction.reply({
content: 'You do not have permission to use this command',
ephemeral: true,
});
}
break;
}
case 'send': {
const name = (interaction.options.getString('name') as string).toLowerCase();
let messageLink = interaction.options.getString('message-link') || null;
Expand Down
21 changes: 20 additions & 1 deletion src/events/interactionCreate.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable no-console */
import { TextChannel, Interaction, Events, Guild, InteractionType, EmbedBuilder } from 'discord.js';
import { eventMessage } from '../functions/logger';
import { Tag } from '../functions/mongo';
import { Tag, modifyTag } from '../functions/mongo';

export const name = Events.InteractionCreate;
export const execute = async (interaction: Interaction) => {
Expand Down Expand Up @@ -87,6 +87,25 @@ export const execute = async (interaction: Interaction) => {
.setDescription(`The tag \`${name}\` has been added successfully`);
await interaction.reply({ embeds: [embed], ephemeral: true });
}
if (interaction.customId === 'tagEditForm') {
Zickles marked this conversation as resolved.
Show resolved Hide resolved
const name = interaction.fields.getTextInputValue('tagFormUpdatedName').toLowerCase();
Kathund marked this conversation as resolved.
Show resolved Hide resolved
const content = interaction.fields.getTextInputValue('tagFormUpdatedContent');

const updatedTag = await modifyTag(
name,
new Tag(name, content, interaction.user.id, 'approved')
);
if (updatedTag.success) {
const embed = new EmbedBuilder()
.setTitle('Tag Updated')
.setDescription(`The tag \`${name}\` has been added successfully`);
await interaction.reply({ embeds: [embed], ephemeral: true });
} else if (updatedTag.success === false && updatedTag.info === 'Tag not found') {
await interaction.reply({ content: 'This tag does not exist!', ephemeral: true });
} else {
await interaction.reply({ content: 'An error occurred', ephemeral: true });
}
}
}
} catch (error: any) {
console.log(error);
Expand Down
13 changes: 13 additions & 0 deletions src/functions/mongo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,19 @@ export const modifyTag = async (name: string, tag: TagType) => {
}
};

export const deleteTag = async (name: string) => {
try {
const tag = await TagModel.deleteOne({ name: name });
if (tag) {
return { success: true, info: 'Tag deleted successfully' };
} else {
return { success: false, info: 'Tag not found' };
}
} catch (error) {
return { success: false, info: 'An error occurred', error: error };
}
};

export const getTag = async (name: string) => {
try {
const tag = await TagModel.findOne({ name: name });
Expand Down