-4

I need my bot to save user violations to a database when a user uses a command. For example, when #violate @user red light pass is used, the bot should save the red light pass to the database for the mentioned user. I'm using Repl.it's database and this is my code:

client.on("message", async message => {
  if (message.content.toLowerCase().startsWith("#violations")) {
    let violation = await db.get(`wallet_${message.author.id}`)

    if (violation === null) violation = "you dont have any tickets"

    let pembed = new Discord.MessageEmbed()
      .setTitle(`${message.author.username} violations`)
      .setDescription(`المخالفات المرورية : ${violation}`)
      .setColor("RANDOM")
      .setFooter("L.S.P.D")
    message.channel.send(pembed)
  }

  if (message.channel.id === '844933512514633768') {
    if (!message.content.startsWith(prefix) || message.author.bot) return;

    const args = message.content.slice(prefix.length).split(/ +/);
    console.log(args);

    const command = args.shift().toLowerCase();

    if (command == 'red light pass') {
      const member = message.mentions.members.first(); // 
      let light = "red lightpass"
      await db.set(`wallet_${memeber}`, light)
    }
  }
})
Lauren Yim
  • 12,700
  • 2
  • 32
  • 59

1 Answers1

0

First of all, in `wallet_${memeber}`, memeber is not defined. You probably intended to use `wallet_${member}`.

I believe the issue is you're using the key `wallet_${message.author.id}` (e.g. 'wallet_12345') to get the violation/ticket, but then `wallet_${member}` (e.g. 'wallet_<@12345>') (e.g. 'wallet) to set the violation. Interpolating a GuildMember in a string calls its toString method, which creates a string of the mention of the member, not the id.

Also, it's good practice to use === instead of == as == casts the items being compared so things like 3 == '03' and [] == false are true.

The final code should like this:

if (command === "red light pass") {
  const member = message.mentions.members.first();
  const light = "red lightpass";
  // Note how I've changed this to member.id (which is just a shortcut for
  // member.user.id) from memeber
  await db.set(`wallet_${member.id}`, light);
}
Lauren Yim
  • 12,700
  • 2
  • 32
  • 59