gaber-elsayed

Custom Commands (JS) Database (Mongodb)

Apr 26th, 2021
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.80 KB | None | 0 0
  1.  
  2. ///cc-create command:
  3. const db = require('../../models/CC')
  4. const Discord = require('discord.js')
  5.  
  6. module.exports = {
  7. name: "cc-create",
  8. description: "Creates a custom command!",
  9. category: "config",
  10. usage: "cc-create <name> <response>",
  11. example: "cc-create pog poggies",
  12. aliases: [`ccc`, `cccreate`, `customcommandcreate`, `custom-command-create`],
  13. botPermission: [],
  14. authorPermission: [`MANAGE_GUILD`],
  15. guildOnly: true,
  16. cooldown: "3s",
  17. ownerOnly: false,
  18. run: async (bot, message, args, noPerms) => {
  19.  
  20. const name = args[0]
  21. const response = args.slice(1).join(" ")
  22. if (!name || !response) return message.channel.send(
  23. new Discord.MessageEmbed()
  24. .setAuthor(message.author.tag, message.author.displayAvatarURL({ dynamic: true }))
  25. .setColor(bot.color)
  26. .setDescription(`Please provide a response and a name.`)
  27. .addField(`Usage`, `\`ccc <name> <response>\``)
  28. .addField(`Options`,
  29. `\`{author}\` - pings the message author\n\`{server.name}\` - sends the servers name.\n\`{channel}\` - pings the message channel.\n\`{author.id}\` - sends the authors id.\n\`{author.username}\` - sends the author usernames\n\`{author.name}\` - sends the authors name and tag.\n\`{author.discriminator}\` - sends the author discriminator.\n\`{author.avatar}\` - sends the authors avatar\n\`{author.createdAt}\` - sends the authors creation date\n\`{author.joinedAt}\` - sends when the user joined the server.\n\`{server.id}\` - sends the servers id.\n\`{server.icon}\` - sends the servers icon\n\`{server.members}\` - sends the servers members\n\`{server.ownerID}\` - sends the servers owner id\n\`{channel.id}\` - sends the message channels id\n\`{channel.name}\` - sends the channels name`
  30. )
  31. )
  32.  
  33. await db.findOne({ Guild: message.guild.id, Command: name }, async (err, data) => {
  34. if (err) throw err
  35. if (data) {
  36. return message.channel.send(`This custom command already exists.`)
  37. } else if (!data) {
  38. if (response.includes(`{message}`)) {
  39. new db({
  40. Guild: message.guild.id,
  41. Command: name,
  42. Response: response,
  43. Req: true
  44. }).save()
  45. return message.channel.send(
  46. new Discord.MessageEmbed()
  47. .setAuthor(`Custom Command Created`, message.author.displayAvatarURL({ dynamic: true }))
  48. .setColor(bot.color)
  49. .addField(`Command`, name, true)
  50. .addField(`Required Argument`, `True`, true)
  51. .addField(`Response`, `\`\`\`\n${response}\n\`\`\``, true)
  52.  
  53. )
  54. } else {
  55. new db({
  56. Guild: message.guild.id,
  57. Command: name,
  58. Response: response
  59. }).save()
  60. return message.channel.send(
  61. new Discord.MessageEmbed()
  62. .setAuthor(`Custom Command Created`, message.author.displayAvatarURL({ dynamic: true }))
  63. .setColor(bot.color)
  64. .addField(`Command`, name)
  65. .addField(`Response`, `\`\`\`\n${response}\n\`\`\``)
  66. )
  67. }
  68. }
  69. })
  70. }
  71. }
  72. /////cc-delete command:
  73.  
  74. const db = require('../../models/CC')
  75.  
  76. module.exports = {
  77. name: "cc-delete",
  78. description: "Deleted a custom command!",
  79. category: "config",
  80. usage: "cc-delete <name>",
  81. example: "cc-create pog",
  82. aliases: [`ccd`, `ccdelete`, `customcommanddelete`, `custom-command-delete`],
  83. botPermission: [],
  84. authorPermission: [`MANAGE_GUILD`],
  85. guildOnly: true,
  86. cooldown: "3s",
  87. ownerOnly: false,
  88. run: async (bot, message, args, noPerms) => {
  89.  
  90. const name = args[0]
  91. if (!name) return noPerms(bot, message, `Please provide a name.`, `cc-delete <name>`)
  92.  
  93. await db.findOne({ Guild: message.guild.id, Command: name }, async (err, data) => {
  94. if (err) throw err
  95. if (!data) return message.channel.send(`\`${name}\` does not exists.`)
  96. else if (data) {
  97. await db.findOneAndDelete({ Guild: message.guild.id, Command: name })
  98. return message.channel.send(`Deleted \`${name}\`.`)
  99. }
  100. })
  101. }
  102. }
  103.  
  104.  
  105.  
  106. ///cc-list command:
  107. const db = require('../../models/CC')
  108. const Discord = require('discord.js')
  109.  
  110. module.exports = {
  111. name: "cc-list",
  112. description: "Lists all the custom commands!",
  113. category: "config",
  114. usage: "",
  115. example: "",
  116. aliases: [`ccl`, `cclist`, `customcommandlist`, `custom-command-list`],
  117. botPermission: [],
  118. authorPermission: [],
  119. guildOnly: true,
  120. cooldown: "3s",
  121. ownerOnly: false,
  122. run: async (bot, message, args) => {
  123.  
  124. const data = await db.find({ Guild: message.guild.id })
  125. if (data === false) return message.channel.send(`There are no custom commands in the server.`)
  126. message.channel.send(
  127. new Discord.MessageEmbed()
  128. .setTitle(`\`${message.guild.name}\`'s Custom Commands`)
  129. .setThumbnail(message.guild.iconURL({ dynamic: true }))
  130. .setColor(bot.color)
  131. .setDescription(data.map((cmd, i) =>
  132. `**${i + 1}.** ${cmd.Command}`
  133. ).join("\n"))
  134. )
  135. }
  136. }
  137.  
  138. ///the mongoose model:
  139. const { Schema, model } = require('mongoose')
  140. const data = Schema({
  141. Guild: String,
  142. Command: String,
  143. Response: String,
  144. Req: { type: String, require: false }
  145. })
  146. module.exports = model('custom-commands', data)
  147.  
  148.  
  149. /////put this in your message event:
  150.  
  151.  
  152. const ccDb = require('../models/CC')
  153.  
  154. const ccData = await ccDb.findOne({ Guild: message.guild.id, Command: cmd })
  155. if (ccData) {
  156. if (ccData.Response.includes(`{embed}`) && ccData.Response.includes(`{message}`)) {
  157. if (!args[0]) return message.channel.send(`The \`${ccData.Command}\` required you to provide an argument. Ex: \`${ccData.Command} pog\`.`)
  158. let newResponse = ccData.Response
  159. .replace(`{author}`, message.author)
  160. .replace(`{server.name}`, message.guild.name)
  161. .replace(`{channel}`, message.channel)
  162. .replace(`{author.id}`, message.author.id)
  163. .replace(`{author.username}`, message.author.username)
  164. .replace(`{author.name}`, message.author.tag)
  165. .replace(`{author.discriminator}`, message.author.discriminator)
  166. .replace(`{author.avatar}`, message.author.displayAvatarURL({ dynamic: true }))
  167. .replace(`{author.createdAt}`, message.author.createdAt.toString())
  168. .replace(`{author.joinedAt}`, message.member.joinedAt.toString())
  169. .replace(`{server.id}`, message.guild.id)
  170. .replace(`{server.icon}`, message.guild.iconURL({ dynamic: true }))
  171. .replace(`{server.members}`, message.guild.memberCount)
  172. .replace(`{server.ownerID}`, message.guild.ownerID)
  173. .replace(`{channel.id}`, message.channel.id)
  174. .replace(`{channel.name}`, message.channel.name)
  175. .replace(`{message}`, args[0])
  176. .replace(`{embed}`, '')
  177. return message.channel.send(new MessageEmbed().setDescription(newResponse).setColor(bot.color))
  178. } else if (ccData.Response.includes(`{embed}`)) {
  179. let newResponse = ccData.Response
  180. .replace(`{author}`, message.author)
  181. .replace(`{server.name}`, message.guild.name)
  182. .replace(`{channel}`, message.channel)
  183. .replace(`{author.id}`, message.author.id)
  184. .replace(`{author.username}`, message.author.username)
  185. .replace(`{author.name}`, message.author.tag)
  186. .replace(`{author.discriminator}`, message.author.discriminator)
  187. .replace(`{author.avatar}`, message.author.displayAvatarURL({ dynamic: true }))
  188. .replace(`{author.createdAt}`, message.author.createdAt.toString())
  189. .replace(`{author.joinedAt}`, message.member.joinedAt.toString())
  190. .replace(`{server.id}`, message.guild.id)
  191. .replace(`{server.icon}`, message.guild.iconURL({ dynamic: true }))
  192. .replace(`{server.members}`, message.guild.memberCount)
  193. .replace(`{server.ownerID}`, message.guild.ownerID)
  194. .replace(`{channel.id}`, message.channel.id)
  195. .replace(`{channel.name}`, message.channel.name)
  196. .replace(`{embed}`, '')
  197. return message.channel.send(new MessageEmbed().setDescription(newResponse).setColor(bot.color))
  198. } else if (ccData.Response.includes(`{message}`)) {
  199. if (!args[0]) return message.channel.send(`The \`${ccData.Command}\` required you to provide an argument. Ex: \`${ccData.Command} pog\`.`)
  200. let newResponse = ccData.Response
  201. .replace(`{author}`, message.author)
  202. .replace(`{server.name}`, message.guild.name)
  203. .replace(`{channel}`, message.channel)
  204. .replace(`{author.id}`, message.author.id)
  205. .replace(`{author.username}`, message.author.username)
  206. .replace(`{author.name}`, message.author.tag)
  207. .replace(`{author.discriminator}`, message.author.discriminator)
  208. .replace(`{author.avatar}`, message.author.displayAvatarURL({ dynamic: true }))
  209. .replace(`{author.createdAt}`, message.author.createdAt.toString())
  210. .replace(`{author.joinedAt}`, message.member.joinedAt.toString())
  211. .replace(`{server.id}`, message.guild.id)
  212. .replace(`{server.icon}`, message.guild.iconURL({ dynamic: true }))
  213. .replace(`{server.members}`, message.guild.memberCount)
  214. .replace(`{server.ownerID}`, message.guild.ownerID)
  215. .replace(`{channel.id}`, message.channel.id)
  216. .replace(`{channel.name}`, message.channel.name)
  217. .replace(`{message}`, args[0])
  218. return message.channel.send(newResponse)
  219. } else {
  220. let newResponse = ccData.Response
  221. .replace(`{author}`, message.author)
  222. .replace(`{server.name}`, message.guild.name)
  223. .replace(`{channel}`, message.channel)
  224. .replace(`{author.id}`, message.author.id)
  225. .replace(`{author.username}`, message.author.username)
  226. .replace(`{author.name}`, message.author.tag)
  227. .replace(`{author.discriminator}`, message.author.discriminator)
  228. .replace(`{author.avatar}`, message.author.displayAvatarURL({ dynamic: true }))
  229. .replace(`{author.createdAt}`, message.author.createdAt.toString())
  230. .replace(`{author.joinedAt}`, message.member.joinedAt.toString())
  231. .replace(`{server.id}`, message.guild.id)
  232. .replace(`{server.icon}`, message.guild.iconURL({ dynamic: true }) || "This server doesn't have a icon.")
  233. .replace(`{server.members}`, message.guild.memberCount)
  234. .replace(`{server.ownerID}`, message.guild.ownerID)
  235. .replace(`{channel.id}`, message.channel.id)
  236. .replace(`{channel.name}`, message.channel.name)
  237. return message.channel.send(newResponse)
  238. }
  239. }
  240.  
Advertisement
Add Comment
Please, Sign In to add comment