How to create a warn system?

Hello, I would like to know if someone will know how to make a system of warns that will work on several servers. I already have a warn system but it does not register the server or the user has been warn. As an example I would like to do something like this: if I warn someone on my server and I warn him another pars he will have 2 warn instead of having 1 warn on one server and 1 warn another pars. For precision: my warn syste function with enmap.

Can you share your project name or code of your warn system?

If you are saving the warnings in a database you should add the ID of the guild and then make the bot check if the user was warn in that guild

const Command = require("../../modules/Command.js");

const ms = require(“ms”);

class Warn extends Command {
constructor(client) {
super(client, {
name: “warn”,
description: “Commande pour avertir un utilisateur.”,
usage: “warn”,
category: “Système”,
permLevel: “Modérateur”
});
}

async run(message, args) {
try {
const warnedUser = message.guild.member(
message.mentions.users.first() || message.guild.members.get(args[0])
);
if (!warnedUser)
return message.channel.send(“L’utilisateur n’existe pas.”);
const warnToAdd = 1;
const warnToDel = 5;
const reason = args.join(" ").slice(22);
this.client.warns.ensure(${warnedUser.id}, {
warnings: 0
});
let userWarnings = this.client.warns.get(${warnedUser.id}, “warnings”);
userWarnings += warnToAdd;

  this.client.warns.set(`${warnedUser.id}`, userWarnings, "warnings");

  message.delete();

  if (this.client.warns.get(`${warnedUser.id}`, "warnings") == 1) {
    message.channel.send(
      `${warnedUser}, premier avertissement (raison: ${reason})`
    );
  } else if (this.client.warns.get(`${warnedUser.id}`, "warnings") == 2) {
    const muteRole = message.guild.roles.find(x => x.name === "muted");
    if (!muteRole) message.guild.createRole("name", "muted");
    message.channel.send(
      `${warnedUser}, deuxième avertissement (raison: ${reason})`
    );
    const muteTime = "1h";
    await warnedUser.addRole(muteRole.id);
    message.channel.send(
      `${warnedUser} est muté pendant ${muteTime} (raison: ${reason})`
    );

    setTimeout(function() {
      warnedUser.removeRole(muteRole.id);
      message.channel.send(`L'utilisateur ${warnedUser} n'est plus muté !`);
    }, ms(muteTime));
  } else if (this.client.warns.get(`${warnedUser.id}`, "warnings") == 3) {
    message.channel.send(`${warnedUser}, troisième avertissement (raison: ${reason})`);
    message.channel.send(`:warning: Attention ${warnedUser}, à 5 avertissement sa sera ban définitif`);
  } else if (this.client.warns.get(`${warnedUser.id}`, "warnings") == 4) {
    message.channel.send(`${warnedUser}, quatrième avertissement profite d'un kick (raison: ${reason})`);
    warnedUser.kick(reason);
  } else if (this.client.warns.get(`${warnedUser.id}`, "warnings") == 5) {
    message.channel.send(`${warnedUser}, cinquième avertissement comme je te l'avait dit au cinquième warn j'allais te bannir donc au revoir`);
    warnedUser.ban(reason);
    userWarnings -= warnToDel;
  }
} catch (e) {
  console.log(e);
}

}
}

module.exports = Warn;

@cori excuse me for disturbance i have a problem:
I can not indent the code as it should

This is your code:

I can see you save the data with warnedUser.id but you should also save the warnedGuild.id

const warnedGuild = message.guild;

So you will have in your data this:

warnedUser.id: 456789
warnedGuild.id: 123456
warnings: 1

Then you can see if the user have a warning in that guild.

1 Like

I don’t understand can you repeat or correct my code please ? if this not disturb you…

We can’t really do your code for you, that’s spoon feeding. It’s not good to get spoon fed when you’re just beginning, could become a habit to just to get spoon fed later on and you won’t learn much. Honestly though, check out the official discord.js discord and ask for help there, they’ll give quicker support, and lead you in the right direction without spoon feeding. Check it out: https://discord.gg/bRCvFy9

2 Likes