|
| 1 | +const { Client, Intents, Permissions } = require("discord.js"); |
| 2 | +const client = new Client({ |
| 3 | + intents: [ |
| 4 | + Intents.FLAGS.GUILDS, |
| 5 | + Intents.FLAGS.GUILD_MEMBERS, |
| 6 | + Intents.FLAGS.GUILD_MESSAGES, |
| 7 | + Intents.FLAGS.DIRECT_MESSAGES, |
| 8 | + Intents.FLAGS.DIRECT_MESSAGE_TYPING, |
| 9 | + Intents.FLAGS.DIRECT_MESSAGE_REACTIONS |
| 10 | + ], |
| 11 | + partials: ["CHANNEL"] |
| 12 | +}) // not sure about intents, though |
| 13 | +const { REST } = require("@discordjs/rest"); |
| 14 | +const { Routes } = require("discord-api-types/v9"); |
| 15 | +const config = require("./config.json"); |
| 16 | +const strings = require("./strings.json"); |
| 17 | +const { paste } = require("ubuntu-pastebin"); |
| 18 | +const { QuickDB } = require("quick.db"); |
| 19 | +const db = new QuickDB(); |
| 20 | +// If you store your database somewhere else, uncomment & edit the following: |
| 21 | +// const db = new QuickDB({filePath: "/path/to/db.sqlite"}); |
| 22 | + |
| 23 | +// Notify once logged in |
| 24 | +client.once("ready", () => { |
| 25 | + console.log(`Logged in as ${client.user.tag}.`); |
| 26 | +}) |
| 27 | + |
| 28 | +// Log in |
| 29 | +client.login(process.env.TOKEN); |
| 30 | +// If that doesn't work, uncomment & edit this: |
| 31 | +// client.login("PUT YOUR TOKEN HERE"); |
| 32 | + |
| 33 | +client.on("messageCreate", async message => { |
| 34 | + if(message.author.bot) return; |
| 35 | + if(message.guild.member(message.author).communicationDisabledUntilTimestamp !== null && config.permissions.disableOnTimeout === true) return message.author.send(strings.disableOnTimeout); |
| 36 | + if(message.guild.member(message.author).pending) return message.author.send(strings.pending); |
| 37 | + let text = string.replace(/[`]|@everyone|@here/g, ''); |
| 38 | + // Use table from 1.1.9 |
| 39 | + const table = db.table("Support13") |
| 40 | + |
| 41 | + if(message.channel.type === "DM"){ |
| 42 | + let active = await table.get(`support_${message.author.id}`); |
| 43 | + let block = await table.get(`blocked_${message.author.id}`); |
| 44 | + if(config.enabled === false) return message.author.send(strings.disabled); |
| 45 | + if(block === true) return message.author.send(strings.blocked); |
| 46 | + let guild = await client.guilds.fetch(config.id.server); |
| 47 | + let ticketcategory = await guild.channels.fetch(config.id.ticketCategory); |
| 48 | + let channel, found = true; |
| 49 | + |
| 50 | + // NEW TICKET |
| 51 | + if(active === null){ |
| 52 | + await table.add("Tickets", 1); |
| 53 | + let ticket = await table.get("Tickets"); |
| 54 | + |
| 55 | + channel = await guild.channels.create(`${message.author.username}`, { |
| 56 | + type: "GUILD_TEXT", |
| 57 | + topic: `#${ticket} | From ${message.author.username}`, |
| 58 | + parent: tc, |
| 59 | + reason: `${message.author.id} opened a ticket through the modmail service.`, |
| 60 | + permissionOverwrites: [ |
| 61 | + {id: guild.roles.everyone, deny: [Permissions.FLAGS.VIEW_CHANNEL]}, |
| 62 | + {id: guild.roles.fetch(config.roles.mod), allow: [ |
| 63 | + Permissions.FLAGS.VIEW_CHANNEL, |
| 64 | + Permissions.FLAGS.SEND_MESSAGES, |
| 65 | + Permissions.FLAGS.ATTACH_FILES, |
| 66 | + Permissions.FLAGS.EMBED_LINKS, |
| 67 | + Permissions.FLAGS.READ_MESSAGE_HISTORY |
| 68 | + ]}, |
| 69 | + {id: guild.roles.fetch(config.roles.bot), allow: [ |
| 70 | + Permissions.FLAGS.VIEW_CHANNEL, |
| 71 | + Permissions.FLAGS.SEND_MESSAGES, |
| 72 | + Permissions.FLAGS.ATTACH_FILES, |
| 73 | + Permissions.FLAGS.EMBED_LINKS, |
| 74 | + Permissions.FLAGS.READ_MESSAGE_HISTORY |
| 75 | + ]} |
| 76 | + ] |
| 77 | + }); |
| 78 | + |
| 79 | + let author = message.author; |
| 80 | + try { |
| 81 | + const newTicketLog = new MessageEmbed() |
| 82 | + .setAuthor(author.tag, author.avatarURL()) |
| 83 | + .setDescription(`Ticket ${ticket} opened`) |
| 84 | + .setTimestamp().setColor("0x6666ff") |
| 85 | + let logs = await client.channels.fetch(config.id.logchannel); // set the log channel id here |
| 86 | + logs.send({embeds: [newTicketLog]}); |
| 87 | + } catch(e) { |
| 88 | + console.warn("Could not send log embed. Ignoring..."); |
| 89 | + } |
| 90 | + |
| 91 | + message.author.send(strings.welcome); |
| 92 | + await table.set(`support_${author.id}`, {channel: channel.id, target: author.id, ticket: ticket}); |
| 93 | + await table.set(`channel_${channel.id}`, author.id); |
| 94 | + await channel.send(`**New ticket (#${ticket})**\nAuthor: ${author.tag}`); |
| 95 | + await channel.send(`${author.username}: ${text}`); |
| 96 | + |
| 97 | + } // End of new ticket |
| 98 | + channel = guild.channels.cache.get(active.channel); |
| 99 | + // TODO: Do something when channel doesn't exists or no permissions |
| 100 | + channel.send(`${message.author.username}: ${text}`); |
| 101 | + } |
| 102 | + const author = message.author; |
| 103 | + let activechannel = await table.get(`channel_${message.channel.id}`); |
| 104 | + if(activechannel === null) return; // TODO: If no channel is binded, notify mods |
| 105 | + const userID = activechannel.author; |
| 106 | + let activeuser = await table.get(`support_${userID}`); |
| 107 | + let user = await client.users.fetch(userID); |
| 108 | + let args = text.split(" ").slice(1); // use "text" var here |
| 109 | + let pending = args.join(" "); |
| 110 | + let blocked = await table.get(`blocked_${userID}`); |
| 111 | + const prefix = config.prefix; |
| 112 | + |
| 113 | + // Reply |
| 114 | + if(message.content.startsWith(`${prefix}r`) || message.content.startsWith(`${prefix}reply`)){ |
| 115 | + if(blocked) return message.channel.send(strings.thread.blocked) |
| 116 | + if(message.guild.member(user).communicationDisabledUntilTimestamp !== null && config.permissions.disableOnTimeout === true) return message.channel.send(strings.thread.timeout); |
| 117 | + await user.send(`${author.username}: ${pending}`); |
| 118 | + return; |
| 119 | + } |
| 120 | + |
| 121 | + // Get the ID |
| 122 | + if(message.content === `${prefix}id`){ |
| 123 | + return message.channel.send(`The ID of this thread's author is ${userID}.`); |
| 124 | + } |
| 125 | + |
| 126 | + // Block the user |
| 127 | + if(message.content === `${prefix}block`){ |
| 128 | + await table.set(`blocked_${activechannel.author}`, true); |
| 129 | + if(config.permissions.notifyUserOnBlock) await user.send(strings.nowBlocked); |
| 130 | + return message.channel.send(strings.thread.nowBlocked); |
| 131 | + } |
| 132 | + |
| 133 | + // Close the ticket |
| 134 | + if(message.content === `${prefix}close`){ |
| 135 | + let text = `Ticket #${activeuser.ticket}\n\nAuthor: ${user.tag} (${user.id})\n\n`; |
| 136 | + let list = message.channel.messages.cache.map(m => { |
| 137 | + text += `${m.author.tag} (message ${m.id})\n${m.content}\n\n` |
| 138 | + }) |
| 139 | + paste(text).then(async url => { |
| 140 | + // Send log |
| 141 | + try { |
| 142 | + const oldTicketLog = new MessageEmbed() |
| 143 | + .setAuthor(author.tag, author.avatarURL()) |
| 144 | + .setDescription(`Ticket ${ticket} closed\n[Message log](${url})`) |
| 145 | + .setTimestamp().setColor("0x666666") |
| 146 | + let logs = await client.channels.fetch(config.id.logchannel); // set the log channel id here |
| 147 | + logs.send({embeds: [oldTicketLog]}); |
| 148 | + } catch(e) { |
| 149 | + console.warn("Could not send log embed. Ignoring..."); |
| 150 | + } |
| 151 | + // Notify user |
| 152 | + await user.send(strings.nowClosed.replace("{{URL}}", url)); |
| 153 | + }) |
| 154 | + await table.delete(`channel_${message.channel.id}`); |
| 155 | + await table.delete(`support_${activechannel.author}`); |
| 156 | + } |
| 157 | +}) |
| 158 | + |
| 159 | +// Unblock feature |
| 160 | +client.on("messageCreate", async message => { |
| 161 | + if(message.content.startsWith(`${config.prefix}unblock`)){ |
| 162 | + if(message.guild.member(message.author).roles.cache.has(config.roles.mod)){ |
| 163 | + var args = message.content.split(" ").slice(1); |
| 164 | + client.users.fetch(`${args[0]}`).then(async user => { |
| 165 | + const dbTable3 = new db.table("Support13"); |
| 166 | + let data = await dbTable3.get(`blocked_${args[0]}`); |
| 167 | + if(data === true){ |
| 168 | + await dbTable3.delete(`blocked_${args[0]}`); |
| 169 | + return message.channel.send(`Successfully unblocked ${user.username} (${user.id}) from the modmail service.`); |
| 170 | + } else { |
| 171 | + return message.channel.send(`${user.username} (${user.id}) is not blocked from the modmail at the moment.`) |
| 172 | + } |
| 173 | + }).catch(err => { |
| 174 | + if(err) return message.channel.send("Unknown user."); |
| 175 | + }) |
| 176 | + } else { |
| 177 | + return message.channel.send("You can not use that."); |
| 178 | + } |
| 179 | + } |
| 180 | +}) |
0 commit comments