Skip to content

Commit ff2beb7

Browse files
committed
Auto-updating FAQ list containing tags
1 parent a73eaa3 commit ff2beb7

File tree

4 files changed

+56
-2
lines changed

4 files changed

+56
-2
lines changed

autocomplete/tag.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const tag = require('../utils/tag')
33
module.exports = {
44
id: 'tag',
55
async execute(interaction) {
6-
if (!['remove', 'get', 'edit'].includes(interaction.options.getSubcommand())) return
6+
if (!['remove', 'get', 'edit', 'faqtoggle'].includes(interaction.options.getSubcommand())) return
77
const tags = tag.getAll()
88
const tagNames = Object.keys(tags).filter(name => name.includes(interaction.options.getFocused()))
99
return interaction.respond(tagNames.map(tag => ({ name: tag, value: tag })))

commands/tag.js

+22
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ module.exports = {
2727
.setName('edit')
2828
.setDescription('Edit a tag')
2929
.addStringOption(option => option.setName('name').setDescription('The name of the tag').setRequired(true).setAutocomplete(true))
30+
)
31+
.addSubcommand(subcommand =>
32+
subcommand
33+
.setName('faqtoggle')
34+
.setDescription('Toggle specified item to appear on the FAQ list')
35+
.addStringOption(option => option.setName('name').setDescription('The name of the tag').setRequired(true).setAutocomplete(true))
3036
),
3137

3238
async execute(interaction) {
@@ -83,6 +89,7 @@ module.exports = {
8389

8490
tag.remove(name)
8591
interaction.reply({ content: `Removed tag ${name}.`, ephemeral: true })
92+
tag.updateFAQList(interaction.client)
8693
} else if (subcommand === 'list') {
8794
const tagList = Object.keys(tag.getAll())
8895
if (tagList.length === 0) return interaction.reply({ content: 'There are no tags.', ephemeral: true })
@@ -165,6 +172,21 @@ module.exports = {
165172
modal.addComponents(new ActionRowBuilder().addComponents(nameInput), new ActionRowBuilder().addComponents(contentInput), new ActionRowBuilder().addComponents(imageInput))
166173

167174
await interaction.showModal(modal)
175+
} else if (subcommand === 'faqtoggle') {
176+
if (!checkUserPerms(interaction)) {
177+
return interaction.reply({
178+
content: 'You do not have permission to do that!',
179+
ephemeral: true
180+
})
181+
}
182+
183+
if (!config.get().channels.faq) return interaction.reply({ content: 'An FAQ channel has not been configured.', ephemeral: true })
184+
const name = interaction.options.getString('name')
185+
if (!tag.get(name)) return interaction.reply({ content: `A tag with the name ${name} does not exist.`, ephemeral: true })
186+
const wasFAQItem = tag.get(name).faqitem || false
187+
tag.modify(name, null, null, wasFAQItem ? false : true)
188+
interaction.reply({ content: wasFAQItem ? `Removed tag ${name} from the FAQ list.` : `Added tag ${name} to the FAQ list.`, ephemeral: true })
189+
tag.updateFAQList(interaction.client)
168190
}
169191
}
170192
}

modals/edit-tag.js

+1
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ module.exports = {
1010

1111
tag.modify(name, content, image)
1212
interaction.reply({ content: `Edited tag ${name}.`, ephemeral: true })
13+
tag.updateFAQList(interaction.client)
1314
}
1415
}

utils/tag.js

+32-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const fs = require('fs')
2+
const config = require('./config')
23

34
function ensureDatabase() {
45
const exists = fs.existsSync('./databases/tags.json')
@@ -22,6 +23,17 @@ function getDatabase() {
2223
return JSON.parse(fs.readFileSync('./databases/tags.json'))
2324
}
2425

26+
function generateEmbed(name, content, image) {
27+
return {
28+
title: name,
29+
description: content,
30+
color: config.getColor('accent'),
31+
image: {
32+
url: image
33+
}
34+
}
35+
}
36+
2537
module.exports = {
2638
add: async (name, content, image) => {
2739
ensureDatabase()
@@ -40,14 +52,15 @@ module.exports = {
4052
delete database[name]
4153
writeDatabase(database)
4254
},
43-
modify: (name, content, image) => {
55+
modify: (name, content, image, faqitem) => {
4456
ensureDatabase()
4557
const database = getDatabase()
4658
if (!database[name]) database[name] = {}
4759
const item = database[name]
4860
item.name = name || item.name
4961
item.content = content || item.content
5062
item.image = image || item.image
63+
item.faqitem = faqitem ?? item.faqitem
5164
writeDatabase(database)
5265
},
5366
get: name => {
@@ -57,5 +70,23 @@ module.exports = {
5770
getAll: () => {
5871
ensureDatabase()
5972
return getDatabase()
73+
},
74+
updateFAQList: async client => {
75+
if (!config.get().channels.faq) return
76+
ensureDatabase()
77+
const database = getDatabase()
78+
const guild = await client.guilds.fetch(config.get().guildId)
79+
const channel = await guild.channels.fetch(config.get().channels.faq)
80+
const messages = await channel.messages.fetch({ limit: 100 })
81+
const botMessages = messages.filter(m => m.author.id === client.user.id).toJSON()
82+
await channel.bulkDelete(botMessages)
83+
84+
const embeds = Object.values(database)
85+
.filter(t => t.faqitem === true)
86+
.map(({ name, content, image }) => generateEmbed(name, content, image))
87+
if (embeds.length === 0) return
88+
const chunks = []
89+
for (let i = 0; i < embeds.length; i += 10) chunks.push(embeds.slice(i, i + 10))
90+
chunks.forEach(embeds => channel.send({ embeds }))
6091
}
6192
}

0 commit comments

Comments
 (0)