Finished Message States & Overhauled Verifybars
This commit is contained in:
+184
-66
@@ -3,8 +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(){
|
||||
//UNPIN COMMAND RESPONDER
|
||||
@@ -12,46 +15,36 @@ export async function registerCommandResponders(){
|
||||
opendiscord.responders.commands.get("opendiscord:unpin").workers.add([
|
||||
new api.ODWorker("opendiscord:unpin",0,async (instance,params,origin,cancel) => {
|
||||
const {user,member,channel,guild} = instance
|
||||
|
||||
//check permissions
|
||||
const permsResult = await opendiscord.permissions.checkCommandPerms(generalConfig.data.system.permissions.unpin,"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 not pinned yet
|
||||
if (!ticket.get("opendiscord:pinned").value){
|
||||
instance.reply(await opendiscord.builders.messages.getSafe("opendiscord:error").build("button",{guild,channel,user,error:opendiscord.languages.getTranslation("errors.actionInvalid.unpin"),layout:"simple"}))
|
||||
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()
|
||||
}
|
||||
|
||||
const reason = instance.options.getString("reason",false)
|
||||
|
||||
//start unpinning ticket
|
||||
//responder checks
|
||||
const hasPerms = await actionUtils.replyHasPermissions(instance,origin,"unpin")
|
||||
if (!hasPerms) 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 isTicketPinned = await actionUtils.replyTicketMustBePinned(instance,origin,ticket)
|
||||
if (!isTicketPinned) return cancel()
|
||||
|
||||
//start unpining ticket
|
||||
await instance.defer(false)
|
||||
const reason = instance.options.getString("reason",false)
|
||||
await opendiscord.actions.get("opendiscord:unpin-ticket").run(origin,{guild,channel,user,ticket,reason,sendMessage:false})
|
||||
await instance.reply(await opendiscord.builders.messages.getSafe("opendiscord:unpin-message").build(origin,{guild,channel,user,ticket,reason}))
|
||||
|
||||
//send message & set state
|
||||
const sentMsg = await instance.reply(await opendiscord.builders.messages.getSafe("opendiscord:unpin-message").build(origin,{guild,channel,user,ticket,reason}))
|
||||
if (sentMsg.success) await interactiveMsgState.setMsgState({channel,message:sentMsg.message},{
|
||||
messageType:"unpin-message",
|
||||
messageOrigin:origin,
|
||||
messageAuthor:user.id,
|
||||
messageReason:reason
|
||||
},sentMsg.ephemeral)
|
||||
}),
|
||||
new api.ODWorker("opendiscord:logs",-1,(instance,params,origin,cancel) => {
|
||||
opendiscord.log(instance.user.displayName+" used the 'unpin' command!","info",[
|
||||
@@ -69,48 +62,173 @@ export async function registerButtonResponders(){
|
||||
opendiscord.responders.buttons.add(new api.ODButtonResponder("opendiscord:unpin-ticket",/^od:unpin-ticket/))
|
||||
opendiscord.responders.buttons.get("opendiscord:unpin-ticket").workers.add(
|
||||
new api.ODWorker("opendiscord:unpin-ticket",0,async (instance,params,origin,cancel) => {
|
||||
const originalOrigin = instance.interaction.customId.split("_")[1] as Exclude<api.ODActionManagerIdMappings["opendiscord:unpin-ticket"]["origin"],"slash"|"text">
|
||||
const {guild,channel,user,message} = instance
|
||||
|
||||
//responder checks
|
||||
const hasPerms = await actionUtils.replyHasPermissions(instance,origin,"unpin")
|
||||
if (!hasPerms) return cancel()
|
||||
|
||||
if (originalOrigin == "ticket-message") await opendiscord.verifybars.get("opendiscord:unpin-ticket-ticket-message").activate(instance)
|
||||
else if (originalOrigin == "pin-message") await opendiscord.verifybars.get("opendiscord:unpin-ticket-pin-message").activate(instance)
|
||||
else await instance.defer("update",false)
|
||||
const isInGuild = await actionUtils.replyIsInGuild(instance,origin)
|
||||
if (!isInGuild || !guild || channel.isDMBased()) return cancel()
|
||||
|
||||
const state = await actionUtils.replyInteractiveMessageState(instance,origin,channel,message,"/unpin")
|
||||
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 isTicketPinned = await actionUtils.replyTicketMustBePinned(instance,origin,ticket)
|
||||
if (!isTicketPinned) return cancel()
|
||||
|
||||
//fetch state details
|
||||
const verifybar = opendiscord.verifybars.get("opendiscord:unpin-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:unpin-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 == "pin-message"){
|
||||
//pin message verifybar
|
||||
const modifiedMsg = opendiscord.components.modifiers.get("opendiscord:unpin-ticket-verifybar").modify(opendiscord.builders.messages.getSafe("opendiscord:pin-message"),originalMsgType,{guild,channel,user,verifybar})
|
||||
await instance.update(await modifiedMsg.build("verifybar",{guild,channel,user:originalUser,ticket,reason:originalReason}))
|
||||
}
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
export async function registerVerifyBars(){
|
||||
//UNPIN TICKET
|
||||
opendiscord.verifybars.add(new api.ODVerifyBar("opendiscord:unpin-ticket"))
|
||||
opendiscord.verifybars.get("opendiscord:unpin-ticket").workers.add([
|
||||
new api.ODWorker("opendiscord:unpin-ticket",0,async (instance,params,origin,cancel) => {
|
||||
const {user,member,channel,guild,message} = instance
|
||||
|
||||
//responder checks
|
||||
const hasPerms = await actionUtils.replyHasPermissions(instance,origin,"unpin")
|
||||
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,"/unpin")
|
||||
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 isTicketPinned = await actionUtils.replyTicketMustBePinned(instance,origin,ticket)
|
||||
if (!isTicketPinned) 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 unpinning 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 == "pin-message"){
|
||||
//pin message
|
||||
await instance.update(await opendiscord.builders.messages.getSafe("opendiscord:pin-message").build("verifybar",{guild,channel,user:originalUser,ticket,reason:originalReason}))
|
||||
}
|
||||
}else if (params.selectedButtonId == "accept"){
|
||||
//UNPIN TICKET
|
||||
await instance.defer("update",false)
|
||||
|
||||
if (originalMsgType == "ticket-message"){
|
||||
//ticket message
|
||||
await opendiscord.actions.get("opendiscord:unpin-ticket").run(originalMsgType,{guild,channel,user,ticket,reason:null,sendMessage:true})
|
||||
await instance.update(await opendiscord.builders.messages.getSafe("opendiscord:ticket-message").build("verifybar",{guild,channel,user,ticket}))
|
||||
}else if (originalMsgType == "pin-message"){
|
||||
//converted to unpin message
|
||||
await opendiscord.actions.get("opendiscord:unpin-ticket").run(originalMsgType,{guild,channel,user,ticket,reason:null,sendMessage:false})
|
||||
const sentMsg = await instance.update(await opendiscord.builders.messages.getSafe("opendiscord:unpin-message").build("verifybar",{guild,channel,user,ticket,reason:null}))
|
||||
if (sentMsg.success) await interactiveMsgState.setMsgState({channel,message:sentMsg.message},{
|
||||
messageType:"unpin-message",
|
||||
messageOrigin:origin,
|
||||
messageAuthor:user.id,
|
||||
messageReason:null
|
||||
},sentMsg.ephemeral)
|
||||
}
|
||||
}else if (params.selectedButtonId == "accept-with-reason"){
|
||||
//UNPIN WITH REASON (MODAL)
|
||||
instance.modal(await opendiscord.builders.modals.getSafe("opendiscord:unpin-ticket-reason").build("other",{guild,channel,user,ticket,message}))
|
||||
}
|
||||
})
|
||||
])
|
||||
}
|
||||
|
||||
export async function registerModalResponders(){
|
||||
//UNPIN WITH REASON MODAL RESPONDER
|
||||
opendiscord.responders.modals.add(new api.ODModalResponder("opendiscord:unpin-ticket-reason",/^od:unpin-ticket-reason_/))
|
||||
opendiscord.responders.modals.add(new api.ODModalResponder("opendiscord:unpin-ticket-reason",/^od:unpin-ticket-reason\|([^|]+)\|([^|]+)/))
|
||||
opendiscord.responders.modals.get("opendiscord:unpin-ticket-reason").workers.add([
|
||||
new api.ODWorker("opendiscord:unpin-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 match = /^od:unpin-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,"unpin")
|
||||
if (!hasPerms) return cancel()
|
||||
|
||||
const isInGuild = await actionUtils.replyIsInGuild(instance,origin)
|
||||
if (!isInGuild || !guild || !channel || channel.isDMBased()) return cancel()
|
||||
|
||||
const originalOrigin = instance.interaction.customId.split("_")[2] as Exclude<api.ODActionManagerIdMappings["opendiscord:unpin-ticket"]["origin"],"slash"|"text">
|
||||
const state = await actionUtils.replyInteractiveMessageState(instance,origin,channel,message,"/unpin")
|
||||
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 isTicketPinned = await actionUtils.replyTicketMustBePinned(instance,origin,ticket)
|
||||
if (!isTicketPinned) return cancel()
|
||||
|
||||
//fetch state details
|
||||
const reason = instance.values.getTextField("reason",true)
|
||||
const originalMsgOrigin = state.data.messageOrigin
|
||||
const originalMsgType = state.data.messageType
|
||||
|
||||
//unpin with reason
|
||||
if (originalOrigin == "ticket-message"){
|
||||
await instance.defer("update",false)
|
||||
await opendiscord.actions.get("opendiscord:unpin-ticket").run(originalOrigin,{guild,channel,user,ticket,reason,sendMessage:true})
|
||||
//start pinning ticket
|
||||
await instance.defer("update",false)
|
||||
|
||||
if (originalMsgType == "ticket-message"){
|
||||
//ticket message
|
||||
await opendiscord.actions.get("opendiscord:unpin-ticket").run(originalMsgType,{guild,channel,user,ticket,reason:null,sendMessage:true})
|
||||
await instance.update(await opendiscord.builders.messages.getSafe("opendiscord:ticket-message").build("other",{guild,channel,user,ticket}))
|
||||
}else if (originalOrigin == "pin-message"){
|
||||
await instance.defer("update",false)
|
||||
await opendiscord.actions.get("opendiscord:unpin-ticket").run(originalOrigin,{guild,channel,user,ticket,reason,sendMessage:false})
|
||||
await instance.update(await opendiscord.builders.messages.getSafe("opendiscord:unpin-message").build("other",{guild,channel,user,ticket,reason}))
|
||||
}else{
|
||||
await instance.defer("update",false)
|
||||
await opendiscord.actions.get("opendiscord:unpin-ticket").run(originalOrigin,{guild,channel,user,ticket,reason,sendMessage:true})
|
||||
|
||||
}else if (originalMsgType == "pin-message"){
|
||||
//converted to unpin message
|
||||
await opendiscord.actions.get("opendiscord:unpin-ticket").run(originalMsgType,{guild,channel,user,ticket,reason:null,sendMessage:false})
|
||||
const sentMsg = await instance.update(await opendiscord.builders.messages.getSafe("opendiscord:unpin-message").build(originalMsgType,{guild,channel,user,ticket,reason}))
|
||||
if (sentMsg.success) await interactiveMsgState.setMsgState({channel,message:sentMsg.message},{
|
||||
messageType:"unpin-message",
|
||||
messageOrigin:originalMsgOrigin,
|
||||
messageAuthor:user.id,
|
||||
messageReason:reason
|
||||
},sentMsg.ephemeral)
|
||||
}
|
||||
})
|
||||
])
|
||||
|
||||
Reference in New Issue
Block a user