我如何使用 Repl.it 的数据库?

问题描述

用户使用命令时,我需要我的机器人将用户违规保存到数据库中。例如,当使用 #violate @user red light pass 时,机器人应该为提到的用户将红灯传递保存到数据库中。我正在使用 Repl.it's database,这是我的代码

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)
    }
  }
})

解决方法

首先,在`wallet_${memeber}`中,没有定义memeber。您可能打算使用 `wallet_${member}`

我认为问题在于您使用密钥 `wallet_${message.author.id}`(例如 'wallet_12345')来获取违规/罚单,但随后使用 `wallet_${member}`(例如 'wallet_<@12345>')(例如 'wallet) 设置违规。在字符串中插入 GuildMember 调用其 toString 方法,该方法创建成员的 mention 字符串,而不是 id。

此外,它是 good practice to use === instead of == ,因为 == 会投射被比较的项目,因此 3 == '03'[] == false 之类的东西是 true

最终代码应该是这样的:

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);
}