-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.ts
472 lines (425 loc) · 21.8 KB
/
index.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
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
import {api, opendiscord, utilities} from "#opendiscord"
import * as discord from "discord.js"
import ansis from "ansis"
if (utilities.project != "openticket") throw new api.ODPluginError("This plugin only works in Open Ticket!")
//DECLARATION
export interface OTCustomEmbedsEmbed {
id:string,
content:string,
title:string,
description:string,
customColor:discord.ColorResolvable,
image:string,
thumbnail:string,
authorText:string,
authorImage:string,
footerText:string,
footerImage:string,
timestamp:boolean,
fields:{name:string,value:string,inline:boolean}[],
ping:{
"@here":boolean,
"@everyone":boolean,
custom:string[]
}
}
export class OTCustomEmbedsConfig extends api.ODJsonConfig {
declare data: OTCustomEmbedsEmbed[]
}
export class OTCustomEmbed extends api.ODManagerData {
data: OTCustomEmbedsEmbed
constructor(id:api.ODValidId, data:OTCustomEmbedsEmbed){
super(id)
this.data = data
}
}
export class OTCustomEmbedManager extends api.ODManager<OTCustomEmbed> {
id: api.ODId = new api.ODId("ot-embeds:manager")
defaults: {customEmbedsLoading:boolean} = {customEmbedsLoading:true}
constructor(debug:api.ODDebugger){
super(debug,"custom embed")
}
}
declare module "#opendiscord-types" {
export interface ODPluginManagerIds_Default {
"ot-embeds":api.ODPlugin
}
export interface ODConfigManagerIds_Default {
"ot-embeds:config":OTCustomEmbedsConfig
}
export interface ODCheckerManagerIds_Default {
"ot-embeds:config":api.ODChecker
}
export interface ODSlashCommandManagerIds_Default {
"ot-embeds:embed":api.ODSlashCommand
}
export interface ODTextCommandManagerIds_Default {
"ot-embeds:embed":api.ODTextCommand
}
export interface ODCommandResponderManagerIds_Default {
"ot-embeds:embed":{source:"slash"|"text",params:{},workers:"ot-embeds:embed"|"ot-embeds:logs"},
}
export interface ODMessageManagerIds_Default {
"ot-embeds:embed-message":{source:"slash"|"other",params:{embed:OTCustomEmbedsEmbed},workers:"ot-embeds:embed-message"},
"ot-embeds:success-message":{source:"slash"|"other",params:{},workers:"ot-embeds:success-message"},
}
export interface ODEmbedManagerIds_Default {
"ot-embeds:embed-embed":{source:"slash"|"other",params:{embed:OTCustomEmbedsEmbed},workers:"ot-embeds:embed-embed"},
}
export interface ODEventIds_Default {
"ot-embeds:onEmbedLoad":api.ODEvent_Default<(embeds:OTCustomEmbedManager) => api.ODPromiseVoid>
"ot-embeds:afterEmbedsLoaded":api.ODEvent_Default<(embeds:OTCustomEmbedManager) => api.ODPromiseVoid>
}
export interface ODPluginClassManagerIds_Default {
"ot-embeds:manager":OTCustomEmbedManager
}
}
//REGISTER PLUGIN CLASS
opendiscord.events.get("onPluginClassLoad").listen((classes) => {
classes.add(new OTCustomEmbedManager(opendiscord.debug))
})
//REGISTER CONFIG FILE
opendiscord.events.get("onConfigLoad").listen((configs) => {
configs.add(new OTCustomEmbedsConfig("ot-embeds:config","config.json","./plugins/ot-embeds/"))
})
//CONFIG STRUCTURE
export const embedsConfigStructure = new api.ODCheckerArrayStructure("ot-embeds:config",{maxLength:25,allowedTypes:["object"],propertyChecker:new api.ODCheckerObjectStructure("ot-embeds:config",{children:[
//TODO id
{key:"id",optional:false,priority:0,checker:new api.ODCheckerCustomStructure_UniqueId("ot-embeds:id","ot-embeds","embed-ids",{regex:/^[A-Za-z0-9-éèçàêâôûî]+$/,minLength:3,maxLength:40})},
{key:"content",optional:false,priority:0,checker:new api.ODCheckerStringStructure("ot-embeds:content",{maxLength:2000})},
{key:"title",optional:false,priority:0,checker:new api.ODCheckerStringStructure("ot-embeds:title",{minLength:1,maxLength:256})},
{key:"description",optional:false,priority:0,checker:new api.ODCheckerStringStructure("ot-embeds:description",{maxLength:4096})},
{key:"customColor",optional:false,priority:0,checker:new api.ODCheckerCustomStructure_HexColor("ot-embeds:custom-color",true,true)},
{key:"image",optional:false,priority:0,checker:new api.ODCheckerCustomStructure_UrlString("ot-embeds:image",true,{allowHttp:false,allowedExtensions:[".png",".jpg",".jpeg",".webp",".gif"]})},
{key:"thumbnail",optional:false,priority:0,checker:new api.ODCheckerCustomStructure_UrlString("ot-embeds:thumbnail",true,{allowHttp:false,allowedExtensions:[".png",".jpg",".jpeg",".webp",".gif"]})},
{key:"authorText",optional:false,priority:0,checker:new api.ODCheckerStringStructure("ot-embeds:author-text",{maxLength:256})},
{key:"authorImage",optional:false,priority:0,checker:new api.ODCheckerCustomStructure_UrlString("ot-embeds:author-image",true,{allowHttp:false,allowedExtensions:[".png",".jpg",".jpeg",".webp",".gif"]})},
{key:"footerText",optional:false,priority:0,checker:new api.ODCheckerStringStructure("ot-embeds:footer-text",{maxLength:2048})},
{key:"footerImage",optional:false,priority:0,checker:new api.ODCheckerCustomStructure_UrlString("ot-embeds:footer-image",true,{allowHttp:false,allowedExtensions:[".png",".jpg",".jpeg",".webp",".gif"]})},
{key:"timestamp",optional:false,priority:0,checker:new api.ODCheckerBooleanStructure("ot-embeds:timestamp",{})},
{key:"fields",optional:false,priority:0,checker:new api.ODCheckerArrayStructure("ot-embeds:fields",{allowedTypes:["object"],propertyChecker:new api.ODCheckerObjectStructure("ot-embeds:field",{children:[
{key:"name",optional:false,priority:0,checker:new api.ODCheckerStringStructure("ot-embeds:field-name",{minLength:1,maxLength:256})},
{key:"value",optional:false,priority:0,checker:new api.ODCheckerStringStructure("ot-embeds:field-value",{minLength:1,maxLength:1024})},
{key:"inline",optional:false,priority:0,checker:new api.ODCheckerBooleanStructure("ot-embeds:field-inline",{})}
]})})},
{key:"ping",optional:false,priority:0,checker:new api.ODCheckerObjectStructure("ot-embeds:ping",{children:[
{key:"@here",optional:false,priority:0,checker:new api.ODCheckerBooleanStructure("ot-embeds:ping-here",{})},
{key:"@everyone",optional:false,priority:0,checker:new api.ODCheckerBooleanStructure("ot-embeds:ping-everyone",{})},
{key:"custom",optional:false,priority:0,checker:new api.ODCheckerCustomStructure_DiscordIdArray("ot-embeds:ping-custom","role",[],{allowDoubles:false})},
]})}
]})})
//REGISTER CONFIG CHECKER
opendiscord.events.get("onCheckerLoad").listen((checkers) => {
const config = opendiscord.configs.get("ot-embeds:config")
checkers.add(new api.ODChecker("ot-embeds:config",checkers.storage,0,config,embedsConfigStructure))
})
//REGISTER SLASH COMMAND
const act = discord.ApplicationCommandType
const acot = discord.ApplicationCommandOptionType
opendiscord.events.get("onSlashCommandLoad").listen((slash) => {
const config = opendiscord.configs.get("ot-embeds:config")
//create embed choices
const embedChoices : {name:string, value:string}[] = []
config.data.forEach((embed) => {
embedChoices.push({name:embed.title,value:embed.id})
})
slash.add(new api.ODSlashCommand("ot-embeds:embed",{
name:"embed",
description:"Send a custom embed or choose a pre-configured message.",
type:act.ChatInput,
contexts:[discord.InteractionContextType.Guild],
integrationTypes:[discord.ApplicationIntegrationType.GuildInstall],
options:[
{
type:acot.Subcommand,
name:"custom",
description:"Create a custom embed from scratch. This method isn't as advanced as the pre-configured messages.",
options:[
{
type:acot.Channel,
name:"channel",
description:"The channel to send the embed.",
required:true,
channelTypes:[discord.ChannelType.GuildText,discord.ChannelType.GuildAnnouncement]
},
{
type:acot.String,
name:"color",
description:"The embed color!",
required:true,
choices:[
{name:"White", value:"White"},
{name:"Aqua", value:"Aqua"},
{name:"Green", value:"Green"},
{name:"Blue", value:"Blue"},
{name:"Yellow", value:"Yellow"},
{name:"Purple", value:"Purple"},
{name:"Orange", value:"Orange"},
{name:"Red", value:"Red"},
{name:"Grey", value:"Grey"},
{name:"Navy", value:"Navy"},
{name:"Blurple", value:"Blurple"},
{name:"Black", value:"#000000"},
{name:"Bot Color", value:"%CONFIG_COLOR%"}
]
},
{
type:acot.String,
name:"title",
description:"The embed title.",
required:false,
maxLength:256
},
{
type:acot.String,
name:"description",
description:"The embed description.",
required:false,
maxLength:4096
},
{
type:acot.String,
name:"footer",
description:"The embed footer.",
required:false,
maxLength:2048
},
{
type:acot.String,
name:"author",
description:"The embed author.",
required:false,
maxLength:256
},
{
type:acot.Boolean,
name:"timestamp",
description:"Add an embed timestamp.",
required:false
},
{
type:acot.String,
name:"image",
description:"The embed image.",
required:false
},
{
type:acot.String,
name:"thumbnail",
description:"The embed thumbnail.",
required:false
},
{
type:acot.Mentionable,
name:"ping",
description:"The user/role you want to ping.",
required:false
}
]
},
{
type:acot.Subcommand,
name:"preset",
description:"Spawn a pre-configured embed from the config.",
options:[
{
type:acot.Channel,
name:"channel",
description:"The channel to send the embed.",
required:true,
channelTypes:[discord.ChannelType.GuildText,discord.ChannelType.GuildAnnouncement]
},
{
name:"id",
description:"The id of the embed to send.",
type:acot.String,
required:true,
choices:embedChoices
}
]
}
]
},(current) => {
//check if this slash command needs to be updated
if (!current.options) return true
const presetSubcommand = current.options.find((opt) => opt.name == "preset") as discord.ApplicationCommandSubCommandData|undefined
if (!presetSubcommand || !presetSubcommand.options) return true
const idOption = presetSubcommand.options.find((opt) => opt.name == "id" && opt.type == acot.String) as discord.ApplicationCommandStringOptionData|undefined
if (!idOption || !idOption.choices || idOption.choices.length != embedChoices.length) return true
else if (!embedChoices.every((embed) => {
if (!idOption.choices) return false
else if (!idOption.choices.find((choice) => choice.value == embed.value && choice.name == embed.name)) return false
else return true
})) return true
else return false
}))
})
//REGISTER HELP MENU
opendiscord.events.get("onHelpMenuComponentLoad").listen((menu) => {
menu.get("opendiscord:extra").add(new api.ODHelpMenuCommandComponent("ot-embeds:embed",0,{
slashName:"embed",
slashDescription:"Create a custom embed in the server.",
}))
})
//LOAD EMBEDS
opendiscord.events.get("afterBlacklistLoaded").listen(async () => {
const embedManager = opendiscord.plugins.classes.get("ot-embeds:manager")
const config = opendiscord.configs.get("ot-embeds:config")
opendiscord.log("Loading custom embeds...","plugin")
if (embedManager.defaults.customEmbedsLoading){
config.data.forEach((embed) => {
embedManager.add(new OTCustomEmbed(embed.id,embed))
})
}
await opendiscord.events.get("ot-embeds:onEmbedLoad").emit([embedManager])
await opendiscord.events.get("ot-embeds:afterEmbedsLoaded").emit([embedManager])
})
//REGISTER EMBED BUILDER
opendiscord.events.get("onEmbedBuilderLoad").listen((embeds) => {
embeds.add(new api.ODEmbed("ot-embeds:embed-embed"))
embeds.get("ot-embeds:embed-embed").workers.add(
new api.ODWorker("ot-embeds:embed-embed",0,(instance,params,source,cancel) => {
const generalConfig = opendiscord.configs.get("opendiscord:general")
const {embed} = params
instance.setTitle(embed.title)
instance.setColor((embed.customColor) ? embed.customColor : generalConfig.data.mainColor)
if (embed.description) instance.setDescription(embed.description)
if (embed.image) instance.setImage(embed.image)
if (embed.thumbnail) instance.setThumbnail(embed.thumbnail)
if (embed.footerText) instance.setFooter(embed.footerText,(embed.footerImage) ? embed.footerImage : null)
if (embed.authorText) instance.setAuthor(embed.authorText,(embed.authorImage) ? embed.authorImage : null)
if (embed.timestamp) instance.setTimestamp(new Date())
if (embed.fields.length > 0) instance.setFields(embed.fields)
})
)
})
//REGISTER MESSAGE BUILDER
opendiscord.events.get("onMessageBuilderLoad").listen((messages) => {
messages.add(new api.ODMessage("ot-embeds:embed-message"))
messages.get("ot-embeds:embed-message").workers.add(
new api.ODWorker("ot-embeds:embed-message",0,async (instance,params,source,cancel) => {
const {embed} = params
instance.addEmbed(await opendiscord.builders.embeds.getSafe("ot-embeds:embed-embed").build(source,{embed}))
//create pings
const pings: string[] = []
if (embed.ping["@everyone"]) pings.push("@everyone")
if (embed.ping["@here"]) pings.push("@here")
embed.ping.custom.forEach((ping) => pings.push(discord.roleMention(ping)))
const pingText = (pings.length > 0) ? pings.join(" ")+"\n" : ""
//create content
if (embed.content !== "") instance.setContent(pingText+embed.content)
else if (pings.length > 0) instance.setContent(pingText)
})
)
messages.add(new api.ODMessage("ot-embeds:success-message"))
messages.get("ot-embeds:success-message").workers.add(
new api.ODWorker("ot-embeds:success-message",0,async (instance,params,source,cancel) => {
instance.setContent("✅ The embed has been created successfully!")
instance.setEphemeral(true)
})
)
})
//REGISTER COMMAND RESPONDER
opendiscord.events.get("onCommandResponderLoad").listen((commands) => {
const generalConfig = opendiscord.configs.get("opendiscord:general")
const embedManager = opendiscord.plugins.classes.get("ot-embeds:manager")
commands.add(new api.ODCommandResponder("ot-embeds:embed",generalConfig.data.prefix,"embed"))
commands.get("ot-embeds:embed").workers.add([
new api.ODWorker("ot-embeds:embed",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(source,{channel,user}))
return cancel()
}
if (!opendiscord.permissions.hasPermissions("admin",await opendiscord.permissions.getPermissions(instance.user,instance.channel,instance.guild))){
//no permissions
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 "custom"|"preset"
if (scope == "custom"){
const embedChannel = instance.options.getChannel("channel",true) as discord.GuildTextBasedChannel
const customColor = (instance.options.getString("color",true) as `#${string}`).replace("%CONFIG_COLOR%",generalConfig.data.mainColor.toString()) as `#${string}`
const title = instance.options.getString("title",false) ?? ""
const description = instance.options.getString("description",false) ?? ""
const footerText = instance.options.getString("footer",false) ?? ""
const authorText = instance.options.getString("author",false) ?? ""
const timestamp = instance.options.getBoolean("timestamp",false) ?? false
const image = instance.options.getString("image",false) ?? ""
const thumbnail = instance.options.getString("thumbnail",false) ?? ""
const ping = instance.options.getMentionable("ping",false)
const pingCustom = ping ? [ping.id] : []
await embedChannel.send((await opendiscord.builders.messages.getSafe("ot-embeds:embed-message").build(source,{embed:{
id:"_CUSTOM_",
content:"",
title,
description,
customColor,
image,
thumbnail,
authorText,
authorImage:"",
footerText,
footerImage:"",
timestamp,
fields:[],
ping:{
"@here":false,
"@everyone":false,
custom:pingCustom
}
}})).message)
}else if (scope == "preset"){
const embedChannel = instance.options.getChannel("channel",true) as discord.GuildTextBasedChannel
const embedId = instance.options.getString("id",true)
const embed = embedManager.get(embedId)
if (!embed){
instance.reply(await opendiscord.builders.messages.getSafe("opendiscord:error").build(source,{guild,channel,user,layout:"simple",error:"Invalid embed id. Please try again!"}))
return cancel()
}
await embedChannel.send((await opendiscord.builders.messages.getSafe("ot-embeds:embed-message").build(source,{embed:{
id:embed.id.value,
content:embed.data.content,
title:embed.data.title,
description:embed.data.description,
customColor:embed.data.customColor,
image:embed.data.image,
thumbnail:embed.data.thumbnail,
authorText:embed.data.authorText,
authorImage:embed.data.authorImage,
footerText:embed.data.footerText,
footerImage:embed.data.footerImage,
timestamp:embed.data.timestamp,
fields:embed.data.fields,
ping:embed.data.ping
}})).message)
}
//reply
await instance.reply(await opendiscord.builders.messages.getSafe("ot-embeds:success-message").build(source,{}))
}),
new api.ODWorker("ot-embeds:logs",-1,(instance,params,source,cancel) => {
const scope = instance.options.getSubCommand() as "custom"|"preset"
opendiscord.log(instance.user.displayName+" used the 'embed "+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}
])
})
])
})
//STARTUP SCREEN
opendiscord.events.get("onStartScreenLoad").listen((startscreen) => {
const embedManager = opendiscord.plugins.classes.get("ot-embeds:manager")
const stats = startscreen.get("opendiscord:stats")
if (!stats) return
//insert embeds startup info before "help" stat.
const newProperties = [
...stats.properties.slice(0,5),
{key:"embeds",value:"loaded "+ansis.bold(embedManager.getLength().toString())+" embeds!"},
...stats.properties.slice(5)
]
stats.properties = newProperties
})