Advertisement
ala89

TUTO DEV #9 - CREER UN BOT DISCORD : SYSTEME DE WARN

Jul 13th, 2020 (edited)
5,210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // index.js
  2. const Discord = require('discord.js'),
  3.     client = new Discord.Client({
  4.         fetchAllMembers: true,
  5.         partials: ['MESSAGE', 'REACTION']
  6.     }),
  7.     config = require('./config.json'),
  8.     fs = require('fs'),
  9.     humanizeDuration = require('humanize-duration'),
  10.     cooldown = new Set()
  11.  
  12. client.login(config.token)
  13. client.commands = new Discord.Collection()
  14. client.db = require('./db.json')
  15.  
  16. fs.readdir('./commands', (err, files) => {
  17.     if (err) throw err
  18.     files.forEach(file => {
  19.         if (!file.endsWith('.js')) return
  20.         const command = require(`./commands/${file}`)
  21.         client.commands.set(command.name, command)
  22.     })
  23. })
  24.  
  25. client.on('message', message => {
  26.     if (message.type !== 'DEFAULT' || message.author.bot) return
  27.  
  28.     if (!message.member.hasPermission('MANAGE_MESSAGES')) {
  29.         const duration = config.cooldown[message.channel.id]
  30.         if (duration) {
  31.             const id = `${message.channel.id}_${message.author.id}`
  32.             if (cooldown.has(id)) {
  33.                 message.delete()
  34.                 return message.channel.send(`Ce salon est soumis a un cooldown de ${humanizeDuration(duration, {language: 'fr'})}.`).then(sent => sent.delete({timeout: 5e3}))
  35.             }
  36.             cooldown.add(id)
  37.             setTimeout(() => cooldown.delete(id), duration)
  38.         }
  39.     }
  40.  
  41.     const args = message.content.trim().split(/ +/g)
  42.     const commandName = args.shift().toLowerCase()
  43.     if (!commandName.startsWith(config.prefix)) return
  44.     const command = client.commands.get(commandName.slice(config.prefix.length))
  45.     if (!command) return
  46.     if (command.guildOnly && !message.guild) return message.channel.send('Cette commande ne peut être utilisée que dans un serveur.')
  47.     command.run(message, args, client)
  48. })
  49.  
  50. client.on('guildMemberAdd', member => {
  51.     member.guild.channels.cache.get(config.greeting.channel).send(`${member}`, new Discord.MessageEmbed()
  52.         .setDescription(`${member} a rejoint le serveur. Nous sommes désormais ${member.guild.memberCount} ! 🎉`)
  53.         .setColor('#00ff00'))
  54.     member.roles.add(config.greeting.role)
  55. })
  56.  
  57. client.on('guildMemberRemove', member => {
  58.     member.guild.channels.cache.get(config.greeting.channel).send(new Discord.MessageEmbed()
  59.         .setDescription(`${member.user.tag} a quitté le serveur... 😢`)
  60.         .setColor('#ff0000'))
  61. })
  62.  
  63. client.on('messageReactionAdd', (reaction, user) => {
  64.     if (!reaction.message.guild || user.bot) return
  65.     const reactionRoleElem = config.reactionRole[reaction.message.id]
  66.     if (!reactionRoleElem) return
  67.     const prop = reaction.emoji.id ? 'id' : 'name'
  68.     const emoji = reactionRoleElem.emojis.find(emoji => emoji[prop] === reaction.emoji[prop])
  69.     if (emoji) reaction.message.guild.member(user).roles.add(emoji.roles)
  70.     else reaction.users.remove(user)
  71. })
  72.  
  73. client.on('messageReactionRemove', (reaction, user) => {
  74.     if (!reaction.message.guild || user.bot) return
  75.     const reactionRoleElem = config.reactionRole[reaction.message.id]
  76.     if (!reactionRoleElem || !reactionRoleElem.removable) return
  77.     const prop = reaction.emoji.id ? 'id' : 'name'
  78.     const emoji = reactionRoleElem.emojis.find(emoji => emoji[prop] === reaction.emoji[prop])
  79.     if (emoji) reaction.message.guild.member(user).roles.remove(emoji.roles)
  80. })
  81.  
  82. client.on('ready', () => {
  83.     const statuses = [
  84.         () => `${client.guilds.cache.size} serveurs`,
  85.         () => `${client.guilds.cache.reduce((acc, guild) => acc + guild.memberCount, 0)} utilisateurs`
  86.     ]
  87.     let i = 0
  88.     setInterval(() => {
  89.         client.user.setActivity(statuses[i](), {type: 'PLAYING'})
  90.         i = ++i % statuses.length
  91.     }, 1e4)
  92. })
  93.  
  94. client.on('channelCreate', channel => {
  95.     if (!channel.guild) return
  96.     const muteRole = channel.guild.roles.cache.find(role => role.name === 'Muted')
  97.     if (!muteRole) return
  98.     channel.createOverwrite(muteRole, {
  99.         SEND_MESSAGES: false,
  100.         CONNECT: false,
  101.         ADD_REACTIONS: false
  102.     })
  103. })
  104.  
  105. // warn.js
  106. const fs = require('fs')
  107.  
  108. module.exports = {
  109.     run: async (message, args, client) => {
  110.         if (!message.member.hasPermission('MANAGE_MESSAGES')) return message.channel.send('Vous n\'avez pas la permission d\'utiliser cette commande.')
  111.         const member = message.mentions.members.first()
  112.         if (!member) return message.channel.send('Veuillez mentionner le membre à warn.')
  113.         if (member.id === message.guild.ownerID) return message.channel.send('Vous ne pouvez pas warn le propriétaire du serveur.')
  114.         if (message.member.roles.highest.comparePositionTo(member.roles.highest) < 1 && message.author.id !== message.guild.ownerID) return message.channel.send('Vous ne pouvez pas warn ce membre.')
  115.         const reason = args.slice(1).join(' ')
  116.         if (!reason) return message.channel.send('Veuillez indiquer une raison.')
  117.         if (!client.db.warns[member.id]) client.db.warns[member.id] = []
  118.         client.db.warns[member.id].unshift({
  119.             reason,
  120.             date: Date.now(),
  121.             mod: message.author.id
  122.         })
  123.         fs.writeFileSync('./db.json', JSON.stringify(client.db))
  124.         message.channel.send(`${member} a été warn pour ${reason} !`)
  125.     },
  126.     name: 'warn',
  127.     guildOnly: true
  128. }
  129.  
  130. // infractions.js
  131. const moment = require('moment'),
  132.     Discord = require('discord.js')
  133.  
  134. moment.locale('fr')
  135.  
  136. module.exports = {
  137.     run: async (message, args, client) => {
  138.         if (!message.member.hasPermission('MANAGE_MESSAGES')) return message.channel.send('Vous n\'avez pas la permission d\'utiliser cette commande.')
  139.         const member = message.mentions.members.first()
  140.         if (!member) return message.channel.send('Veuillez mentionner le membre dont voir les warns.')
  141.         if (!client.db.warns[member.id]) return message.channel.send('Ce membre n\'a aucun warn.')
  142.         message.channel.send(new Discord.MessageEmbed()
  143.             .setDescription(`**Total de warns :** ${client.db.warns[member.id].length}\n\n__**10 derniers warns**__\n\n${client.db.warns[member.id].slice(0, 10).map((warn, i) => `**${i + 1}.** ${warn.reason}\nSanctionné ${moment(warn.date).fromNow()} par <@!${warn.mod}>`).join('\n\n')}`))
  144.     },
  145.     name: 'infractions',
  146.     guildOnly: true
  147. }
  148.  
  149. // unwarn.js
  150. const fs = require('fs')
  151.  
  152. module.exports = {
  153.     run: async (message, args, client) => {
  154.         if (!message.member.hasPermission('MANAGE_MESSAGES')) return message.channel.send('Vous n\'avez pas la permission d\'utiliser cette commande.')
  155.         const member = message.mentions.members.first()
  156.         if (!member) return message.channel.send('Veuillez mentionner le membre à unwarn.')
  157.         if (!client.db.warns[member.id]) return message.channel.send('Ce membre n\'a aucun warn.')
  158.         const warnIndex = parseInt(args[1], 10) - 1
  159.         if (warnIndex < 0 || !client.db.warns[member.id][warnIndex]) return message.channel.send('Ce warn n\'existe pas.')
  160.         const { reason } = client.db.warns[member.id].splice(warnIndex, 1)[0]
  161.         if (!client.db.warns[member.id].length) delete client.db.warns[member.id]
  162.         fs.writeFileSync('./db.json', JSON.stringify(client.db))
  163.         message.channel.send(`${member} a été unwarn pour ${reason} !`)
  164.     },
  165.     name: 'unwarn',
  166.     guildOnly: true
  167. }
  168.  
  169. // db.json
  170. {"warns":{}}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement