Finished Message States & Overhauled Verifybars

This commit is contained in:
DJj123dj
2026-05-14 21:39:50 +02:00
parent 67f4417ee6
commit 1a530aae4c
30 changed files with 1917 additions and 2549 deletions
+209 -87
View File
@@ -3,9 +3,11 @@
///////////////////////////////////////
import {opendiscord, api, utilities} from "../index.js"
import * as discord from "discord.js"
import * as actionUtils from "../actions/utilities.js"
const generalConfig = opendiscord.configs.get("opendiscord:general")
const lang = opendiscord.languages
const interactiveMsgState = opendiscord.states.get("opendiscord:interactive-message")
export async function registerCommandResponders(){
//DELETE COMMAND RESPONDER
@@ -14,50 +16,27 @@ export async function registerCommandResponders(){
new api.ODWorker("opendiscord:delete",0,async (instance,params,origin,cancel) => {
const {user,member,channel,guild} = instance
//check permissions
const permsResult = await opendiscord.permissions.checkCommandPerms(generalConfig.data.system.permissions.delete,"support",user,member,channel,guild)
if (!permsResult.hasPerms){
if (permsResult.reason == "not-in-server") await instance.reply(await opendiscord.builders.messages.getSafe("opendiscord:error-not-in-guild").build("button",{channel,user}))
else await instance.reply(await opendiscord.builders.messages.getSafe("opendiscord:error-no-permissions").build(origin,{guild,channel,user,permissions:["support"]}))
return cancel()
}
//check is in guild/server
if (!guild){
instance.reply(await opendiscord.builders.messages.getSafe("opendiscord:error-not-in-guild").build("button",{channel,user}))
return cancel()
}
//check if ticket exists
const ticket = opendiscord.tickets.get(channel.id)
if (!ticket || channel.isDMBased()){
instance.reply(await opendiscord.builders.messages.getSafe("opendiscord:error-ticket-unknown").build("button",{guild,channel,user}))
return cancel()
}
//return when busy
if (ticket.get("opendiscord:busy").value){
instance.reply(await opendiscord.builders.messages.getSafe("opendiscord:error-ticket-busy").build("button",{guild,channel,user}))
return cancel()
}
//responder checks
const hasPerms = await actionUtils.replyHasPermissions(instance,origin,"delete")
if (!hasPerms) return cancel()
//return when not allowed because of missing messages
if (!permsResult.isAdmin && (!generalConfig.data.system.allowCloseBeforeMessage || !generalConfig.data.system.allowCloseBeforeAdminMessage)){
const analysis = await opendiscord.transcripts.collector.ticketUserMessagesAnalysis(ticket,guild,channel)
if (analysis && !generalConfig.data.system.allowCloseBeforeMessage && analysis.totalMessages < 1){
instance.reply(await opendiscord.builders.messages.getSafe("opendiscord:error").build("button",{guild,channel,user,layout:"simple",error:lang.getTranslation("errors.descriptions.closeBeforeMessage"),customTitle:lang.getTranslation("errors.titles.noPermissions")}))
return cancel()
}
if (analysis && !generalConfig.data.system.allowCloseBeforeAdminMessage && analysis.adminMessages < 1){
instance.reply(await opendiscord.builders.messages.getSafe("opendiscord:error").build("button",{guild,channel,user,layout:"simple",error:lang.getTranslation("errors.descriptions.closeBeforeAdminMessage"),customTitle:lang.getTranslation("errors.titles.noPermissions")}))
return cancel()
}
}
const isInGuild = await actionUtils.replyIsInGuild(instance,origin)
if (!isInGuild || !guild || channel.isDMBased()) return cancel()
const ticket = await actionUtils.replyIsTicket(instance,origin)
if (!ticket) return cancel()
const isAvailable = await actionUtils.replyTicketIsAvailable(instance,origin,ticket)
if (!isAvailable) return cancel()
const isTicketOpen = await actionUtils.replyTicketMustBeOpen(instance,origin,ticket)
if (!isTicketOpen) return cancel()
const reason = instance.options.getString("reason",false)
const withoutTranscript = instance.options.getBoolean("notranscript",false) ?? false
const messagesHaveBeenSent = await actionUtils.replyMessageMustBeSentBeforeClose(instance,origin,ticket,"delete")
if (!messagesHaveBeenSent) return cancel()
//don't allow deleteWithoutTranscript to non-global-admins when enabled
const withoutTranscript = instance.options.getBoolean("notranscript",false) ?? false
if (withoutTranscript && generalConfig.data.system.adminOnlyDeleteWithoutTranscript){
if (!opendiscord.permissions.hasPermissions("support",await opendiscord.permissions.getPermissions(instance.user,instance.channel,instance.guild,{allowChannelRoleScope:false,allowChannelUserScope:false,allowGlobalRoleScope:true,allowGlobalUserScope:true}))){
instance.reply(await opendiscord.builders.messages.getSafe("opendiscord:error-no-permissions").build("button",{guild:instance.guild,channel:instance.channel,user:instance.user,permissions:["support"]}))
@@ -67,6 +46,7 @@ export async function registerCommandResponders(){
//start deleting ticket
await instance.defer(false)
const reason = instance.options.getString("reason",false)
await instance.reply(await opendiscord.builders.messages.getSafe("opendiscord:delete-message").build(origin,{guild,channel,user,ticket,reason}))
await opendiscord.actions.get("opendiscord:delete-ticket").run(origin,{guild,channel,user,ticket,reason,sendMessage:false,withoutTranscript})
}),
@@ -86,65 +66,207 @@ export async function registerButtonResponders(){
opendiscord.responders.buttons.add(new api.ODButtonResponder("opendiscord:delete-ticket",/^od:delete-ticket/))
opendiscord.responders.buttons.get("opendiscord:delete-ticket").workers.add(
new api.ODWorker("opendiscord:delete-ticket",0,async (instance,params,origin,cancel) => {
const originalOrigin = instance.interaction.customId.split("_")[1] as Exclude<api.ODActionManagerIdMappings["opendiscord:delete-ticket"]["origin"],"slash"|"text"|"autodelete"|"clear">
const {guild,channel,message,user,member} = instance
if (originalOrigin == "ticket-message") await opendiscord.verifybars.get("opendiscord:delete-ticket-ticket-message").activate(instance)
else if (originalOrigin == "close-message") await opendiscord.verifybars.get("opendiscord:delete-ticket-close-message").activate(instance)
else if (originalOrigin == "reopen-message") await opendiscord.verifybars.get("opendiscord:delete-ticket-reopen-message").activate(instance)
else if (originalOrigin == "autoclose-message") await opendiscord.verifybars.get("opendiscord:delete-ticket-autoclose-message").activate(instance)
else await instance.defer("update",false)
//responder checks
const hasPerms = await actionUtils.replyHasPermissions(instance,origin,"delete")
if (!hasPerms) return cancel()
const isInGuild = await actionUtils.replyIsInGuild(instance,origin)
if (!isInGuild || !guild || channel.isDMBased()) return cancel()
const state = await actionUtils.replyInteractiveMessageState(instance,origin,channel,message,"/delete")
if (!state) return cancel()
const ticket = await actionUtils.replyIsTicket(instance,origin)
if (!ticket) return cancel()
const isAvailable = await actionUtils.replyTicketIsAvailable(instance,origin,ticket)
if (!isAvailable) return cancel()
const messagesHaveBeenSent = await actionUtils.replyMessageMustBeSentBeforeClose(instance,origin,ticket,"delete")
if (!messagesHaveBeenSent) return cancel()
//fetch state details
const verifybar = opendiscord.verifybars.get("opendiscord:delete-ticket")
const originalUser = ((state.data.messageAuthor) ? await opendiscord.client.fetchUser(state.data.messageAuthor) : user) ?? user
const originalReason = state.data.messageReason ?? null
const originalMsgType = state.data.messageType
//send verifybar
if (generalConfig.data.system.disableVerifyBars){
//verifybar disabled, directly run response
await verifybar.activate(instance,"accept")
}else if (originalMsgType == "ticket-message"){
//ticket message verifybar
const modifiedMsg = opendiscord.components.modifiers.get("opendiscord:delete-ticket-verifybar").modify(opendiscord.builders.messages.getSafe("opendiscord:ticket-message"),originalMsgType,{guild,channel,user,verifybar})
await instance.update(await modifiedMsg.build("verifybar",{guild,channel,user,ticket}))
}else if (originalMsgType == "close-message"){
//close message verifybar
const modifiedMsg = opendiscord.components.modifiers.get("opendiscord:delete-ticket-verifybar").modify(opendiscord.builders.messages.getSafe("opendiscord:close-message"),originalMsgType,{guild,channel,user,verifybar})
await instance.update(await modifiedMsg.build("verifybar",{guild,channel,user:originalUser,ticket,reason:originalReason}))
}else if (originalMsgType == "reopen-message"){
//reopen message verifybar
const modifiedMsg = opendiscord.components.modifiers.get("opendiscord:delete-ticket-verifybar").modify(opendiscord.builders.messages.getSafe("opendiscord:reopen-message"),originalMsgType,{guild,channel,user,verifybar})
await instance.update(await modifiedMsg.build("verifybar",{guild,channel,user:originalUser,ticket,reason:originalReason}))
}else if (originalMsgType == "autoclose-message"){
//autoclose message verifybar
const modifiedMsg = opendiscord.components.modifiers.get("opendiscord:delete-ticket-verifybar").modify(opendiscord.builders.messages.getSafe("opendiscord:autoclose-message"),originalMsgType,{guild,channel,user,verifybar})
await instance.update(await modifiedMsg.build("verifybar",{guild,channel,user:originalUser,ticket}))
}
})
)
}
export async function registerVerifyBars(){
//DELETE TICKET
opendiscord.verifybars.add(new api.ODVerifyBar("opendiscord:delete-ticket"))
opendiscord.verifybars.get("opendiscord:delete-ticket").workers.add([
new api.ODWorker("opendiscord:delete-ticket",0,async (instance,params,origin,cancel) => {
const {user,member,channel,guild,message} = instance
//responder checks
const hasPerms = await actionUtils.replyHasPermissions(instance,origin,"delete")
if (!hasPerms) return cancel()
const isInGuild = await actionUtils.replyIsInGuild(instance,origin)
if (!isInGuild || !guild || channel.isDMBased()) return cancel()
const state = await actionUtils.replyInteractiveMessageState(instance,origin,channel,message,"/delete")
if (!state) return cancel()
const ticket = await actionUtils.replyIsTicket(instance,origin)
if (!ticket) return cancel()
const isAvailable = await actionUtils.replyTicketIsAvailable(instance,origin,ticket)
if (!isAvailable) return cancel()
const messagesHaveBeenSent = await actionUtils.replyMessageMustBeSentBeforeClose(instance,origin,ticket,"delete")
if (!messagesHaveBeenSent) return cancel()
//don't allow deleteWithoutTranscript to non-global-admins when enabled
const withoutTranscript = (params.selectedButtonId == "accept-without-transcript")
if (withoutTranscript && generalConfig.data.system.adminOnlyDeleteWithoutTranscript){
if (!opendiscord.permissions.hasPermissions("support",await opendiscord.permissions.getPermissions(instance.user,instance.channel,instance.guild,{allowChannelRoleScope:false,allowChannelUserScope:false,allowGlobalRoleScope:true,allowGlobalUserScope:true}))){
instance.reply(await opendiscord.builders.messages.getSafe("opendiscord:error-no-permissions").build("button",{guild:instance.guild,channel:instance.channel,user:instance.user,permissions:["support"]}))
return cancel()
}
}
//fetch state details
const originalUser = ((state.data.messageAuthor) ? await opendiscord.client.fetchUser(state.data.messageAuthor) : user) ?? user
const originalReason = state.data.messageReason ?? null
const originalMsgType = state.data.messageType
//start deleting ticket
if (params.selectedButtonId == "cancel"){
//CANCEL
if (originalMsgType == "ticket-message"){
//ticket message
await instance.update(await opendiscord.builders.messages.getSafe("opendiscord:ticket-message").build("verifybar",{guild,channel,user,ticket}))
}else if (originalMsgType == "close-message"){
//close message
await instance.update(await opendiscord.builders.messages.getSafe("opendiscord:close-message").build("verifybar",{guild,channel,user:originalUser,ticket,reason:originalReason}))
}else if (originalMsgType == "reopen-message"){
//reopen message
await instance.update(await opendiscord.builders.messages.getSafe("opendiscord:reopen-message").build("verifybar",{guild,channel,user:originalUser,ticket,reason:originalReason}))
}else if (originalMsgType == "autoclose-message"){
//autoclose message
await instance.update(await opendiscord.builders.messages.getSafe("opendiscord:autoclose-message").build("verifybar",{guild,channel,user:originalUser,ticket}))
}
}else if (params.selectedButtonId == "accept" || params.selectedButtonId == "accept-without-transcript"){
//DELETE TICKET
await instance.defer("update",false)
if (originalMsgType == "ticket-message"){
//ticket message
opendiscord.actions.get("opendiscord:delete-ticket").run(originalMsgType,{guild,channel,user,ticket,reason:null,sendMessage:true,withoutTranscript})
ticket.get("opendiscord:for-deletion").value = true //disable ticket message buttons
await instance.update(await opendiscord.builders.messages.getSafe("opendiscord:ticket-message").build("other",{guild,channel,user,ticket}))
}else if (originalMsgType == "close-message"){
//converted to delete message
opendiscord.actions.get("opendiscord:delete-ticket").run(originalMsgType,{guild,channel,user,ticket,reason:null,sendMessage:false,withoutTranscript})
await instance.update(await opendiscord.builders.messages.getSafe("opendiscord:delete-message").build(originalMsgType,{guild,channel,user,ticket,reason:null}))
}else if (originalMsgType == "reopen-message"){
//converted to delete message
opendiscord.actions.get("opendiscord:delete-ticket").run(originalMsgType,{guild,channel,user,ticket,reason:null,sendMessage:false,withoutTranscript})
await instance.update(await opendiscord.builders.messages.getSafe("opendiscord:delete-message").build(originalMsgType,{guild,channel,user,ticket,reason:null}))
}else if (originalMsgType == "autoclose-message"){
//converted to delete message
opendiscord.actions.get("opendiscord:delete-ticket").run(originalMsgType,{guild,channel,user,ticket,reason:null,sendMessage:false,withoutTranscript})
await instance.update(await opendiscord.builders.messages.getSafe("opendiscord:delete-message").build(originalMsgType,{guild,channel,user,ticket,reason:null}))
}
}else if (params.selectedButtonId == "accept-with-reason"){
//DELETE WITH REASON (MODAL)
instance.modal(await opendiscord.builders.modals.getSafe("opendiscord:delete-ticket-reason").build("other",{guild,channel,user,ticket,message}))
}
})
])
}
export async function registerModalResponders(){
//REOPEN WITH REASON MODAL RESPONDER
opendiscord.responders.modals.add(new api.ODModalResponder("opendiscord:delete-ticket-reason",/^od:delete-ticket-reason_/))
//DELETE WITH REASON MODAL RESPONDER
opendiscord.responders.modals.add(new api.ODModalResponder("opendiscord:delete-ticket-reason",/^od:delete-ticket-reason\|([^|]+)\|([^|]+)/))
opendiscord.responders.modals.get("opendiscord:delete-ticket-reason").workers.add([
new api.ODWorker("opendiscord:delete-ticket-reason",0,async (instance,params,origin,cancel) => {
const {guild,channel,user} = instance
if (!channel) return
if (!guild){
//error
instance.reply(await opendiscord.builders.messages.getSafe("opendiscord:error-not-in-guild").build(origin,{channel,user:instance.user}))
return cancel()
}
const ticket = opendiscord.tickets.get(instance.interaction.customId.split("_")[1])
if (!ticket || channel.isDMBased()){
instance.reply(await opendiscord.builders.messages.getSafe("opendiscord:error-ticket-unknown").build("button",{guild,channel,user}))
return
}
const {guild,user} = instance
const originalOrigin = instance.interaction.customId.split("_")[2] as Exclude<api.ODActionManagerIdMappings["opendiscord:delete-ticket"]["origin"],"slash"|"text"|"autodelete"|"clear">
const match = /^od:delete-ticket-reason\|([^|]+)\|([^|]+)/.exec(instance.interaction.customId)
if (!match) return cancel()
const channel = await opendiscord.client.fetchTextChannel(match[1])
const message = await opendiscord.client.fetchChannelMessage(match[1],match[2])
//responder checks
const hasPerms = await actionUtils.replyHasPermissions(instance,origin,"delete")
if (!hasPerms) return cancel()
const isInGuild = await actionUtils.replyIsInGuild(instance,origin)
if (!isInGuild || !guild || !channel || channel.isDMBased()) return cancel()
const state = await actionUtils.replyInteractiveMessageState(instance,origin,channel,message,"/delete")
if (!state) return cancel()
const ticket = await actionUtils.replyIsTicket(instance,origin)
if (!ticket) return cancel()
const isAvailable = await actionUtils.replyTicketIsAvailable(instance,origin,ticket)
if (!isAvailable) return cancel()
//fetch state details
const reason = instance.values.getTextField("reason",true)
const originalMsgOrigin = state.data.messageOrigin
const originalMsgType = state.data.messageType
//delete with reason
if (originalOrigin == "ticket-message"){
await instance.defer("update",false)
//don't await DELETE action => else it will update the message after the channel has been deleted
opendiscord.actions.get("opendiscord:delete-ticket").run(originalOrigin,{guild,channel,user,ticket,reason,sendMessage:true,withoutTranscript:false})
//update ticket (for ticket message) => no-await doesn't wait for the action to set this variable
ticket.get("opendiscord:for-deletion").value = true
//start deleting ticket
//don't await DELETE action => else it will update the message after the channel has been deleted
await instance.defer("update",false)
if (originalMsgType == "ticket-message"){
//ticket message
opendiscord.actions.get("opendiscord:delete-ticket").run(originalMsgType,{guild,channel,user,ticket,reason,sendMessage:true,withoutTranscript:false})
ticket.get("opendiscord:for-deletion").value = true //disable ticket message buttons
await instance.update(await opendiscord.builders.messages.getSafe("opendiscord:ticket-message").build("other",{guild,channel,user,ticket}))
}else if (originalOrigin == "close-message"){
await instance.defer("update",false)
//don't await DELETE action => else it will update the message after the channel has been deleted
opendiscord.actions.get("opendiscord:delete-ticket").run(originalOrigin,{guild,channel,user,ticket,reason,sendMessage:false,withoutTranscript:false})
await instance.update(await opendiscord.builders.messages.getSafe("opendiscord:delete-message").build("other",{guild,channel,user,ticket,reason}))
}else if (originalOrigin == "reopen-message"){
await instance.defer("update",false)
//don't await DELETE action => else it will update the message after the channel has been deleted
opendiscord.actions.get("opendiscord:delete-ticket").run(originalOrigin,{guild,channel,user,ticket,reason,sendMessage:false,withoutTranscript:false})
await instance.update(await opendiscord.builders.messages.getSafe("opendiscord:delete-message").build("other",{guild,channel,user,ticket,reason}))
}else if (originalOrigin == "autoclose-message"){
await instance.defer("update",false)
//don't await DELETE action => else it will update the message after the channel has been deleted
opendiscord.actions.get("opendiscord:delete-ticket").run(originalOrigin,{guild,channel,user,ticket,reason,sendMessage:false,withoutTranscript:false})
await instance.update(await opendiscord.builders.messages.getSafe("opendiscord:delete-message").build("other",{guild,channel,user,ticket,reason}))
}else{
await instance.defer("update",false)
//don't await DELETE action => else it will update the message after the channel has been deleted
opendiscord.actions.get("opendiscord:delete-ticket").run(originalOrigin,{guild,channel,user,ticket,reason,sendMessage:true,withoutTranscript:false})
}else if (originalMsgType == "close-message"){
//converted to delete message
opendiscord.actions.get("opendiscord:delete-ticket").run(originalMsgType,{guild,channel,user,ticket,reason,sendMessage:false,withoutTranscript:false})
await instance.update(await opendiscord.builders.messages.getSafe("opendiscord:delete-message").build(originalMsgType,{guild,channel,user,ticket,reason}))
}else if (originalMsgType == "reopen-message"){
//converted to delete message
opendiscord.actions.get("opendiscord:delete-ticket").run(originalMsgType,{guild,channel,user,ticket,reason,sendMessage:false,withoutTranscript:false})
await instance.update(await opendiscord.builders.messages.getSafe("opendiscord:delete-message").build(originalMsgType,{guild,channel,user,ticket,reason}))
}else if (originalMsgType == "autoclose-message"){
//converted to delete message
opendiscord.actions.get("opendiscord:delete-ticket").run(originalMsgType,{guild,channel,user,ticket,reason,sendMessage:false,withoutTranscript:false})
await instance.update(await opendiscord.builders.messages.getSafe("opendiscord:delete-message").build(originalMsgType,{guild,channel,user,ticket,reason}))
}
})
])