Advertisement
ala89

TUTO DEV #6 - CREER UN BOT DISCORD : STATUT DE BOT DYNAMIQUE

Jun 21st, 2020 (edited)
5,710
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.  
  15. fs.readdir('./commands', (err, files) => {
  16.     if (err) throw err
  17.     files.forEach(file => {
  18.         if (!file.endsWith('.js')) return
  19.         const command = require(`./commands/${file}`)
  20.         client.commands.set(command.name, command)
  21.     })
  22. })
  23.  
  24. client.on('message', message => {
  25.     if (message.type !== 'DEFAULT' || message.author.bot) return
  26.  
  27.     if (!message.member.hasPermission('MANAGE_MESSAGES')) {
  28.         const duration = config.cooldown[message.channel.id]
  29.         if (duration) {
  30.             const id = `${message.channel.id}_${message.author.id}`
  31.             if (cooldown.has(id)) {
  32.                 message.delete()
  33.                 return message.channel.send(`Ce salon est soumis a un cooldown de ${humanizeDuration(duration, {language: 'fr'})}.`).then(sent => sent.delete({timeout: 5e3}))
  34.             }
  35.             cooldown.add(id)
  36.             setTimeout(() => cooldown.delete(id), duration)
  37.         }
  38.     }
  39.  
  40.     const args = message.content.trim().split(/ +/g)
  41.     const commandName = args.shift().toLowerCase()
  42.     if (!commandName.startsWith(config.prefix)) return
  43.     const command = client.commands.get(commandName.slice(config.prefix.length))
  44.     if (!command) return
  45.     if (command.guildOnly && !message.guild) return message.channel.send('Cette commande ne peut être utilisée que dans un serveur.')
  46.     command.run(message, args, client)
  47. })
  48.  
  49. client.on('guildMemberAdd', member => {
  50.     member.guild.channels.cache.get(config.greeting.channel).send(`${member}`, new Discord.MessageEmbed()
  51.         .setDescription(`${member} a rejoint le serveur. Nous sommes désormais ${member.guild.memberCount} ! 🎉`)
  52.         .setColor('#00ff00'))
  53.     member.roles.add(config.greeting.role)
  54. })
  55.  
  56. client.on('guildMemberRemove', member => {
  57.     member.guild.channels.cache.get(config.greeting.channel).send(new Discord.MessageEmbed()
  58.         .setDescription(`${member.user.tag} a quitté le serveur... 😢`)
  59.         .setColor('#ff0000'))
  60. })
  61.  
  62. client.on('messageReactionAdd', (reaction, user) => {
  63.     if (!reaction.message.guild || user.bot) return
  64.     const reactionRoleElem = config.reactionRole[reaction.message.id]
  65.     if (!reactionRoleElem) return
  66.     const prop = reaction.emoji.id ? 'id' : 'name'
  67.     const emoji = reactionRoleElem.emojis.find(emoji => emoji[prop] === reaction.emoji[prop])
  68.     if (emoji) reaction.message.guild.member(user).roles.add(emoji.roles)
  69.     else reaction.users.remove(user)
  70. })
  71.  
  72. client.on('messageReactionRemove', (reaction, user) => {
  73.     if (!reaction.message.guild || user.bot) return
  74.     const reactionRoleElem = config.reactionRole[reaction.message.id]
  75.     if (!reactionRoleElem || !reactionRoleElem.removable) return
  76.     const prop = reaction.emoji.id ? 'id' : 'name'
  77.     const emoji = reactionRoleElem.emojis.find(emoji => emoji[prop] === reaction.emoji[prop])
  78.     if (emoji) reaction.message.guild.member(user).roles.remove(emoji.roles)
  79. })
  80.  
  81. client.on('ready', () => {
  82.     const statuses = [
  83.         () => `${client.guilds.cache.size} serveurs`,
  84.         () => `${client.guilds.cache.reduce((acc, guild) => acc + guild.memberCount, 0)} utilisateurs`
  85.     ]
  86.     let i = 0
  87.     setInterval(() => {
  88.         client.user.setActivity(statuses[i](), {type: 'PLAYING'})
  89.         i = ++i % statuses.length
  90.     }, 1e4)
  91. })
  92.  
  93. // config.js
  94. {
  95.     "token": "votre token",
  96.     "prefix": "!",
  97.     "greeting": {
  98.         "channel": "718970110164992041",
  99.         "role": "718970160647635056"
  100.     },
  101.     "reactionRole": {
  102.         "721812281347932311": {
  103.             "emojis": [{
  104.                 "id": "719563468008718348",
  105.                 "roles": "722404495371534358"
  106.             }]
  107.         },
  108.         "721812680763244596": {
  109.             "removable": true,
  110.             "emojis": [{
  111.                 "name": "💻",
  112.                 "roles": ["722088168458813584", "722088234963828747"]
  113.             }, {
  114.                 "name": "🎮",
  115.                 "roles": "722088255402672139"
  116.             }]
  117.         }
  118.     },
  119.     "cooldown": {
  120.         "723998402395767288": 1e4,
  121.         "723998419986808883": 3e4
  122.     }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement