Skip to content

Commit

Permalink
Formatting, edit tag modal, refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
BluDood committed Nov 3, 2022
1 parent 30c04b7 commit e963409
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 67 deletions.
4 changes: 2 additions & 2 deletions autocomplete/tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ const fs = require('fs')
module.exports = {
id: 'tag',
async execute(interaction) {
if (interaction.options.getSubcommand() !== 'remove' && interaction.options.getSubcommand() !== 'get') return
if (!['remove', 'get', 'edit'].includes(interaction.options.getSubcommand())) return
const tags = JSON.parse(fs.readFileSync('./databases/tags.json'))
const tagNames = Object.keys(tags).filter(name => name.startsWith(interaction.options.getFocused()))
const tagNames = Object.keys(tags).filter(name => name.includes(interaction.options.getFocused()))
return interaction.respond(tagNames.map(tag => ({ name: tag, value: tag })))
}
}
5 changes: 4 additions & 1 deletion commands/reactionroles.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,10 @@ module.exports = {
title: `Reaction roles in ${interaction.guild.name}`,
color: config.getColor('accent'),
description: roles
.map(r => `[Jump to Message](https://discord.com/channels/${interaction.guild.id}/${r.channelId}/${r.id})\n${r.roles.map(ro => `${ro.emojiName}: <@&${ro.role}>`).join('\n')}`)
.map(
r =>
`[Jump to Message](https://discord.com/channels/${interaction.guild.id}/${r.channelId}/${r.id})\n${r.roles.map(ro => `${ro.emojiName}: <@&${ro.role}>`).join('\n')}`
)
.join('\n\n')
}
return interaction.reply({
Expand Down
86 changes: 49 additions & 37 deletions commands/tag.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,15 @@
const { SlashCommandBuilder, resolveColor, ModalBuilder, TextInputBuilder, ActionRowBuilder, TextInputStyle } = require('discord.js')
const { SlashCommandBuilder, ModalBuilder, TextInputBuilder, ActionRowBuilder, TextInputStyle } = require('discord.js')
const fs = require('fs')
const checkUserPerms = require('../utils/checkUserPerms')
const config = require('../utils/config')

if (!fs.existsSync('./databases/tags.json')) fs.writeFileSync('./databases/tags.json', '{}')
let tags = JSON.parse(fs.readFileSync('./databases/tags.json', "utf-8"))

module.exports = {
data: new SlashCommandBuilder()
.setName('tag')
.setDescription('Manage tags')
.addSubcommand(subcommand =>
subcommand
.setName('add')
.setDescription('Add a tag')
)
.addSubcommand(subcommand =>
subcommand
.setName('add-with-modal')
.setDescription('Add a tag with a modal')
)
.addSubcommand(subcommand => subcommand.setName('add').setDescription('Add a tag'))
.addSubcommand(subcommand =>
subcommand
.setName('remove')
Expand All @@ -38,12 +28,12 @@ module.exports = {
subcommand
.setName('edit')
.setDescription('Edit a tag')
.addStringOption(option => option.setName('name').setDescription('The name of the tag').setRequired(true))
.addStringOption(option => option.setName('content').setDescription('The new content of the tag').setRequired(true))
.addStringOption(option => option.setName('image').setDescription('URL of image to attach'))
.addStringOption(option => option.setName('name').setDescription('The name of the tag').setRequired(true).setAutocomplete(true))
),

async execute(interaction) {
const tags = JSON.parse(fs.readFileSync('./databases/tags.json', 'utf-8'))

const subcommand = interaction.options.getSubcommand()
if (subcommand === 'add') {
if (!checkUserPerms(interaction)) {
Expand All @@ -52,15 +42,13 @@ module.exports = {
ephemeral: true
})
}

const modal = new ModalBuilder()
.setTitle('Add a tag')
.setCustomId('add-tag')

const modal = new ModalBuilder().setTitle('Add a tag').setCustomId('add-tag')

const nameInput = new TextInputBuilder()
.setCustomId('name')
.setPlaceholder('Name')
.setLabel("Name")
.setLabel('Name')
.setMinLength(1)
.setMaxLength(32)
.setRequired(true)
Expand All @@ -69,26 +57,25 @@ module.exports = {
const contentInput = new TextInputBuilder()
.setCustomId('content')
.setPlaceholder('Content')
.setLabel("Content")
.setLabel('Content')
.setMinLength(1)
.setMaxLength(2000)
.setRequired(true)
.setStyle(TextInputStyle.Paragraph)

const imageInput = new TextInputBuilder()
.setCustomId('image')
.setLabel("Image")
.setLabel('Image')
.setPlaceholder('Image URL')
.setMinLength(1)
.setMaxLength(2000)
.setStyle(TextInputStyle.Short)
.setRequired(false)

modal.addComponents(new ActionRowBuilder().addComponents(nameInput), new ActionRowBuilder().addComponents(contentInput), new ActionRowBuilder().addComponents(imageInput))

await interaction.showModal(modal)
}
else if (subcommand === 'remove') {
} else if (subcommand === 'remove') {
if (!checkUserPerms(interaction)) {
return interaction.reply({
content: "Just don't use that tag then ¯\\_(ツ)_/¯ (You don't have permission to do that.)",
Expand Down Expand Up @@ -121,7 +108,9 @@ module.exports = {
// Shhhh, you didn't see anything.
// i certainly did not ;)
// Sorry for changing this again the lack of the question mark was really getting to me!
// might add my own one too then :)
if (name === 'sbeve is amazing') return interaction.reply({ content: 'I know, right?!', ephemeral: true })
if (name === 'bludood is the best') return interaction.reply({ content: 'very true', ephemeral: true })

return interaction.reply({ content: `A tag with the name ${name} does not exist.`, ephemeral: true })
}
Expand All @@ -142,20 +131,43 @@ module.exports = {
})
}
const name = interaction.options.getString('name')
const content = interaction.options.getString('content')
const image = interaction.options.getString('image')
if (!tags[name]) return interaction.reply({ content: `A tag with the name ${name} does not exist.`, ephemeral: true })

tags[name] = {
content: content,
image: image || tags[name].image
}
fs.writeFileSync('./databases/tags.json', JSON.stringify(tags, null, 4))
interaction.reply({ content: `Edited tag ${name}.`, ephemeral: true })
}
},
const modal = new ModalBuilder().setTitle('Edit a tag').setCustomId('edit-tag')

const nameInput = new TextInputBuilder()
.setCustomId('name')
.setPlaceholder('Name')
.setLabel('Name')
.setMinLength(1)
.setMaxLength(32)
.setRequired(true)
.setStyle(TextInputStyle.Short)
.setValue(name)

updateTags(newTags) {
tags = newTags
const contentInput = new TextInputBuilder()
.setCustomId('content')
.setPlaceholder('Content')
.setLabel('Content')
.setMinLength(1)
.setMaxLength(2000)
.setRequired(true)
.setStyle(TextInputStyle.Paragraph)
.setValue(tags[name].content)

const imageInput = new TextInputBuilder()
.setCustomId('image')
.setLabel('New image')
.setPlaceholder('New image URL')
.setMinLength(1)
.setMaxLength(2000)
.setStyle(TextInputStyle.Short)
.setRequired(false)
.setValue(tags[name].image)

modal.addComponents(new ActionRowBuilder().addComponents(nameInput), new ActionRowBuilder().addComponents(contentInput), new ActionRowBuilder().addComponents(imageInput))

await interaction.showModal(modal)
}
}
}
21 changes: 21 additions & 0 deletions modals/add-tag.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const fs = require('fs')

if (!fs.existsSync('./databases/tags.json')) fs.writeFileSync('./databases/tags.json', '{}')

module.exports = {
id: 'add-tag',
async execute(interaction) {
const tags = JSON.parse(fs.readFileSync('./databases/tags.json', 'utf-8'))
const name = interaction.fields.fields.find(f => f.customId === 'name').value
const content = interaction.fields.fields.find(f => f.customId === 'content').value
const image = interaction.fields.fields.find(f => f.customId === 'image').value
if (tags[name]) return interaction.reply({ content: `A tag with the name ${name} already exists.`, ephemeral: true })

tags[name] = {
content,
image
}
fs.writeFileSync('./databases/tags.json', JSON.stringify(tags, null, 4))
interaction.reply({ content: `Added tag ${name}.`, ephemeral: true })
}
}
21 changes: 21 additions & 0 deletions modals/edit-tag.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const fs = require('fs')

if (!fs.existsSync('./databases/tags.json')) fs.writeFileSync('./databases/tags.json', '{}')

module.exports = {
id: 'edit-tag',
async execute(interaction) {
const tags = JSON.parse(fs.readFileSync('./databases/tags.json', 'utf-8'))
const name = interaction.fields.fields.find(f => f.customId === 'name').value
const content = interaction.fields.fields.find(f => f.customId === 'content').value
const image = interaction.fields.fields.find(f => f.customId === 'image').value
if (!tags[name]) return interaction.reply({ content: `A tag with the name ${name} does not exist.`, ephemeral: true })

tags[name] = {
content,
image
}
fs.writeFileSync('./databases/tags.json', JSON.stringify(tags, null, 4))
interaction.reply({ content: `Edited tag ${name}.`, ephemeral: true })
}
}
Empty file removed modals/nothing here yet
Empty file.
25 changes: 0 additions & 25 deletions modals/tag.js

This file was deleted.

9 changes: 7 additions & 2 deletions utils/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,17 @@ function readline() {
current.customization.accent = await readline()
console.log('Now enter a HEX color you would like as a "good" color. This will apply to embeds which remove a moderation. Leave blank for default.')
current.customization.colors.good = await readline()
console.log('Now enter a HEX color you would like as a "medium" color. This will apply to embeds which add a semi-fatal moderation like a kick or timeout. Leave blank for default.')
console.log(
'Now enter a HEX color you would like as a "medium" color. This will apply to embeds which add a semi-fatal moderation like a kick or timeout. Leave blank for default.'
)
current.customization.colors.medium = await readline()
console.log('Now enter a HEX color you would like as a "bad" color. This will apply to embeds which add a fatal moderation like a ban. Leave blank for default.')
current.customization.colors.bad = await readline()
}
console.log((customColors === 'y' ? '' : "Alright, i'll use the default colors. ") + 'Now, time to set up your moderation roles. Enter all your moderation role IDs here, separated by a comma.')
console.log(
(customColors === 'y' ? '' : "Alright, i'll use the default colors. ") +
'Now, time to set up your moderation roles. Enter all your moderation role IDs here, separated by a comma.'
)
current.modRoles = (await readline()).split(',').map(r => r.trim())
console.log("Got that. Now enter the channel you'd like me to send logs to:")
current.channels.logs = (await readline()).trim()
Expand Down

0 comments on commit e963409

Please sign in to comment.