-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathedit_suggestions_command.ts
224 lines (202 loc) · 9.26 KB
/
edit_suggestions_command.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import axios, {AxiosResponse, Method} from 'axios';
import {
SlashCommandBuilder,
SlashCommandSubcommandBuilder,
SlashCommandIntegerOption,
SlashCommandStringOption,
EmbedBuilder
} from '@discordjs/builders'
import {
ChatInputCommandInteraction,
Client,
CommandInteraction,
ForumChannel,
InteractionReplyOptions,
TextChannel
} from "discord.js";
import {getConfig} from "./config";
export default async (client: Client, states: string[]) => {
const {guildId, suggestionsChannel, logChannel, host, authorization} = await getConfig();
const approvalStates = ['Pending', 'Approved', 'Implemented', 'Partially Approved', 'Partially Implemented', 'Denied', 'Duplicate']
const suggestionsCommand = new SlashCommandBuilder().setName('editsuggestions').setDescription('Suggestion commands').setDefaultMemberPermissions(0).setDMPermission(false)
suggestionsCommand.addSubcommand(new SlashCommandSubcommandBuilder()
.setName('state')
.setDescription('Set suggestion approval state.')
.addIntegerOption(new SlashCommandIntegerOption().setName('id').setDescription('Suggestion ID').setRequired(true))
.addStringOption(
new SlashCommandStringOption()
.setName('state')
.setDescription('Approval State')
.setRequired(true)
.addChoices(
...approvalStates.map(state => ({
name: state,
value: state.toLowerCase().replace(' ', '_')
}))
)
)
)
suggestionsCommand.addSubcommand(new SlashCommandSubcommandBuilder()
.setName('delete')
.setDescription('Remove a suggestion.')
.addIntegerOption(new SlashCommandIntegerOption().setName('id').setDescription('Suggestion ID').setRequired(true))
)
suggestionsCommand.addSubcommand(new SlashCommandSubcommandBuilder()
.setName('add')
.setDescription('Mark a post as a suggestion.')
.addStringOption(new SlashCommandStringOption().setName('id').setDescription('Thread ID').setRequired(true))
)
async function modifySuggestions(
path: string,
interaction: CommandInteraction,
method: Method,
reply: (result) => Promise<InteractionReplyOptions>,
{
body,
failMessage,
notFoundMessage = failMessage
}: {
body?: any,
failMessage: string,
notFoundMessage: string
}
) {
let result: AxiosResponse
try {
result = await axios(`${host}/suggestions/${path}`, {
method,
headers: {
'Content-Type': 'application/json'
},
data: body ? {pass: authorization, ...body} : authorization
})
} catch (error) {
if (error.response.status === 404) {
await interaction.reply({content: notFoundMessage, ephemeral: true})
} else {
await interaction.reply({content: failMessage, ephemeral: true})
await (client.channels.cache.get(logChannel) as TextChannel)
.send(`<@${interaction.guild.ownerId}> Request to suggestion path ${path} failed\nStatus Code: ${error.response.status}\nStatus Text: ${error.response?.statusText}`)
}
return
}
await interaction.reply(await reply(result.data))
}
client.on('threadCreate', async thread => {
if (thread.parent.id === suggestionsChannel) {
const message = await thread.fetchStarterMessage();
let addResult: AxiosResponse
try {
addResult = await axios.post(`${host}/suggestions/add`, {
pass: authorization,
creatorId: thread.ownerId,
threadId: thread.id,
title: thread.name,
content: message.content,
creatorName: message.author.username
}, {
headers: {
'Content-Type': 'application/json'
}
})
} catch (error) {
await thread.send('Failed to request suggestion.');
await (client.channels.cache.get(logChannel) as TextChannel)
.send(`<@${thread.guild.ownerId}> Request to suggestion path add/${thread.id}/${message.id} failed\nStatus Code: ${error.response.status}\nStatus Text: ${error.response?.statusText}`)
return
}
await thread.send(`Suggestion #${addResult.data} created by <@${thread.ownerId}>.\n(Adding <@${thread.guild.ownerId}>.)`);
}
})
client.on('interactionCreate', async interaction => {
if (!interaction.isCommand()) {
return
}
const {commandName, options} = interaction as ChatInputCommandInteraction
if (commandName === 'editsuggestions') {
switch (options.getSubcommand()) {
case 'state': {
const state = {
pending: 0,
approved: 1,
implemented: 2,
partially_approved: 3,
partially_implemented: 4,
denied: 5,
duplicate: 6
}[options.getString('state', true)]
await modifySuggestions(options.getInteger('id', true).toString(), interaction, 'PATCH', async suggestion => {
const guild = client.guilds.cache.get(guildId)
const channel = guild.channels.cache.get(suggestionsChannel) as ForumChannel
const message = await channel.messages.fetch(suggestion.messageId)
const embed = new EmbedBuilder()
.setTitle(`Suggestion #${suggestion.id} has been updated`)
.addFields(
{name: 'Author:', value: `<@${suggestion.authorId}>`},
{name: 'Approval State:', value: states[suggestion.state.id - 1]}
)
.setDescription(message.content.length < 29 ? `[${message.content}](${message.url})` : `[${message.content.substring(0, 29)}...](${message.url})`)
if (suggestion.state.id > 5) {
embed.setColor(0xFF0000)
} else if (suggestion.state.id > 1) {
embed.setColor(0xFF00)
}
return {embeds: [embed]}
}, {
body: {stateId: state + 1},
failMessage: 'Failed to update suggestion.',
notFoundMessage: 'Invalid suggestion ID.'
})
break
}
case 'delete': {
await modifySuggestions(
options.getInteger('id', true).toString(),
interaction,
'DELETE',
async () => ({content: 'Suggestion deleted successfully.'}),
{
failMessage: 'Failed to delete suggestion.',
notFoundMessage: 'Invalid suggestion ID.'
}
)
break
}
case 'add': {
const threadId = options.getString('id');
const guild = client.guilds.cache.get(guildId)
const channel = guild.channels.cache.get(suggestionsChannel) as ForumChannel;
const thread = channel.threads.cache.get(threadId);
if (thread) {
const message = await thread.fetchStarterMessage();
await modifySuggestions(`add`, interaction, 'POST', async result => {
const message = await channel.messages.fetch(threadId)
return {
embeds: [
new EmbedBuilder().setDescription(`Marked [message](${message.url}) by ${message.author} as suggestion with ID ${result}.`)
]
}
}, {
body: {
creatorId: thread.ownerId,
threadId: thread.id,
title: thread.name,
content: message.content,
creatorName: message.author.username
},
failMessage: 'Failed to request suggestion.',
notFoundMessage: 'Invalid message ID.'
})
} else {
await interaction.followUp({
ephemeral: true,
content: 'Post not found'
});
}
break
}
}
}
})
return suggestionsCommand.toJSON()
}