diff --git a/src/actions/calculateTicketCategory.ts b/src/actions/calculateTicketCategory.ts new file mode 100644 index 0000000..94b69ef --- /dev/null +++ b/src/actions/calculateTicketCategory.ts @@ -0,0 +1,127 @@ +/////////////////////////////////////// +//CALCULATE TICKET CATEGORY SYSTEM +/////////////////////////////////////// +import {opendiscord, api, utilities} from "../index.js" +import * as discord from "discord.js" + +const generalConfig = opendiscord.configs.get("opendiscord:general") + +export async function registerActions(){ + opendiscord.actions.add(new api.ODAction("opendiscord:calculate-ticket-category")) + opendiscord.actions.get("opendiscord:calculate-ticket-category").workers.add([ + new api.ODWorker("opendiscord:default-category",2,async (instance,params,origin,cancel) => { + //handle default category + const {guild,user,channel,option,ticket,currentCategoryId} = params + + const defaultCategoryId = option.get("opendiscord:channel-category").value + if (!defaultCategoryId){ + //default category is disabled + instance.newCategoryId = null + instance.newCategoryMode = null + instance.newCategory = null + instance.shouldChangeCategory = (instance.newCategoryId !== currentCategoryId) + }else{ + const defaultCategory = await opendiscord.client.fetchGuildCategoryChannel(guild,defaultCategoryId) + if (defaultCategory){ + //default category is enabled + instance.newCategoryId = defaultCategoryId + instance.newCategoryMode = "default" + instance.newCategory = defaultCategory + instance.shouldChangeCategory = (instance.newCategoryId !== currentCategoryId) + }else{ + //default category is not found (do not switch categories) + opendiscord.log("Unable to find ticket category '"+defaultCategoryId+"' #1","error",[ + {key:"categoryid",value:defaultCategoryId}, + {key:"type",value:"default"} + ]) + instance.newCategoryId = null + instance.newCategoryMode = null + instance.newCategory = null + instance.shouldChangeCategory = false + } + } + }), + new api.ODWorker("opendiscord:close-category",1,async (instance,params,origin,cancel) => { + //handle close category + const {guild,user,channel,option,ticket,currentCategoryId} = params + if (!ticket) return + if (!ticket.get("opendiscord:closed").value) return + + const closeCategoryId = option.get("opendiscord:channel-category-closed").value + if (!closeCategoryId) return + const closeCategory = await opendiscord.client.fetchGuildCategoryChannel(guild,closeCategoryId) + if (closeCategory){ + //close category is enabled + instance.newCategoryId = closeCategoryId + instance.newCategoryMode = "close" + instance.newCategory = closeCategory + instance.shouldChangeCategory = (instance.newCategoryId !== currentCategoryId) + }else{ + //close category is not found (do not switch categories) + opendiscord.log("Unable to find ticket category '"+closeCategoryId+"' #2","error",[ + {key:"categoryid",value:closeCategoryId}, + {key:"type",value:"close"} + ]) + instance.newCategoryId = null + instance.newCategoryMode = null + instance.newCategory = null + instance.shouldChangeCategory = false + } + }), + new api.ODWorker("opendiscord:claim-category",0,async (instance,params,origin,cancel) => { + //handle claim category + const {guild,user,channel,option,ticket,currentCategoryId} = params + if (!ticket) return + if (!ticket.get("opendiscord:claimed").value) return + + const claimedCategoryIds = option.get("opendiscord:channel-categories-claimed").value + const claimCategoryId = claimedCategoryIds.find((c) => c.user == user.id)?.category + if (!claimCategoryId) return + const claimCategory = await opendiscord.client.fetchGuildCategoryChannel(guild,claimCategoryId) + if (claimCategory){ + //claim category is enabled + instance.newCategoryId = claimCategoryId + instance.newCategoryMode = "claim" + instance.newCategory = claimCategory + instance.shouldChangeCategory = (instance.newCategoryId !== currentCategoryId) + }else{ + //claim category is not found (do not switch categories) + opendiscord.log("Unable to find ticket category '"+claimCategoryId+"' #3","error",[ + {key:"categoryid",value:claimCategoryId}, + {key:"type",value:"claim"} + ]) + instance.newCategoryId = null + instance.newCategoryMode = null + instance.newCategory = null + instance.shouldChangeCategory = false + } + }), + new api.ODWorker("opendiscord:backup-category",-100,async (instance,params,origin,cancel) => { + //handle backup category + const {guild,user,channel,option,ticket,currentCategoryId} = params + if (!instance.newCategory || !instance.newCategoryId || !instance.shouldChangeCategory) return + if (instance.newCategory.children.cache.size < 50) return + + const backupCategoryId = option.get("opendiscord:channel-category-backup").value + if (!backupCategoryId) return + const backupCategory = await opendiscord.client.fetchGuildCategoryChannel(guild,backupCategoryId) + if (backupCategory){ + //backup category is enabled + instance.newCategoryId = backupCategoryId + instance.newCategoryMode = "backup" + instance.newCategory = backupCategory + instance.shouldChangeCategory = (instance.newCategoryId !== currentCategoryId) + }else{ + //backup category is not found (do not switch categories) + opendiscord.log("Unable to find ticket category '"+backupCategoryId+"' #4","error",[ + {key:"categoryid",value:backupCategoryId}, + {key:"type",value:"backup"} + ]) + instance.newCategoryId = null + instance.newCategoryMode = null + instance.newCategory = null + instance.shouldChangeCategory = false + } + }) + ]) +} \ No newline at end of file diff --git a/src/actions/claimTicket.ts b/src/actions/claimTicket.ts index 7d886fc..8eed7c2 100644 --- a/src/actions/claimTicket.ts +++ b/src/actions/claimTicket.ts @@ -26,22 +26,25 @@ export async function registerActions(){ await opendiscord.statistics.get("opendiscord:global").setStat("opendiscord:tickets-claimed",1,"increase") await opendiscord.statistics.get("opendiscord:user").setStat("opendiscord:tickets-claimed",user.id,1,"increase") - //update category + //calculate & update category if (typeof params.allowCategoryChange == "boolean" ? params.allowCategoryChange : true){ - const rawClaimCategory = ticket.option.get("opendiscord:channel-categories-claimed").value.find((c) => c.user == user.id) - const claimCategory = (rawClaimCategory) ? rawClaimCategory.category : null - if (claimCategory){ - try { - channel.setParent(claimCategory,{lockPermissions:false}) - ticket.get("opendiscord:category-mode").value = "claimed" - ticket.get("opendiscord:category").value = claimCategory - }catch(e){ - opendiscord.log("Unable to move ticket to 'claimed category'!","error",[ + const categoryResult = await opendiscord.actions.get("opendiscord:calculate-ticket-category").run("claim-ticket",{guild,user,option:ticket.option,channel,ticket,currentCategoryId:channel.parentId}) + if (categoryResult && categoryResult.shouldChangeCategory && typeof categoryResult.newCategoryId !== "undefined" && typeof categoryResult.newCategoryMode !== "undefined" && typeof categoryResult.newCategory !== "undefined"){ + const originalCategoryName = channel.parent?.name ?? "" + const newCategoryName = categoryResult.newCategory?.name ?? "" + try{ + await utilities.timedAwait(channel.setParent(categoryResult.newCategoryId,{lockPermissions:false}),3000,(err) => { + process.emit("uncaughtException",new Error("Error: Unable to change channel parent: "+err)) + }) + ticket.get("opendiscord:category-mode").value = categoryResult.newCategoryMode + ticket.get("opendiscord:category").value = categoryResult.newCategoryId + }catch(err){ + await channel.send((await opendiscord.builders.messages.getSafe("opendiscord:error-channel-category").build("ticket-claim",{guild,channel,user,originalCategory:originalCategoryName,newCategory:newCategoryName})).message) + opendiscord.log("Unable to move ticket to claimed category.","error",[ {key:"channel",value:"#"+channel.name}, {key:"channelid",value:channel.id,hidden:true}, - {key:"categoryid",value:claimCategory} + {key:"categoryid",value:categoryResult.newCategoryId ?? "/"} ]) - opendiscord.debugfile.writeErrorMessage(new api.ODError(e,"uncaughtException")) } } } diff --git a/src/actions/closeTicket.ts b/src/actions/closeTicket.ts index 29d886f..7cc0279 100644 --- a/src/actions/closeTicket.ts +++ b/src/actions/closeTicket.ts @@ -34,21 +34,25 @@ export async function registerActions(){ await opendiscord.statistics.get("opendiscord:global").setStat("opendiscord:tickets-closed",1,"increase") await opendiscord.statistics.get("opendiscord:user").setStat("opendiscord:tickets-closed",user.id,1,"increase") - //update category + //calculate & update category if (typeof params.allowCategoryChange == "boolean" ? params.allowCategoryChange : true){ - const closeCategory = ticket.option.get("opendiscord:channel-category-closed").value - if (closeCategory !== ""){ - try { - channel.setParent(closeCategory,{lockPermissions:false}) - ticket.get("opendiscord:category-mode").value = "closed" - ticket.get("opendiscord:category").value = closeCategory - }catch(e){ - opendiscord.log("Unable to move ticket to 'closed category'!","error",[ + const categoryResult = await opendiscord.actions.get("opendiscord:calculate-ticket-category").run("close-ticket",{guild,user,option:ticket.option,channel,ticket,currentCategoryId:channel.parentId}) + if (categoryResult && categoryResult.shouldChangeCategory && typeof categoryResult.newCategoryId !== "undefined" && typeof categoryResult.newCategoryMode !== "undefined" && typeof categoryResult.newCategory !== "undefined"){ + const originalCategoryName = channel.parent?.name ?? "" + const newCategoryName = categoryResult.newCategory?.name ?? "" + try{ + await utilities.timedAwait(channel.setParent(categoryResult.newCategoryId,{lockPermissions:false}),3000,(err) => { + process.emit("uncaughtException",new Error("Error: Unable to change channel parent: "+err)) + }) + ticket.get("opendiscord:category-mode").value = categoryResult.newCategoryMode + ticket.get("opendiscord:category").value = categoryResult.newCategoryId + }catch(err){ + await channel.send((await opendiscord.builders.messages.getSafe("opendiscord:error-channel-category").build("ticket-close",{guild,channel,user,originalCategory:originalCategoryName,newCategory:newCategoryName})).message) + opendiscord.log("Unable to move ticket to closed category.","error",[ {key:"channel",value:"#"+channel.name}, {key:"channelid",value:channel.id,hidden:true}, - {key:"categoryid",value:closeCategory} + {key:"categoryid",value:categoryResult.newCategoryId ?? "/"} ]) - opendiscord.debugfile.writeErrorMessage(new api.ODError(e,"uncaughtException")) } } } diff --git a/src/actions/createTicket.ts b/src/actions/createTicket.ts index 7b580d9..e87a285 100644 --- a/src/actions/createTicket.ts +++ b/src/actions/createTicket.ts @@ -19,46 +19,15 @@ export async function registerActions(){ //get channel properties const channelPrefix = option.get("opendiscord:channel-prefix").value - const channelCategory = option.get("opendiscord:channel-category").value - const channelBackupCategory = option.get("opendiscord:channel-category-backup").value const channelTopicText = option.get("opendiscord:channel-topic").value const channelSuffix = await opendiscord.options.suffix.getSuffixFromOption(option,user,guild) const channelName = channelPrefix+channelSuffix - //handle category - let category: string|null = null - let categoryMode: "backup"|"normal"|null = null - if (channelCategory != ""){ - //category enabled - const normalCategory = await opendiscord.client.fetchGuildCategoryChannel(guild,channelCategory) - if (!normalCategory){ - //default category was not found - opendiscord.log("Ticket Creation Error: Unable to find category! #1","error",[ - {key:"categoryid",value:channelCategory}, - {key:"backup",value:"false"} - ]) - }else{ - //default category was found - if (normalCategory.children.cache.size >= 50 && channelBackupCategory != ""){ - //use backup category - const backupCategory = await opendiscord.client.fetchGuildCategoryChannel(guild,channelBackupCategory) - if (!backupCategory){ - //default category was not found - opendiscord.log("Ticket Creation Error: Unable to find category! #2","error",[ - {key:"categoryid",value:channelBackupCategory}, - {key:"backup",value:"true"} - ]) - }else{ - category = backupCategory.id - categoryMode = "backup" - } - }else{ - //use default category - category = normalCategory.id - categoryMode = "normal" - } - } - } + //calculate category + const categoryResult = await opendiscord.actions.get("opendiscord:calculate-ticket-category").run("create-ticket",{guild,user,option,channel:null,ticket:null,currentCategoryId:null}) + if (!categoryResult) return opendiscord.log("Ticket Creation Error: Unable to calculate ticket category.","error") + const ticketCategoryId = (categoryResult.shouldChangeCategory && typeof categoryResult.newCategoryId !== "undefined") ? categoryResult.newCategoryId : undefined + const ticketCategoryMode = (categoryResult.shouldChangeCategory && typeof categoryResult.newCategoryMode !== "undefined") ? categoryResult.newCategoryMode : undefined //handle permissions const permissions: discord.OverwriteResolvable[] = [{ @@ -135,7 +104,7 @@ export async function registerActions(){ name:channelName, nsfw:false, topic:(channelTopics.length > 0) ? channelTopics.join(" • ") : undefined, - parent:category, + parent:ticketCategoryId, reason:"Ticket Created By "+user.displayName, permissionOverwrites:permissions, rateLimitPerUser:slowMode @@ -168,8 +137,8 @@ export async function registerActions(){ new api.ODTicketData("opendiscord:pinned-on",null), new api.ODTicketData("opendiscord:for-deletion",false), - new api.ODTicketData("opendiscord:category",category), - new api.ODTicketData("opendiscord:category-mode",categoryMode), + new api.ODTicketData("opendiscord:category",ticketCategoryId ?? null), + new api.ODTicketData("opendiscord:category-mode",ticketCategoryMode ?? null), new api.ODTicketData("opendiscord:autoclose-enabled",option.get("opendiscord:autoclose-enable-hours").value), new api.ODTicketData("opendiscord:autoclose-hours",(option.get("opendiscord:autoclose-enable-hours").value ? option.get("opendiscord:autoclose-hours").value : 0)), diff --git a/src/actions/moveTicket.ts b/src/actions/moveTicket.ts index 2415da4..a31308e 100644 --- a/src/actions/moveTicket.ts +++ b/src/actions/moveTicket.ts @@ -23,72 +23,28 @@ export async function registerActions(){ //get new channel properties const channelPrefix = ticket.option.get("opendiscord:channel-prefix").value const channelSuffix = ticket.get("opendiscord:channel-suffix").value - const channelCategory = ticket.option.get("opendiscord:channel-category").value - const channelBackupCategory = ticket.option.get("opendiscord:channel-category-backup").value - const rawClaimCategory = ticket.option.get("opendiscord:channel-categories-claimed").value.find((c) => c.user == user.id) - const claimCategory = (rawClaimCategory) ? rawClaimCategory.category : null - const closeCategory = ticket.option.get("opendiscord:channel-category-closed").value - const channelTopic = ticket.option.get("opendiscord:channel-topic").value - //handle category - let category: string|null = null - let categoryMode: "backup"|"normal"|"closed"|"claimed"|null = null - if (claimCategory){ - //use claim category - category = claimCategory - categoryMode = "claimed" - }else if (closeCategory != "" && ticket.get("opendiscord:closed").value){ - //use close category - category = closeCategory - categoryMode = "closed" - }else if (channelCategory != ""){ - //category enabled - const normalCategory = await opendiscord.client.fetchGuildCategoryChannel(guild,channelCategory) - if (!normalCategory){ - //default category was not found - opendiscord.log("Ticket Move Error: Unable to find category! #1","error",[ - {key:"categoryid",value:channelCategory}, - {key:"backup",value:"false"} + //calculate & update category + const categoryResult = await opendiscord.actions.get("opendiscord:calculate-ticket-category").run("move-ticket",{guild,user,option:ticket.option,channel,ticket,currentCategoryId:channel.parentId}) + if (categoryResult && categoryResult.shouldChangeCategory && typeof categoryResult.newCategoryId !== "undefined" && typeof categoryResult.newCategoryMode !== "undefined" && typeof categoryResult.newCategory !== "undefined"){ + const originalCategoryName = channel.parent?.name ?? "" + const newCategoryName = categoryResult.newCategory?.name ?? "" + try{ + await utilities.timedAwait(channel.setParent(categoryResult.newCategoryId,{lockPermissions:false}),3000,(err) => { + process.emit("uncaughtException",new Error("Error: Unable to change channel parent: "+err)) + }) + ticket.get("opendiscord:category-mode").value = categoryResult.newCategoryMode + ticket.get("opendiscord:category").value = categoryResult.newCategoryId + }catch(err){ + await channel.send((await opendiscord.builders.messages.getSafe("opendiscord:error-channel-category").build("ticket-move",{guild,channel,user,originalCategory:originalCategoryName,newCategory:newCategoryName})).message) + opendiscord.log("Unable to move ticket to moved category.","error",[ + {key:"channel",value:"#"+channel.name}, + {key:"channelid",value:channel.id,hidden:true}, + {key:"categoryid",value:categoryResult.newCategoryId ?? "/"} ]) - }else{ - //default category was found - if (normalCategory.children.cache.size >= 50 && channelBackupCategory != ""){ - //use backup category - const backupCategory = await opendiscord.client.fetchGuildCategoryChannel(guild,channelBackupCategory) - if (!backupCategory){ - //default category was not found - opendiscord.log("Ticket Move Error: Unable to find category! #2","error",[ - {key:"categoryid",value:channelBackupCategory}, - {key:"backup",value:"true"} - ]) - }else{ - category = backupCategory.id - categoryMode = "backup" - } - }else{ - //use default category - category = normalCategory.id - categoryMode = "normal" - } } } - try { - //only move category when not the same. - if (channel.parentId != category) await utilities.timedAwait(channel.setParent(category,{lockPermissions:false}),2500,(err) => { - opendiscord.log("Failed to change channel category on ticket move","error") - }) - ticket.get("opendiscord:category-mode").value = categoryMode - ticket.get("opendiscord:category").value = category - }catch(e){ - opendiscord.log("Unable to move ticket to 'moved category'!","error",[ - {key:"channel",value:"#"+channel.name}, - {key:"channelid",value:channel.id,hidden:true}, - {key:"categoryid",value:category ?? "/"} - ]) - opendiscord.debugfile.writeErrorMessage(new api.ODError(e,"uncaughtException")) - } - //handle permissions const permissions: discord.OverwriteResolvable[] = [{ type:discord.OverwriteType.Role, diff --git a/src/actions/reopenTicket.ts b/src/actions/reopenTicket.ts index dbc169b..d5f1ffb 100644 --- a/src/actions/reopenTicket.ts +++ b/src/actions/reopenTicket.ts @@ -39,55 +39,26 @@ export async function registerActions(){ await opendiscord.statistics.get("opendiscord:global").setStat("opendiscord:tickets-reopened",1,"increase") await opendiscord.statistics.get("opendiscord:user").setStat("opendiscord:tickets-reopened",user.id,1,"increase") - //update category + //calculate & update category if (typeof params.allowCategoryChange == "boolean" ? params.allowCategoryChange : true){ - const channelCategory = ticket.option.get("opendiscord:channel-category").value - const channelBackupCategory = ticket.option.get("opendiscord:channel-category-backup").value - if (channelCategory !== ""){ - //category enabled - try { - const normalCategory = await opendiscord.client.fetchGuildCategoryChannel(guild,channelCategory) - if (!normalCategory){ - //default category was not found - opendiscord.log("Ticket Reopening Error: Unable to find category! #1","error",[ - {key:"categoryid",value:channelCategory}, - {key:"backup",value:"false"} - ]) - }else{ - //default category was found - if (normalCategory.children.cache.size >= 49 && channelBackupCategory != ""){ - //use backup category - const backupCategory = await opendiscord.client.fetchGuildCategoryChannel(guild,channelBackupCategory) - if (!backupCategory){ - //default category was not found - opendiscord.log("Ticket Reopening Error: Unable to find category! #2","error",[ - {key:"categoryid",value:channelBackupCategory}, - {key:"backup",value:"true"} - ]) - }else{ - //use backup category - channel.setParent(backupCategory,{lockPermissions:false}) - ticket.get("opendiscord:category-mode").value = "backup" - ticket.get("opendiscord:category").value = backupCategory.id - } - }else{ - //use default category - channel.setParent(normalCategory,{lockPermissions:false}) - ticket.get("opendiscord:category-mode").value = "normal" - ticket.get("opendiscord:category").value = normalCategory.id - } - } - }catch(e){ - opendiscord.log("Unable to move ticket to 'reopened category'!","error",[ + const categoryResult = await opendiscord.actions.get("opendiscord:calculate-ticket-category").run("reopen-ticket",{guild,user,option:ticket.option,channel,ticket,currentCategoryId:channel.parentId}) + if (categoryResult && categoryResult.shouldChangeCategory && typeof categoryResult.newCategoryId !== "undefined" && typeof categoryResult.newCategoryMode !== "undefined" && typeof categoryResult.newCategory !== "undefined"){ + const originalCategoryName = channel.parent?.name ?? "" + const newCategoryName = categoryResult.newCategory?.name ?? "" + try{ + await utilities.timedAwait(channel.setParent(categoryResult.newCategoryId,{lockPermissions:false}),3000,(err) => { + process.emit("uncaughtException",new Error("Error: Unable to change channel parent: "+err)) + }) + ticket.get("opendiscord:category-mode").value = categoryResult.newCategoryMode + ticket.get("opendiscord:category").value = categoryResult.newCategoryId + }catch(err){ + await channel.send((await opendiscord.builders.messages.getSafe("opendiscord:error-channel-category").build("ticket-reopen",{guild,channel,user,originalCategory:originalCategoryName,newCategory:newCategoryName})).message) + opendiscord.log("Unable to move ticket to reopened category.","error",[ {key:"channel",value:"#"+channel.name}, {key:"channelid",value:channel.id,hidden:true}, + {key:"categoryid",value:categoryResult.newCategoryId ?? "/"} ]) - opendiscord.debugfile.writeErrorMessage(new api.ODError(e,"uncaughtException")) } - }else{ - channel.setParent(null,{lockPermissions:false}) - ticket.get("opendiscord:category-mode").value = null - ticket.get("opendiscord:category").value = null } } diff --git a/src/actions/unclaimTicket.ts b/src/actions/unclaimTicket.ts index a26853a..ad216af 100644 --- a/src/actions/unclaimTicket.ts +++ b/src/actions/unclaimTicket.ts @@ -22,56 +22,26 @@ export async function registerActions(){ ticket.get("opendiscord:claimed-on").value = null ticket.get("opendiscord:busy").value = true - //update category + //calculate & update category if (typeof params.allowCategoryChange == "boolean" ? params.allowCategoryChange : true){ - const channelCategory = ticket.option.get("opendiscord:channel-category").value - const channelBackupCategory = ticket.option.get("opendiscord:channel-category-backup").value - if (channelCategory !== ""){ - //category enabled - try { - const normalCategory = await opendiscord.client.fetchGuildCategoryChannel(guild,channelCategory) - if (!normalCategory){ - //default category was not found - opendiscord.log("Ticket Unclaiming Error: Unable to find category! #1","error",[ - {key:"categoryid",value:channelCategory}, - {key:"backup",value:"false"} - ]) - }else{ - //default category was found - if (normalCategory.children.cache.size >= 49 && channelBackupCategory != ""){ - //use backup category - const backupCategory = await opendiscord.client.fetchGuildCategoryChannel(guild,channelBackupCategory) - if (!backupCategory){ - //default category was not found - opendiscord.log("Ticket Unclaiming Error: Unable to find category! #2","error",[ - {key:"categoryid",value:channelBackupCategory}, - {key:"backup",value:"true"} - ]) - }else{ - //use backup category - channel.setParent(backupCategory,{lockPermissions:false}) - ticket.get("opendiscord:category-mode").value = "backup" - ticket.get("opendiscord:category").value = backupCategory.id - } - }else{ - //use default category - channel.setParent(normalCategory,{lockPermissions:false}) - ticket.get("opendiscord:category-mode").value = "normal" - ticket.get("opendiscord:category").value = normalCategory.id - } - } - - }catch(e){ - opendiscord.log("Unable to move ticket to 'unclaimed category'!","error",[ + const categoryResult = await opendiscord.actions.get("opendiscord:calculate-ticket-category").run("unclaim-ticket",{guild,user,option:ticket.option,channel,ticket,currentCategoryId:channel.parentId}) + if (categoryResult && categoryResult.shouldChangeCategory && typeof categoryResult.newCategoryId !== "undefined" && typeof categoryResult.newCategoryMode !== "undefined" && typeof categoryResult.newCategory !== "undefined"){ + const originalCategoryName = channel.parent?.name ?? "" + const newCategoryName = categoryResult.newCategory?.name ?? "" + try{ + await utilities.timedAwait(channel.setParent(categoryResult.newCategoryId,{lockPermissions:false}),3000,(err) => { + process.emit("uncaughtException",new Error("Error: Unable to change channel parent: "+err)) + }) + ticket.get("opendiscord:category-mode").value = categoryResult.newCategoryMode + ticket.get("opendiscord:category").value = categoryResult.newCategoryId + }catch(err){ + await channel.send((await opendiscord.builders.messages.getSafe("opendiscord:error-channel-category").build("ticket-unclaim",{guild,channel,user,originalCategory:originalCategoryName,newCategory:newCategoryName})).message) + opendiscord.log("Unable to move ticket to unclaimed category.","error",[ {key:"channel",value:"#"+channel.name}, - {key:"channelid",value:channel.id,hidden:true} + {key:"channelid",value:channel.id,hidden:true}, + {key:"categoryid",value:categoryResult.newCategoryId ?? "/"} ]) - opendiscord.debugfile.writeErrorMessage(new api.ODError(e,"uncaughtException")) } - }else{ - channel.setParent(null,{lockPermissions:false}) - ticket.get("opendiscord:category-mode").value = null - ticket.get("opendiscord:category").value = null } } diff --git a/src/builders/embeds.ts b/src/builders/embeds.ts index 7797834..5ce7699 100644 --- a/src/builders/embeds.ts +++ b/src/builders/embeds.ts @@ -336,7 +336,7 @@ const errorEmbeds = () => { instance.setTitle(utilities.emojiTitle("❌",lang.getTranslation("errors.titles.channelRename"))) instance.setAuthor(user.displayName,user.displayAvatarURL()) instance.setDescription(lang.getTranslation("errors.descriptions.channelRename")) - instance.setFooter(lang.getTranslationWithParams("errors.descriptions.channelRenameOrigin",[method])) + instance.setFooter(lang.getTranslationWithParams("errors.descriptions.channelRenameSource",[method])) instance.addFields( {name:lang.getTranslation("params.uppercase.originalName")+":",value:"```#"+originalName+"```",inline:false}, {name:lang.getTranslation("params.uppercase.newName")+":",value:"```#"+newName+"```",inline:false} @@ -344,6 +344,30 @@ const errorEmbeds = () => { }) ) + //ERROR CHANNEL CATEGORY + embeds.add(new api.ODEmbed("opendiscord:error-channel-category")) + embeds.get("opendiscord:error-channel-category").workers.add( + new api.ODWorker("opendiscord:error-channel-category",0,async (instance,params,origin) => { + const {channel,user,originalCategory,newCategory} = params + + const method = (origin == "ticket-create" || origin == "ticket-close" || origin == "ticket-reopen" || origin == "ticket-claim" || origin == "ticket-unclaim" || origin == "ticket-move") ? origin : getMethodFromOrigin(origin) + + instance.setColor(generalConfig.data.system.useRedErrorEmbeds ? "Red" : generalConfig.data.mainColor) + //TODO TRANSLATION!!! + instance.setTitle(utilities.emojiTitle("❌","Unable To Change Category")) + instance.setAuthor(user.displayName,user.displayAvatarURL()) + //TODO TRANSLATION!!! + instance.setDescription("Due to Discord rate limits, the channel category could not be changed immediately. It will be changed automatically within 10 minutes if the bot remains online.") + instance.setFooter(lang.getTranslationWithParams("errors.descriptions.channelRenameSource",[method])) + instance.addFields( + //TODO TRANSLATION!!! + {name:"Original Category"+":",value:"```"+originalCategory+"```",inline:false}, + //TODO TRANSLATION!!! + {name:"New Category"+":",value:"```"+newCategory+"```",inline:false} + ) + }) + ) + //ERROR TICKET BUSY embeds.add(new api.ODEmbed("opendiscord:error-ticket-busy")) embeds.get("opendiscord:error-ticket-busy").workers.add( diff --git a/src/builders/messages.ts b/src/builders/messages.ts index 0028578..e974882 100644 --- a/src/builders/messages.ts +++ b/src/builders/messages.ts @@ -178,6 +178,16 @@ const errorMessages = () => { }) ) + //ERROR CHANNEL CATEGORY + messages.add(new api.ODMessage("opendiscord:error-channel-category")) + messages.get("opendiscord:error-channel-category").workers.add( + new api.ODWorker("opendiscord:error-channel-category",0,async (instance,params,origin) => { + const {guild,channel,user,originalCategory,newCategory} = params + instance.addEmbed(await embeds.getSafe("opendiscord:error-channel-category").build(origin,{guild,channel,user,originalCategory,newCategory})) + instance.setEphemeral(true) + }) + ) + //ERROR TICKET BUSY messages.add(new api.ODMessage("opendiscord:error-ticket-busy")) messages.get("opendiscord:error-ticket-busy").workers.add( diff --git a/src/core/api/ticket.ts b/src/core/api/ticket.ts index 389feaa..f68adb5 100644 --- a/src/core/api/ticket.ts +++ b/src/core/api/ticket.ts @@ -39,7 +39,7 @@ export interface ODTicketIdMappings extends ODTicketIdConstraint { "opendiscord:for-deletion":ODTicketData, "opendiscord:category":ODTicketData, - "opendiscord:category-mode":ODTicketData, + "opendiscord:category-mode":ODTicketData, "opendiscord:autoclose-enabled":ODTicketData, "opendiscord:autoclose-hours":ODTicketData, diff --git a/src/core/mappings/action.ts b/src/core/mappings/action.ts index 3ce48d4..8ef5780 100644 --- a/src/core/mappings/action.ts +++ b/src/core/mappings/action.ts @@ -128,6 +128,12 @@ export interface ODActionManagerIdMappings extends api.ODActionManagerIdConstrai result:{}, workers:"opendiscord:transfer-ticket"|"opendiscord:discord-logs"|"opendiscord:logs" }, + "opendiscord:calculate-ticket-category":{ + origin:"create-ticket"|"close-ticket"|"reopen-ticket"|"claim-ticket"|"unclaim-ticket"|"move-ticket"|"other", + params:{guild:discord.Guild,channel:discord.GuildTextBasedChannel|null,user:discord.User,option:ODTicketOption,ticket:ODTicket|null,currentCategoryId:string|null}, + result:{newCategoryId:string|null,newCategoryMode:string|null,newCategory:discord.CategoryChannel|null,shouldChangeCategory:boolean}, + workers:"opendiscord:default-category"|"opendiscord:close-category"|"opendiscord:claim-category"|"opendiscord:backup-category" + }, } ///////////////////////////// diff --git a/src/core/mappings/builder.ts b/src/core/mappings/builder.ts index 7ad2d97..63dcfbe 100644 --- a/src/core/mappings/builder.ts +++ b/src/core/mappings/builder.ts @@ -82,6 +82,7 @@ export interface ODEmbedManagerIdMappings extends api.ODEmbedManagerIdConstraint "opendiscord:error-panel-unknown":{origin:"slash"|"text"|"button"|"dropdown"|"modal"|"other",params:{guild:discord.Guild|null,channel:discord.TextBasedChannel,user:discord.User},workers:"opendiscord:error-panel-unknown"}, "opendiscord:error-not-in-guild":{origin:"slash"|"text"|"button"|"dropdown"|"modal"|"other",params:{channel:discord.TextBasedChannel,user:discord.User},workers:"opendiscord:error-not-in-guild"}, "opendiscord:error-channel-rename":{origin:"slash"|"text"|"button"|"dropdown"|"modal"|"ticket-pin"|"ticket-unpin"|"ticket-rename"|"ticket-move"|"ticket-priority"|"ticket-transfer"|"other",params:{guild:discord.Guild|null,channel:discord.TextBasedChannel,user:discord.User,originalName:string,newName:string},workers:"opendiscord:error-channel-rename"}, + "opendiscord:error-channel-category":{origin:"slash"|"text"|"button"|"dropdown"|"modal"|"ticket-create"|"ticket-close"|"ticket-reopen"|"ticket-claim"|"ticket-unclaim"|"ticket-move"|"other",params:{guild:discord.Guild|null,channel:discord.TextBasedChannel,user:discord.User,originalCategory:string,newCategory:string},workers:"opendiscord:error-channel-category"}, "opendiscord:error-ticket-busy":{origin:"slash"|"text"|"button"|"dropdown"|"modal"|"other",params:{guild:discord.Guild|null,channel:discord.TextBasedChannel,user:discord.User},workers:"opendiscord:error-ticket-busy"}, "opendiscord:help-menu":{origin:"text"|"slash"|"button"|"other",params:{mode:"slash"|"text",page:number},workers:"opendiscord:help-menu"}, @@ -164,6 +165,7 @@ export interface ODMessageManagerIdMappings extends api.ODMessageManagerIdConstr "opendiscord:error-panel-unknown":{origin:"slash"|"text"|"button"|"dropdown"|"modal"|"other",params:{guild:discord.Guild|null,channel:discord.TextBasedChannel,user:discord.User},workers:"opendiscord:error-panel-unknown"}, "opendiscord:error-not-in-guild":{origin:"slash"|"text"|"button"|"dropdown"|"modal"|"other",params:{channel:discord.TextBasedChannel,user:discord.User},workers:"opendiscord:error-not-in-guild"}, "opendiscord:error-channel-rename":{origin:"slash"|"text"|"button"|"dropdown"|"modal"|"ticket-pin"|"ticket-unpin"|"ticket-rename"|"ticket-move"|"ticket-priority"|"ticket-transfer"|"other",params:{guild:discord.Guild|null,channel:discord.TextBasedChannel,user:discord.User,originalName:string,newName:string},workers:"opendiscord:error-channel-rename"}, + "opendiscord:error-channel-category":{origin:"slash"|"text"|"button"|"dropdown"|"modal"|"ticket-create"|"ticket-close"|"ticket-reopen"|"ticket-claim"|"ticket-unclaim"|"ticket-move"|"other",params:{guild:discord.Guild|null,channel:discord.TextBasedChannel,user:discord.User,originalCategory:string,newCategory:string},workers:"opendiscord:error-channel-category"}, "opendiscord:error-ticket-busy":{origin:"slash"|"text"|"button"|"dropdown"|"modal"|"other",params:{guild:discord.Guild|null,channel:discord.TextBasedChannel,user:discord.User},workers:"opendiscord:error-ticket-busy"}, "opendiscord:help-menu":{origin:"slash"|"text"|"button"|"other",params:{mode:"slash"|"text",page:number},workers:"opendiscord:help-menu"}, diff --git a/src/index.ts b/src/index.ts index cba8a7c..25bb2fb 100644 --- a/src/index.ts +++ b/src/index.ts @@ -781,6 +781,7 @@ const main = async () => { opendiscord.log("Loading actions...","system") if (opendiscord.sharedFuses.getFuse("actionsLoading")){ await (await import("./actions/createTicketPermissions.js")).registerActions() + await (await import("./actions/calculateTicketCategory.js")).registerActions() await (await import("./actions/createTranscript.js")).registerActions() await (await import("./actions/createTicket.js")).registerActions() await (await import("./actions/closeTicket.js")).registerActions()