/////////////////////////////////////// //TICKET MOVING 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:move-ticket")) opendiscord.actions.get("opendiscord:move-ticket").workers.add([ new api.ODWorker("opendiscord:move-ticket",2,async (instance,params,origin,cancel) => { const {guild,channel,user,ticket,reason,data} = params if (channel.isThread()) throw new api.ODSystemError("Unable to move ticket! Open Ticket doesn't support threads!") await opendiscord.events.get("onTicketMove").emit([ticket,user,channel,reason]) ticket.option = data //update stats await opendiscord.statistics.get("opendiscord:global").setStat("opendiscord:tickets-moved",1,"increase") await opendiscord.statistics.get("opendiscord:user").setStat("opendiscord:tickets-moved",user.id,1,"increase") //get new channel properties const channelPrefix = ticket.option.get("opendiscord:channel-prefix").value const channelSuffix = ticket.get("opendiscord:channel-suffix").value //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 ?? "/"} ]) } } //handle permissions const permissions: discord.OverwriteResolvable[] = [{ type:discord.OverwriteType.Role, id:guild.roles.everyone.id, allow:[], deny:["ViewChannel","SendMessages","ReadMessageHistory"] }] const globalAdmins = opendiscord.configs.get("opendiscord:general").data.globalAdmins const optionAdmins = ticket.option.get("opendiscord:admins").value const readonlyAdmins = ticket.option.get("opendiscord:admins-readonly").value globalAdmins.forEach((admin) => { permissions.push({ type:discord.OverwriteType.Role, id:admin, allow:["ViewChannel","SendMessages","AddReactions","AttachFiles","SendPolls","ReadMessageHistory","ManageMessages"], deny:[] }) }) optionAdmins.forEach((admin) => { if (globalAdmins.includes(admin)) return permissions.push({ type:discord.OverwriteType.Role, id:admin, allow:["ViewChannel","SendMessages","AddReactions","AttachFiles","SendPolls","ReadMessageHistory","ManageMessages"], deny:[] }) }) readonlyAdmins.forEach((admin) => { if (globalAdmins.includes(admin)) return if (optionAdmins.includes(admin)) return permissions.push({ type:discord.OverwriteType.Role, id:admin, allow:["ViewChannel","ReadMessageHistory"], deny:["SendMessages","AddReactions","AttachFiles","SendPolls"] }) }) //transfer all old user-participants over to the new ticket (creator & participants) ticket.get("opendiscord:participants").value.forEach((p) => { if (p.type == "user") permissions.push({ type:discord.OverwriteType.Member, id:p.id, allow:["ViewChannel","SendMessages","AddReactions","AttachFiles","SendPolls","ReadMessageHistory"], deny:[] }) }) try{ await channel.permissionOverwrites.set(permissions) }catch{ opendiscord.log("Failed to reset channel permissions on ticket move!","error") } //handle participants const participants: {type:"role"|"user",id:string}[] = [] permissions.forEach((permission,index) => { if (index == 0) return //don't include @everyone const type = (permission.type == discord.OverwriteType.Role) ? "role" : "user" const id = permission.id as string participants.push({type,id}) }) ticket.get("opendiscord:participants").value = participants ticket.get("opendiscord:participants").refreshDatabase() //rename channel (and give error when crashed) const pinEmoji = ticket.get("opendiscord:pinned").value ? generalConfig.data.system.pinEmoji : "" const priorityEmoji = opendiscord.priorities.getFromPriorityLevel(ticket.get("opendiscord:priority").value).channelEmoji ?? "" const originalName = channel.name const newName = pinEmoji+priorityEmoji+utilities.trimEmojis(channelPrefix+channelSuffix) try{ await utilities.timedAwait(channel.setName(newName),2500,(err) => { opendiscord.log("Failed to rename channel on ticket move","error") }) }catch(err){ await channel.send((await opendiscord.builders.messages.getSafe("opendiscord:error-channel-rename").build("ticket-move",{guild,channel,user,originalName,newName:newName})).message) } //update ticket message const ticketMessage = await opendiscord.tickets.getTicketMessage(ticket) if (ticketMessage){ try{ ticketMessage.edit((await opendiscord.builders.messages.getSafe("opendiscord:ticket-message").build("other",{guild,channel,user,ticket})).message) }catch(e){ opendiscord.log("Unable to edit ticket message on ticket moving!","error",[ {key:"channel",value:"#"+channel.name}, {key:"channelid",value:channel.id,hidden:true}, {key:"messageid",value:ticketMessage.id}, {key:"option",value:ticket.option.id.value} ]) opendiscord.debugfile.writeErrorMessage(new api.ODError(e,"uncaughtException")) } } //reply with new message if (params.sendMessage) await channel.send((await opendiscord.builders.messages.getSafe("opendiscord:move-message").build(origin,{guild,channel,user,ticket,reason,data})).message) ticket.get("opendiscord:busy").value = false await opendiscord.events.get("afterTicketMoved").emit([ticket,user,channel,reason]) //update channel topic await opendiscord.actions.get("opendiscord:update-ticket-topic").run("ticket-action",{guild,channel,user,ticket,sendMessage:false,newTopic:null}) }), new api.ODWorker("opendiscord:discord-logs",1,async (instance,params,origin,cancel) => { const {guild,channel,user,ticket,reason,data} = params //to logs if (generalConfig.data.system.logs.enabled && generalConfig.data.system.messages.moving.logs){ const logChannel = opendiscord.posts.get("opendiscord:logs") if (logChannel) logChannel.send(await opendiscord.builders.messages.getSafe("opendiscord:ticket-action-logs").build(origin,{guild,channel,user,ticket,mode:"move",reason,additionalData:data})) } //to dm const creator = await opendiscord.tickets.getTicketUser(ticket,"creator") if (creator && generalConfig.data.system.messages.moving.dm) await opendiscord.client.sendUserDm(creator,await opendiscord.builders.messages.getSafe("opendiscord:ticket-action-dm").build(origin,{guild,channel,user,ticket,mode:"move",reason,additionalData:data})) }), new api.ODWorker("opendiscord:logs",0,(instance,params,origin,cancel) => { const {guild,channel,user,ticket} = params opendiscord.log(user.displayName+" moved a ticket!","info",[ {key:"user",value:user.username}, {key:"userid",value:user.id,hidden:true}, {key:"channel",value:"#"+channel.name}, {key:"channelid",value:channel.id,hidden:true}, {key:"reason",value:params.reason ?? "/"}, {key:"method",value:origin} ]) }) ]) opendiscord.actions.get("opendiscord:move-ticket").workers.backupWorker = new api.ODWorker("opendiscord:cancel-busy",0,(instance,params) => { //set busy to false in case of crash or cancel params.ticket.get("opendiscord:busy").value = false }) }