From 1b8468330ee62d70db2eb13bfc844859fa87dc56 Mon Sep 17 00:00:00 2001 From: DJj123dj <80536295+DJj123dj@users.noreply.github.com> Date: Wed, 18 Jan 2023 21:44:56 +0100 Subject: [PATCH] v3.2 last push --- .gitignore | 3 +- commands/change.js | 101 ++++++++++++++++++ core/api/api.json | 6 +- core/api/modules/events.js | 94 +++++++++++++++- core/interactionHandlers/embeds/commands.js | 13 +++ core/slashSystem/slashEnable.js | 23 +++- core/ticketActions/ticketCloser.js | 12 --- core/ticketActions/ticketOpener.js | 2 +- .../communication/compileJson.js | 7 +- core/transcriptSystem/communication/index.js | 23 ++-- core/transcriptSystem/embeds.js | 7 +- core/transcriptSystem/manager.js | 14 ++- index.js | 1 + language/english.json | 1 + language/slashcmds/slash.json | 47 ++++---- transcriptconfig.json | 14 +-- 16 files changed, 306 insertions(+), 62 deletions(-) create mode 100644 commands/change.js diff --git a/.gitignore b/.gitignore index d20c8c1..2c3ca30 100644 --- a/.gitignore +++ b/.gitignore @@ -16,4 +16,5 @@ storage/tsdatabase.json storage/.DS_Store .vscode/ livestatus.json -test.js \ No newline at end of file +test.js +testtsconfig.json \ No newline at end of file diff --git a/commands/change.js b/commands/change.js new file mode 100644 index 0000000..052e2fa --- /dev/null +++ b/commands/change.js @@ -0,0 +1,101 @@ +const discord = require('discord.js') +const bot = require('../index') +const client = bot.client +const config = bot.config +const log = bot.errorLog.log +const l = bot.language +const permsChecker = require("../core/utils/permisssionChecker") + +const APIEvents = require("../core/api/modules/events") +const DISABLE = require("../core/api/api.json").disable + +module.exports = () => { + bot.errorLog.log("debug","COMMANDS: loaded change.js") + + if (!DISABLE.commands.text.change) client.on("messageCreate",msg => { + if (!msg.content.startsWith(config.prefix+"change")) return + + if (!msg.guild) return + if (!permsChecker.command(msg.author.id,msg.guild.id)){ + permsChecker.sendUserNoPerms(msg.author) + return + } + + msg.channel.messages.fetchPinned().then(msglist => { + var firstmsg = msglist.last() + if (firstmsg == undefined || firstmsg.author.id != client.user.id) return msg.channel.send({embeds:[bot.errorLog.notInATicket]}) + const hiddendata = bot.hiddenData.readHiddenData(firstmsg.embeds[0].description) + const ticketId = hiddendata.data.find(d => d.key == "type").value + + var newtype = msg.content.split(config.prefix+"change")[1].substring(1) + if (!newtype) return msg.channel.send({embeds:[bot.errorLog.invalidArgsMessage(l.errors.missingArgsDescription+" ``:\n`"+config.prefix+"change `")]}) + + const newTicket = require("../core/utils/configParser").getTicketById(newtype) + const list = [] + config.options.forEach((o) => list.push(o.id)) + if (!newTicket) return bot.errorLog.invalidIdChooseFromList(list) + + /**@type {String} */ + var chName = msg.channel.name + const splitted = chName.split("-") + const prefix = splitted.shift() + const name = (splitted.length > 0) ? splitted.join("-") : prefix + + + msg.channel.setName(newTicket.channelprefix+name) + msg.channel.send({embeds:[bot.embeds.commands.changeEmbed(msg.author,newtype)]}) + + log("command","someone used the 'change' command",[{key:"user",value:msg.author.tag}]) + log("system","ticket type changed",[{key:"user",value:msg.author.tag},{key:"ticket",value:name},{key:"newtype",value:newtype}]) + APIEvents.onCommand("change",permsChecker.command(msg.author.id,msg.guild.id),msg.author,msg.channel,msg.guild,new Date()) + }) + + + }) + + if (!DISABLE.commands.slash.change) client.on("interactionCreate",(interaction) => { + if (!interaction.isChatInputCommand()) return + if (interaction.commandName != "change") return + + if (!interaction.guild) return + if (!permsChecker.command(interaction.user.id,interaction.guild.id)){ + permsChecker.sendUserNoPerms(interaction.user) + return + } + + //interaction.deferReply() + interaction.channel.messages.fetchPinned().then(msglist => { + var firstmsg = msglist.last() + if (firstmsg == undefined || firstmsg.author.id != client.user.id) return interaction.reply({embeds:[bot.errorLog.notInATicket]}) + const hiddendata = bot.hiddenData.readHiddenData(firstmsg.embeds[0].description) + const ticketId = hiddendata.data.find(d => d.key == "type").value + + console.log("HELLO WORLD!! 1") + var newtype = interaction.options.getString("type",true) + if (!newtype) return interaction.reply({embeds:[bot.errorLog.invalidArgsMessage(l.errors.missingArgsDescription+" ``:\n`"+config.prefix+"change `")]}) + + console.log("HELLO WORLD!! 2") + const newTicket = require("../core/utils/configParser").getTicketById(newtype,true) + const list = [] + config.options.forEach((o) => list.push(o.id)) + if (!newTicket) return bot.errorLog.invalidIdChooseFromList(list) + + console.log("HELLO WORLD!! 3") + /**@type {String} */ + var chName = interaction.channel.name + const splitted = chName.split("-") + const prefix = splitted.shift() + const name = (splitted.length > 0) ? splitted.join("-") : prefix + + console.log("HELLO WORLD!! 4") + + interaction.channel.setName(newTicket.channelprefix+name) + interaction.reply({embeds:[bot.embeds.commands.changeEmbed(interaction.user,newtype)]}) + + console.log("HELLO WORLD!! 5") + log("command","someone used the 'change' command",[{key:"user",value:interaction.user.tag}]) + log("system","ticket type changed",[{key:"user",value:interaction.user.tag},{key:"ticket",value:name},{key:"newtype",value:newtype}]) + APIEvents.onCommand("change",permsChecker.command(interaction.user.id,interaction.guild.id),interaction.user,interaction.channel,interaction.guild,new Date()) + }) + }) +} \ No newline at end of file diff --git a/core/api/api.json b/core/api/api.json index 6832775..d82b261 100644 --- a/core/api/api.json +++ b/core/api/api.json @@ -29,7 +29,8 @@ "remove":false, "rename":false, "claim":false, - "unclaim":false + "unclaim":false, + "change":false }, "slash":{ "help":false, @@ -42,7 +43,8 @@ "remove":false, "rename":false, "claim":false, - "unclaim":false + "unclaim":false, + "change":false } }, "checkerjs":{ diff --git a/core/api/modules/events.js b/core/api/modules/events.js index 5536ffb..f51f503 100644 --- a/core/api/modules/events.js +++ b/core/api/modules/events.js @@ -17,6 +17,10 @@ const ticketReopenListeners = [] const ticketAddListeners = [] const ticketRemoveListeners = [] +const ticketClaimListeners = [] +const ticketUnclaimListeners = [] +const ticketChangeListeners = [] + const transcriptCreationListeners = [] const commandListeners = [] @@ -146,6 +150,66 @@ exports.onTicketRemove = (user,editeduser,channel,guild,date,ticketdata) => { } +/** + * + * @param {discord.User} user + * @param {discord.TextChannel} channel + * @param {discord.Guild} guild + * @param {Date} date + * @param {{name:String,status:"open"|"closed"|"deleted"|"reopened",ticketOptions:configParser.OTTicketOptions}} ticketdata + */ +exports.onTicketClaim = (user,channel,guild,date,ticketdata) => { + //system + bot.errorLog.log("api","user claimed a ticket",[{key:"userid",value:user.id},{key:"channelid",value:channel.id},{key:"guildid",value:guild.id},{key:"ticketdata",value:JSON.stringify(ticketdata)}]) + + ticketClaimListeners.forEach((func) => { + try { + func(user,channel,guild,date,ticketdata) + }catch{} + }) + +} + +/** + * + * @param {discord.User} user + * @param {discord.TextChannel} channel + * @param {discord.Guild} guild + * @param {Date} date + * @param {{name:String,status:"open"|"closed"|"deleted"|"reopened",ticketOptions:configParser.OTTicketOptions}} ticketdata + */ +exports.onTicketUnclaim = (user,channel,guild,date,ticketdata) => { + //system + bot.errorLog.log("api","user unclaimed a ticket",[{key:"userid",value:user.id},{key:"channelid",value:channel.id},{key:"guildid",value:guild.id},{key:"ticketdata",value:JSON.stringify(ticketdata)}]) + + ticketUnclaimListeners.forEach((func) => { + try { + func(user,channel,guild,date,ticketdata) + }catch{} + }) + +} + +/** + * @param {String} newtype + * @param {discord.User} user + * @param {discord.TextChannel} channel + * @param {discord.Guild} guild + * @param {Date} date + * @param {{name:String,status:"open"|"closed"|"deleted"|"reopened",ticketOptions:configParser.OTTicketOptions}} ticketdata + */ +exports.onTicketChange = (newtype,user,channel,guild,date,ticketdata) => { + //system + bot.errorLog.log("api","user changed ticket type",[{key:"newtype",value:newtype},{key:"userid",value:user.id},{key:"channelid",value:channel.id},{key:"guildid",value:guild.id},{key:"ticketdata",value:JSON.stringify(ticketdata)}]) + + ticketChangeListeners.forEach((func) => { + try { + func(newtype,user,channel,guild,date,ticketdata) + }catch{} + }) + +} + /** * * @param {discord.Message[]} messages @@ -165,7 +229,7 @@ exports.onTranscriptCreation = (messages,channel,guild,date) => { } -/**@typedef {"help"|"message"|"close"|"delete"|"reopen"|"rename"|"add"|"remove"} OTCommandType */ +/**@typedef {"help"|"message"|"close"|"delete"|"reopen"|"rename"|"add"|"remove"|"claim"|"unclaim"|"change"} OTCommandType */ /** * @@ -269,6 +333,16 @@ exports.onCommand = (type,hasPerms,user,channel,guild,date) => { * @param {{name:String,status:"open"|"closed"|"deleted"|"reopened",ticketOptions:configParser.OTTicketOptions}} ticketdata */ +/** + * @callback TicketTypeChangeEvent + * @param {String} newtype + * @param {discord.User} user + * @param {discord.TextChannel} channel + * @param {discord.Guild} guild + * @param {Date} date + * @param {{name:String,status:"open"|"closed"|"deleted"|"reopened",ticketOptions:configParser.OTTicketOptions}} ticketdata + */ + /** * @callback CommandEvent * @param {OTCommandType} type @@ -328,6 +402,21 @@ const onTicketRemove = (callback) => { ticketRemoveListeners.push(callback) } +/**@param {TicketActionEvent} callback */ +const onTicketClaim = (callback) => { + ticketClaimListeners.push(callback) +} + +/**@param {TicketActionEvent} callback */ +const onTicketUnclaim = (callback) => { + ticketUnclaimListeners.push(callback) +} + +/**@param {TicketTypeChangeEvent} callback */ +const onTicketChange = (callback) => { + ticketChangeListeners.push(callback) +} + /**@param {TranscriptCreationEvent} callback */ const onTranscriptCreation = (callback) => { transcriptCreationListeners.push(callback) @@ -355,6 +444,9 @@ exports.events = { onTicketReopen, onTicketAdd, onTicketRemove, + onTicketClaim, + onTicketUnclaim, + onTicketChange, onTranscriptCreation, onCommand, onReactionRole, diff --git a/core/interactionHandlers/embeds/commands.js b/core/interactionHandlers/embeds/commands.js index 990bd23..b2ec6c2 100644 --- a/core/interactionHandlers/embeds/commands.js +++ b/core/interactionHandlers/embeds/commands.js @@ -136,4 +136,17 @@ exports.renameEmbed = (renamer,newname) => { .setDescription("Loading...") .setFooter({text:author.tag,iconURL:author.displayAvatarURL()}) } +} + +/** + * + * @param {discord.User} changer + * @param {String} newtype + * @returns {discord.EmbedBuilder} + */ +exports.changeEmbed = (changer,newtype) => { + return new embed() + .setTitle("🔄 "+l.commands.changeTitle.replace("{0}",newtype)) + .setColor(mc) + .setFooter({text:changer.tag,iconURL:changer.displayAvatarURL()}) } \ No newline at end of file diff --git a/core/slashSystem/slashEnable.js b/core/slashSystem/slashEnable.js index 299b5e7..b587f7b 100644 --- a/core/slashSystem/slashEnable.js +++ b/core/slashSystem/slashEnable.js @@ -32,8 +32,8 @@ module.exports = async () => { //process.stdout.write("[status] there are "+chalk.blue("0 out of 10")+" commands ready! (this can take up to 40 seconds)") setInterval(() => { process.stdout.cursorTo(0) - process.stdout.write("[status] there are "+chalk.blue(readystats+" out of 12")+" commands ready! (this can take up to 40 seconds)") - if (readystats >= 12){ + process.stdout.write("[status] there are "+chalk.blue(readystats+" out of 13")+" commands ready! (this can take up to 40 seconds)") + if (readystats >= 13){ const fs = require("fs") fs.writeFileSync("./storage/slashcmdEnabled.txt","true") console.log(chalk.green("\nready!")) @@ -245,4 +245,23 @@ module.exports = async () => { readystats++ }) + //category + client.application.commands.create({ + name:"change", + description:"Change ticket type.", + defaultPermission:true, + type:act.ChatInput, + options:[ + { + type:acot.String, + name:"type", + description:"The new ticket type.", + required:true, + choices:choices + } + ] + },sid).then(() => { + readystats++ + }) + } \ No newline at end of file diff --git a/core/ticketActions/ticketCloser.js b/core/ticketActions/ticketCloser.js index 310ae0b..61d76ad 100755 --- a/core/ticketActions/ticketCloser.js +++ b/core/ticketActions/ticketCloser.js @@ -88,12 +88,6 @@ exports.NEWcloseTicket = async (member,channel,prefix,mode,reason,nomessage) => if (!id) return false - try{ - if (config.system.enable_DM_Messages){ - member.send({embeds:[bot.errorLog.custom(l.messages.deletedTicketDmTitle,l.messages.deletedTicketDmDescription,":ticket:",config.main_color)]}) - } - } - catch{log("system","can't send DM to member, member doesn't allow dm's")} }) }else if (mode == "close"){ @@ -229,12 +223,6 @@ exports.NEWcloseTicket = async (member,channel,prefix,mode,reason,nomessage) => if (!id) return false - try{ - if (config.system.enable_DM_Messages){ - member.send({embeds:[bot.errorLog.custom(l.messages.closedTicketDmTitle,l.messages.closedTicketDmDescription+"\n\n**reason:** "+newReason,":ticket:",config.main_color)]}) - } - } - catch{log("system","can't send DM to member, member doesn't allow dm's")} }) diff --git a/core/ticketActions/ticketOpener.js b/core/ticketActions/ticketOpener.js index 3ff877b..9af64eb 100755 --- a/core/ticketActions/ticketOpener.js +++ b/core/ticketActions/ticketOpener.js @@ -66,7 +66,7 @@ module.exports = () => { if (storage.get("ticketStorage",interaction.member.id) == null || storage.get("ticketStorage",interaction.member.id) == "false"|| Number(storage.get("ticketStorage",interaction.member.id)) < config.system.max_allowed_tickets){ try{ - if (config.system.enable_DM_Messages){ + if (currentTicketOptions.enableDmOnOpen){ interaction.member.send({embeds:[bot.errorLog.custom(l.messages.newTicketDmTitle,currentTicketOptions.message,":ticket:",config.main_color)]}) } } diff --git a/core/transcriptSystem/communication/compileJson.js b/core/transcriptSystem/communication/compileJson.js index f19ed8c..6aa1ba4 100644 --- a/core/transcriptSystem/communication/compileJson.js +++ b/core/transcriptSystem/communication/compileJson.js @@ -21,6 +21,7 @@ const version = require("../info.json").version */ exports.compile = (guild,channel,user,messagesInv,data) => { const messages = messagesInv.reverse() + var invisibleMessages = 0 const rawfiles = messages.filter((m) => Array.from(m.attachments).length > 0) var fileAmount = 0 @@ -30,8 +31,6 @@ exports.compile = (guild,channel,user,messagesInv,data) => { const messagesArray = [] messages.forEach((msg) => { - - console.log("'"+msg.content+"'",msg.embeds.length,msg.attachments.toJSON().length) if (msg.content || msg.embeds.length > 0 || msg.attachments.toJSON().length > 0){ const embedArray = [] @@ -78,7 +77,7 @@ exports.compile = (guild,channel,user,messagesInv,data) => { embeds:embedArray, files:fileArray }) - } + }else {invisibleMessages++} }) const result = { @@ -109,7 +108,7 @@ exports.compile = (guild,channel,user,messagesInv,data) => { closedtime:data.ticket.closedtime, openedtime:data.ticket.openedtime, - messages:Array.from(messages).length, + messages:Array.from(messages).length-invisibleMessages, files:fileAmount }, messages:messagesArray diff --git a/core/transcriptSystem/communication/index.js b/core/transcriptSystem/communication/index.js index 8de4682..64235d3 100644 --- a/core/transcriptSystem/communication/index.js +++ b/core/transcriptSystem/communication/index.js @@ -10,19 +10,20 @@ const tsdb = require("../tsdb") /** * @param {Object} json - * @returns {OTuploadResponse} + * @returns {Promise} */ exports.upload = async (json) => { + return new Promise(async (resolve,reject) => { + const data = encodeURIComponent(JSON.stringify(json)) - const data = encodeURIComponent(JSON.stringify(json)) + try { + const res = await axios.get("https://api.transcripts.dj-dj.be/upload?auth=openticketTRANSCRIPT1234&version=1.0.0&data="+data) + if (res.status != 200) resolve(false) - try { - const res = await axios.get("https://api.transcripts.dj-dj.be/upload?auth=openticketTRANSCRIPT1234&version=1.0.0&data="+data) - if (res.status != 200) return false - - return res.data - }catch{ - console.log("failed transcript upload!") - return false - } + resolve(res.data) + }catch{ + console.log("failed transcript upload!") + resolve(false) + } + }) } \ No newline at end of file diff --git a/core/transcriptSystem/embeds.js b/core/transcriptSystem/embeds.js index ac4468d..e849058 100644 --- a/core/transcriptSystem/embeds.js +++ b/core/transcriptSystem/embeds.js @@ -57,15 +57,18 @@ exports.tsready = (name,id,url,user) => { * @param {String} name * @param {String} id * @param {discord.User} user + * @param {undefined|String} err */ -exports.tserror = (name,id,user) => { +exports.tserror = (name,id,user,err) => { const embed = new discord.EmbedBuilder() .setTitle("❌ Transcript") .setColor(discord.Colors.Red) .setAuthor({name:user.tag,iconURL:user.displayAvatarURL()}) .setFooter({text:name}) - .setDescription("Something went wrong while creating the HTML transcript.\n\n[possible reasons](https://docs.openticket.dj-dj.be/the-system/transcripts/transcript-errors)") + const errDesc = (typeof err == "string") ? "\n"+err : "" + + embed.setDescription("Something went wrong while creating the HTML transcript.\n\n[possible reasons](https://docs.openticket.dj-dj.be/the-system/transcripts/transcript-errors)"+errDesc) return embed } diff --git a/core/transcriptSystem/manager.js b/core/transcriptSystem/manager.js index 72dbe43..0b7179e 100644 --- a/core/transcriptSystem/manager.js +++ b/core/transcriptSystem/manager.js @@ -70,7 +70,7 @@ module.exports = async (messages,guild,channel,user,reason) => { }) if (!await checkAvailability()){ - const attachment = await require("./oldTranscript").createTranscript(messages,ch) + const attachment = await require("./oldTranscript").createTranscript(messages,channel) const errembed = tsembeds.tserror(chName,chId,user) if (tsconfig.sendTranscripts.enableChannel){ @@ -85,6 +85,7 @@ module.exports = async (messages,guild,channel,user,reason) => { const TSdata = await require("./communication/index").upload(JSONDATA) if (!TSdata) return false + if (TSdata.status == "success"){ const url = "https://transcripts.dj-dj.be/t/"+TSdata.time+"_"+TSdata.id+".html" @@ -121,6 +122,17 @@ module.exports = async (messages,guild,channel,user,reason) => { }catch{} } },duration) + }else{ + const attachment = await require("./oldTranscript").createTranscript(messages,channel) + const errembed = tsembeds.tserror(chName,chId,user,"`error type: transcript API error`") + + if (tsconfig.sendTranscripts.enableChannel){ + /**@type {discord.TextChannel|undefined} */ + const tc = guild.channels.cache.find((c) => c.id == tsconfig.sendTranscripts.channel) + + if (!tc) return + tc.send({embeds:[errembed],files:[attachment]}) + } } } asyncmanager(msglist) diff --git a/index.js b/index.js index 503df85..d072fa9 100644 --- a/index.js +++ b/index.js @@ -216,6 +216,7 @@ if (process.argv[2] && process.argv[2].startsWith("slash")){ require("./commands/reopen")() require("./commands/claim")() require("./commands/unclaim")() + require("./commands/change")() this.errorLog.log("debug","LOADING CORE") //core diff --git a/language/english.json b/language/english.json index 480d7f4..b57007b 100644 --- a/language/english.json +++ b/language/english.json @@ -27,6 +27,7 @@ "reopenTitle":"Re-Opened this ticket!", "claimTitle":"This ticket is now claimed to {0}", "unclaimTitle":"This ticket isn't claimed anymore!", + "changeTitle":"Changed ticket type to {0}!", "ticketWarning":"The embed is in the message below!", "maxTicketWarning":"**Warning:** _You can only create {0} ticket(s) at a time!_" diff --git a/language/slashcmds/slash.json b/language/slashcmds/slash.json index a3b8f56..30231a3 100644 --- a/language/slashcmds/slash.json +++ b/language/slashcmds/slash.json @@ -114,26 +114,37 @@ "it":"Rinomina il canale di un ticket." }, "claim":{ - "en-GB":"", - "en-US":"", - "nl":"", - "ro":"", - "de":"", - "cs":"", - "fr":"", - "es-ES":"", - "it":"" + "en-GB":"Claim a ticket.", + "en-US":"Claim a ticket.", + "nl":"Claim een ticket.", + "ro":" ", + "de":" ", + "cs":" ", + "fr":" ", + "es-ES":" ", + "it":" " }, "unclaim":{ - "en-GB":"", - "en-US":"", - "nl":"", - "ro":"", - "de":"", - "cs":"", - "fr":"", - "es-ES":"", - "it":"" + "en-GB":"Un-claim a ticket.", + "en-US":"Un-claim a ticket.", + "nl":"Ontclaim een ticket.", + "ro":" ", + "de":" ", + "cs":" ", + "fr":" ", + "es-ES":" ", + "it":" " + }, + "category":{ + "en-GB":"Change ticket type.", + "en-US":"Change ticket type.", + "nl":"Verander ticket type.", + "ro":" ", + "de":" ", + "cs":" ", + "fr":" ", + "es-ES":" ", + "it":" " } } } \ No newline at end of file diff --git a/transcriptconfig.json b/transcriptconfig.json index 1aefbfa..9fd2f33 100644 --- a/transcriptconfig.json +++ b/transcriptconfig.json @@ -1,16 +1,16 @@ { "sendTranscripts":{ - "enableChannel":true, - "channel":"968899606912061490", + "enableChannel":false, + "channel":"transcript channel id", - "enableDM":true + "enableDM":false }, "style":{ - "titleColor":"#49c100", + "titleColor":"#F8BA00", "background":{ - "enableCustomBackground":true, - "backgroundModus":"image", - "backgroundData":"https://pbs.twimg.com/media/EBjFBlVXsAAhvXl?format=jpg&name=4096x4096" + "enableCustomBackground":false, + "backgroundModus":"color OR image", + "backgroundData":"#abc123 OR https://www.example.com/image.png" } }