-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathindex.ts
More file actions
295 lines (244 loc) · 13.6 KB
/
index.ts
File metadata and controls
295 lines (244 loc) · 13.6 KB
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
import {api, opendiscord, utilities} from "#opendiscord"
import * as discord from "discord.js"
if (utilities.project != "openticket") throw new api.ODPluginError("This plugin only works in Open Ticket!")
import { OTForms_Form } from "./classes/Form"
import { OTForms_AnswersManager } from "./classes/AnswersManager";
import "./config/configRegistration";
import "./builders/messageBuilders";
import "./builders/embedBuilders";
import "./builders/buttonBuilders";
import "./builders/modalBuilders";
import "./builders/dropdownBuilders";
import "./builders/commandBuilders";
const forms = new Map<string, OTForms_Form>();
opendiscord.events.get("afterCodeExecuted").listen(async () => {
const formsConfig = opendiscord.configs.get("ot-ticket-forms:config").data;
for(const formConfig of formsConfig) {
formConfig.questions.sort((a, b) => a.position - b.position);
}
opendiscord.log("Plugin \"ot-ticket-forms\" restoring answers...", "plugin");
await OTForms_AnswersManager.restore();
})
/* TICKET CREATED EVENT
* When a ticket is created, check if the ticket ID is in the list of forms that should be automatically sent.
* If it is, send the form to the channel.
*/
opendiscord.events.get("afterTicketCreated").listen(async (ticket, creator, channel) => {
const formsConfig = opendiscord.configs.get("ot-ticket-forms:config").data;
const ticketId = ticket.option.id.value;
for(const formConfig of formsConfig) {
if (formConfig.autoSendOptionIds.includes(ticketId)) {
const startMessageTemplate = await opendiscord.builders.messages.getSafe("ot-ticket-forms:start-form-message").build("ticket", {
formId: formConfig.id,
formName: formConfig.name,
formDescription: formConfig.description,
formColor: formConfig.color,
acceptAnswers: true
});
const startMessage = await channel.send(startMessageTemplate.message);
const answersChannel = await channel.guild.channels.fetch(formConfig.responseChannel);
if(!answersChannel || !answersChannel.isTextBased()) {
opendiscord.log("Error: Invalid answers channel.", "plugin");
return;
}
const questions = formConfig.questions.sort((a, b) => a.position - b.position);
const form = new OTForms_Form(formConfig.id, startMessage, formConfig.name, formConfig.color, startMessage, questions, answersChannel);
forms.set(formConfig.id, form);
}
};
});
//REGISTER COMMAND RESPONDER
opendiscord.events.get("onCommandResponderLoad").listen((commands) => {
const generalConfig = opendiscord.configs.get("opendiscord:general")
const formsConfig = opendiscord.configs.get("ot-ticket-forms:config").data;
/* FORM COMMAND RESPONDER
* The command manage forms. Currently limited to sending forms to a channel.
*/
commands.add(new api.ODCommandResponder("ot-ticket-forms:form",generalConfig.data.prefix,"form"))
commands.get("ot-ticket-forms:form").workers.add([
new api.ODWorker("ot-ticket-forms:form",0,async (instance,params,source,cancel) => {
const {guild,channel,user} = instance
if (!guild){
instance.reply(await opendiscord.builders.messages.getSafe("opendiscord:error-not-in-guild").build("button",{channel,user}))
return cancel()
}
if (!opendiscord.permissions.hasPermissions("admin",await opendiscord.permissions.getPermissions(instance.user,instance.channel,instance.guild))){
instance.reply(await opendiscord.builders.messages.getSafe("opendiscord:error-no-permissions").build(source,{guild:instance.guild,channel:instance.channel,user:instance.user,permissions:["admin"]}))
return cancel()
}
//command doesn't support text-commands!
if (source == "text") return cancel()
const scope = instance.options.getSubCommand() as "send"
if (scope == "send"){
const formId = instance.options.getString("id",true)
const formChannel = instance.options.getChannel("channel",true) as discord.GuildTextBasedChannel
const formConfig = formsConfig.find((form) => form.id == formId)
if (!formConfig){
instance.reply(await opendiscord.builders.messages.getSafe("opendiscord:error").build(source,{guild,channel,user,layout:"simple",error:"Invalid form id. Please try again!"}))
return cancel()
}
const startMessageTemplate = await opendiscord.builders.messages.getSafe("ot-ticket-forms:start-form-message").build("ticket", {
formId: formConfig.id,
formName: formConfig.name,
formDescription: formConfig.description,
formColor: formConfig.color,
acceptAnswers: true
});
const startMessage = await formChannel.send(startMessageTemplate.message);
const answersChannel = await guild.channels.fetch(formConfig.responseChannel);
if(!answersChannel || !answersChannel.isTextBased()) {
opendiscord.log("Error: Invalid answers channel.", "plugin");
return;
}
const questions = formConfig.questions.sort((a, b) => a.position - b.position);
const form = new OTForms_Form(formConfig.id, startMessage, formConfig.name, formConfig.color, startMessage, questions, answersChannel);
forms.set(formConfig.id, form);
}
await instance.reply(await opendiscord.builders.messages.getSafe("ot-ticket-forms:success-message").build(source,{}))
}),
new api.ODWorker("ot-ticket-forms:logs",-1,(instance,params,source,cancel) => {
const scope = instance.options.getSubCommand() as "send"
opendiscord.log(instance.user.displayName+" used the 'form "+scope+"' command!","plugin",[
{key:"user",value:instance.user.username},
{key:"userid",value:instance.user.id,hidden:true},
{key:"channelid",value:instance.channel.id,hidden:true},
{key:"method",value:source}
])
})
])
})
//REGISTER HELP MENU
opendiscord.events.get("onHelpMenuComponentLoad").listen((menu) => {
menu.get("opendiscord:extra").add(new api.ODHelpMenuCommandComponent("ot-ticket-forms:form",0,{
slashName:"form send",
slashDescription:"Send a form to a channel.",
}))
})
// BUTTON RESPONDER
opendiscord.events.get("onButtonResponderLoad").listen((buttons, responders, actions) => {
/* START FORM BUTTON RESPONDER
* The button to start answering a form.
*/
buttons.add(new api.ODButtonResponder("ot-ticket-forms:start-form-button", /^ot-ticket-forms:sb_/));
opendiscord.responders.buttons.get("ot-ticket-forms:start-form-button").workers.add(new api.ODWorker("ot-ticket-forms:start-form-button", 0, async (instance, params, source, cancel) => {
const formId = instance.interaction.customId.split("_")[1];
const form = forms.get(formId);
if (!form) return
const session = form.createSession(instance.interaction.user, instance.message);
await session.setInstance(instance);
await session.start();
}));
/* CONTINUE BUTTON RESPONDER
* The button between two form questions. To continue to the next section of the form.
*/
buttons.add(new api.ODButtonResponder("ot-ticket-forms:continue-button", /^ot-ticket-forms:cb_/));
opendiscord.responders.buttons.get("ot-ticket-forms:continue-button").workers.add(new api.ODWorker("ot-ticket-forms:continue-button", 0, async (instance, params, source, cancel) => {
const customIdParts = instance.interaction.customId.split("_");
const formId = customIdParts[1];
const form = forms.get(formId);
if (!form) return
const sessionId = customIdParts[2];
const session = form.getSession(sessionId);
if (!session) return
session.setInstance(instance);
await session.continue("question");
}));
/* DELETE ANSWERS MESSAGE BUTTON RESPONDER
* The button to delete a form session. Visible on the answers message until the form is completed.
*/
buttons.add(new api.ODButtonResponder("ot-ticket-forms:delete-answers-button", /^ot-ticket-forms:db_/));
opendiscord.responders.buttons.get("ot-ticket-forms:delete-answers-button").workers.add(new api.ODWorker("ot-ticket-forms:delete-answers-button", 0, async (instance, params, source, cancel) => {
await instance.defer("update",true);
OTForms_AnswersManager.removeInstance(instance.message.id);
instance.message.delete();
const customIdParts = instance.interaction.customId.split("_");
const formId = customIdParts[1];
const form = forms.get(formId);
if (!form) return
const sessionId = customIdParts[2];
form.finalizeSession(sessionId, form.name, instance.user);
}));
/* QUESTION BUTTON RESPONDER
* A type button question can have multiple question buttons. Every button represents a possible answer.
*/
buttons.add(new api.ODButtonResponder("ot-ticket-forms:question-button", /^ot-ticket-forms:qb_/));
opendiscord.responders.buttons.get("ot-ticket-forms:question-button").workers.add(new api.ODWorker("ot-ticket-forms:question-button", 0, async (instance, params, source, cancel) => {
const customIdParts = instance.interaction.customId.split("_");
const formId = customIdParts[1];
const form = forms.get(formId);
if (!form) return
const sessionId = customIdParts[2];
const session = form.getSession(sessionId);
if (!session) return
session.setInstance(instance);
const answer = customIdParts[3];
await session.handleButtonResponse(answer);
}));
// PAGINATION BUTTONS
/* NEXT PAGE BUTTON RESPONDER
* The button to go to the next page of the answers message.
*/
buttons.add(new api.ODButtonResponder("ot-ticket-forms:next-page-button", /^ot-ticket-forms:npb_/));
opendiscord.responders.buttons.get("ot-ticket-forms:next-page-button").workers.add(new api.ODWorker("ot-ticket-forms:next-page-button", 0, async (instance, params, source, cancel) => {
instance.defer("update",true);
const answersManager = OTForms_AnswersManager.getInstance(instance.message.id);
if (!answersManager) return;
const currentPageNumber = Number(instance.interaction.customId.split("_")[1]);
answersManager.editMessage(currentPageNumber + 1);
}));
/* PREVIOUS PAGE BUTTON RESPONDER
* The button to go to the previous page of the answers message.
*/
buttons.add(new api.ODButtonResponder("ot-ticket-forms:previous-page-button", /^ot-ticket-forms:ppb_/));
opendiscord.responders.buttons.get("ot-ticket-forms:previous-page-button").workers.add(new api.ODWorker("ot-ticket-forms:previous-page-button", 0, async (instance, params, source, cancel) => {
instance.defer("update",true);
const answersManager = OTForms_AnswersManager.getInstance(instance.message.id);
if (!answersManager) return;
const currentPageNumber = Number(instance.interaction.customId.split("_")[1]);
answersManager.editMessage(currentPageNumber - 1);
}));
});
// MODAL RESPONDER
opendiscord.events.get("onModalResponderLoad").listen((modals, responders, actions) => {
/* QUESTIONS MODAL RESPONDER
* This modal is used to answer questions of a form.
*/
modals.add(new api.ODModalResponder("ot-ticket-forms:questions-modal", /^ot-ticket-forms:qm_/));
opendiscord.responders.modals.get("ot-ticket-forms:questions-modal").workers.add(new api.ODWorker("ot-ticket-forms:questions-modal", 0, async (instance, params, source, cancel) => {
const customIdParts = instance.interaction.customId.split("_");
const formId = customIdParts[1];
const form = forms.get(formId);
if (!form) return
const sessionId = customIdParts[2];
const session = form.getSession(sessionId);
if (!session) return
const answeredQuestions: {number: number,required:boolean}[] = customIdParts[3].split('-').map(pair => {
const [num, required] = pair.split('/');
return {
number: Number(num),
required: required === '1'
};
});
const response = instance.values;
session.setInstance(instance, true);
await session.handleModalResponse(response, answeredQuestions);
}));
})
opendiscord.events.get("onDropdownResponderLoad").listen((dropdowns, responders, actions) => {
/* QUESTION DROPDOWN RESPONDER
* This dropdown is used to answer questions of a form. It can be used for multiple choice questions.
*/
dropdowns.add(new api.ODDropdownResponder("ot-ticket-forms:question-dropdown", /^ot-ticket-forms:qd_/));
opendiscord.responders.dropdowns.get("ot-ticket-forms:question-dropdown").workers.add(new api.ODWorker("ot-ticket-forms:question-dropdown", 0, async (instance, params, source, cancel) => {
const customIdParts = instance.interaction.customId.split("_");
const formId = customIdParts[1];
const form = forms.get(formId);
if (!form) return
const sessionId = customIdParts[2];
const session = form.getSession(sessionId);
if (!session) return
const response = instance.values;
session.setInstance(instance);
await session.handleDropdownResponse(response);
}));
});