zein69

modmail

Jan 12th, 2024
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const Discord = require('discord.js')
  2.  
  3. async function modmail(client, options = []) {
  4.   let { MessageButton, MessageActionRow } = require('discord.js')
  5.  
  6.  
  7. let guild = client.guilds.cache.get(options.guildID);
  8.  
  9. if (!guild) throw new Error("No Guild!");
  10.  
  11.  
  12.  
  13.   client.on("messageCreate", async(message) => {
  14.  
  15. if (message.author.bot) return;
  16. if (message.channel.type === "DM") {
  17.     let mailName = `${message.author.id}`
  18.  
  19. let usersChannel = await guild.channels.cache.find(ch => ch.name === mailName.toLowerCase());
  20.  
  21.  
  22.  
  23. if (!usersChannel) {
  24.  
  25.   const createdEmbed = new Discord.MessageEmbed()
  26.   .setAuthor(`${message.author.tag}`, message.author.displayAvatarURL({ dynamic: true }))
  27.   .setTitle("No Mail Opened")
  28.   .setDescription(message.content)
  29.   .setColor(options.embedColor)
  30.  
  31.  
  32.  
  33.   let categ = guild.channels.cache.get(options.categoryID)
  34.  
  35.  
  36.  
  37. if (!categ) throw new Error("No Category!")
  38.  
  39.  
  40. if (!options.staffRole) throw new Error("No Role!")
  41.  
  42.  
  43.     let permissions = {
  44.       id: options.staffRole,
  45.       allow: ['VIEW_CHANNEL', 'SEND_MESSAGES', 'READ_MESSAGE_HISTORY']
  46.     }
  47.  
  48.  
  49.   guild.channels.create({
  50.     name: message.author.displayName,
  51.     type: 'text',
  52.     parent: categ,
  53.     permissionOverwrites: [
  54.       {
  55.         id: guild.roles.everyone,
  56.         deny: ['VIEW_CHANNEL', 'SEND_MESSAGES', 'READ_MESSAGE_HISTORY'] //Deny permissions
  57.       },
  58.      permissions
  59.     ],
  60.   }).then(async (ch) => {
  61.  
  62.  
  63. let role = ch.guild.roles.cache.find((r) => r.id == options.staffRole)
  64.  
  65.  
  66. if (!role) throw new Error("No role!")
  67.  
  68. const openedUserEmbed = new Discord.MessageEmbed()
  69. .setAuthor(`${message.author.tag}`, message.author.displayAvatarURL({ dynamic: true }))
  70. .setTitle(`${options.userOpenedTitle}`)
  71. .setDescription(`${options.userOpenedMessage}`)
  72. .setTimestamp()
  73. .setColor(options.embedColor)
  74.     message.author.send({ embeds: [openedUserEmbed] })
  75.  
  76.  
  77.     let usersCreatedChannel = await guild.channels.cache.find(ch => ch.name === mailName.toLowerCase());
  78.  
  79.  
  80.  
  81. let delButton = new MessageButton()
  82. .setStyle("DANGER")
  83. .setLabel('Delete')
  84. .setCustomId('close_mail')
  85. .setEmoji(`${options.wrongEmoji}` || '❌')
  86.  
  87.  
  88.  
  89. let deleteRow = new MessageActionRow()
  90. .addComponents([delButton])
  91.  
  92. const openedStaffEmbed = new Discord.MessageEmbed()
  93. .setAuthor(`${message.author.tag}`, message.author.displayAvatarURL({ dynamic: true }))
  94. .setTitle(`${options.staffOpenedTitle}`)
  95. .setDescription(`${options.staffOpenedMessage}\n**User: ${message.author.tag} (${message.author.name})**\n\n ${message.content}`)
  96. .setTimestamp()
  97. .setColor(options.embedColor)
  98.  
  99. usersCreatedChannel.send({ embeds: [openedStaffEmbed], components: [deleteRow] })
  100.   })
  101.  
  102.  
  103. } else {
  104.  
  105.     let usersHadChannel = await guild.channels.cache.find(ch => ch.name === mailName.toLowerCase());
  106.  
  107.  
  108. const userHadEmbed = new Discord.MessageEmbed()
  109. .setAuthor(`${message.author.tag}`, message.author.displayAvatarURL({ dynamic: true }))
  110. .setTitle(`${message.content}`)
  111. .setTimestamp()
  112. .setColor(options.embedColor)
  113.  
  114.  
  115. usersHadChannel.send({ embeds: [userHadEmbed] })
  116. }
  117.  
  118. // Sent In DM's //
  119. } else {
  120.   if (message.channel.type === "GUILD_TEXT") {
  121.  
  122. let categor = guild.channels.cache.get(options.categoryID)
  123.  
  124.  
  125.     if (message.channel.parentId !== categor.id) return;
  126.  
  127.     const usertosend = message.guild.members.cache.find((user) => user.id == message.channel.name)
  128.  
  129.     if (!usertosend) return;
  130.  
  131. if (options.anonymousReply === true) {
  132.  
  133.  
  134.     const staffSendEmbedA = new Discord.MessageEmbed()
  135.     .setAuthor(`Odpowiedź od administratora`)
  136.     .setTitle(`${message.content}`)
  137.     .setTimestamp()
  138.     .setColor(options.embedColor)
  139.  
  140.     usertosend.send({ embeds: [staffSendEmbedA] })
  141.  
  142. } else {
  143.     const staffSendEmbed = new Discord.MessageEmbed()
  144.     .setAuthor(`${message.author.tag}`, message.author.displayAvatarURL())
  145.     .setTitle(`${message.content}`)
  146.     .setTimestamp()
  147.     .setColor(options.embedColor)
  148.  
  149.     usertosend.send({ embeds: [staffSendEmbed] })
  150. }
  151.   }
  152. }
  153.   })
  154.  
  155.  
  156.  
  157. // Channel Deleted //
  158.  
  159. client.on("channelDelete", (channel) => {
  160.  
  161.   let category = guild.channels.cache.get(options.categoryID)
  162.  
  163.  
  164. if (channel.parentId !== category.id) return;
  165.  
  166. const user = channel.guild.members.cache.find((user) => user.id == channel.name)
  167.  
  168. if (!user) return;
  169.  
  170. const deletedEmbed = new Discord.MessageEmbed()
  171. .setTitle(`${options.closedTitle}`)
  172. .setDescription(`${options.closedMessage}`)
  173. .setColor(options.embedColor)
  174.  
  175.  
  176. user.send({ embeds: [deletedEmbed] })
  177.  
  178.  
  179. });
  180.  
  181.  
  182.  
  183.  
  184.  
  185. // Delete Buttons //
  186.  
  187.   let confirmButton = new MessageButton()
  188. .setStyle("SUCCESS")
  189. .setLabel('Confirm')
  190. .setCustomId('confirm_mail')
  191. .setEmoji(`${options.rightEmoji}` || '✔️')
  192.  
  193.  
  194.   let cancleButton = new MessageButton()
  195. .setStyle("SECONDARY")
  196. .setLabel('Cancle')
  197. .setCustomId('cancle_mail')
  198. .setEmoji(`${options.wrongEmoji}` || '❌')
  199.  
  200.  
  201. let optionsRow = new MessageActionRow()
  202. .addComponents([confirmButton])
  203. .addComponents([cancleButton])
  204.  
  205.  
  206. client.on('interactionCreate', interaction => {
  207.  
  208.  
  209.  
  210. if (interaction.customId === "close_mail") {
  211. interaction.update({ components: [optionsRow]})
  212.  
  213. } else {
  214.  
  215.   if (interaction.customId === "cancle_mail") {
  216.  
  217. let delButton2 = new MessageButton()
  218. .setStyle("DANGER")
  219. .setLabel('Delete')
  220. .setCustomId('close_mail')
  221. .setEmoji('❌')
  222.  
  223.  
  224.  
  225. let deleteRow2 = new MessageActionRow()
  226. .addComponents([delButton2])
  227.  
  228.  
  229.  
  230. interaction.update({ components: [deleteRow2]})
  231.  
  232.  
  233.   } else {
  234.     if (interaction.customId === "confirm_mail") {
  235.       interaction.message.channel.delete();
  236.     }
  237.   }
  238. }
  239.  
  240.   });
  241.  
  242. }
  243.  
  244.   module.exports = modmail;
Advertisement
Add Comment
Please, Sign In to add comment