'AFK command with a working set status quick.db

I've been trying to make this command work for a very long time now. I've been trying to make it send the author's status on "trying a fix".

I don't know if I can use quick.db for this but I have been trying to save the author's status with db.set(message.author.id + statusmessage) but I don't know how to insert it in the code.

On "trying a fix" should come the author's status, so when people ping them it says their set status.

const Discord = require("discord.js");
const db = require("quick.db");

const client = new Discord.Client();
client.on("message", async (message) => {
    if (message.author.bot) return false;

    if (db.has(message.author.id + 'yuki afk')) {;
        message.channel.send(`${message.author}, Welcome back I removed your AFK <:yuki_:828071206641074199>`)
  .then(message =>
            message.delete({ timeout: 10000 })
        )
        db.delete(message.author.id + 'yuki afk');
    };
    if (message.content.toLocaleLowerCase().startsWith('yuki afk')) {
        let sentence = message.content.split(" ");
        sentence.shift();
        sentence = sentence.join(" ").slice(4)

        if (!sentence) sentence = 'AFK'

        message.channel.send(`Aight, I have set your AFK: ${sentence}`);
        db.set(message.author.id + 'yuki afk','true')
        db.set(message.author.id + 'messageafk', sentence)
    };
    if (message.content.toLocaleLowerCase().startsWith('yuki afk off')) {;
        db.delete(message.author.id + 'yuki afk');
    };

    message.mentions.users.forEach(user => {
        if (message.author.bot) return false;

        if (message.content.includes("@here") || message.content.includes("@everyone")) return false;

        if (db.has(user.id + 'yuki afk'))
            // db.get(user.id + 'messageafk')

        message.channel.send(`${message.author}, user is AFK: "trying a fix"`)
        // On "trying a fix" should come the author's status.
    })
});

client.login(process.env.DISCORD_TOKEN);


Solution 1:[1]

quick.db requires you to provide a key and a value when using the set() method. I also don't recommend adding spaces (" ") to your database, but that's up to you.

An example for your script would be:

db.set(`${message.author.id}.afk`, sentence);

then you could get it with:

db.get(`${message.mentions.members.first().id}.afk`);

An entire example for a bot like this (using discord.js v13) would be:

const { Client } = require('discord.js');
const db = require('quick.db');
const client = new Client({ intents: 32767, partials: ["MESSAGE"] });
// Note: I'm using every intent in this example, but that is bad practice.
// Only use the intents that you're certain you will be using.

const prefix = "your prefix";
// This will just be an easier way to do things rather than having to re-write the whole string whenever you need it.

client.on("ready", async() => {
  console.log(`${client.user.username} is online!`);
});

client.on("messageCreate", async(message) => {
  if(message.author.bot) return;
  if(message.content.toLowerCase().startsWith(prefix)) {
    const args = message.content.toLowerCase().split(' ');
    let command = args[0].split('').splice(prefix.length).join('');
    // This is here to allow you to add more commands if you want.

    if(command === "afk") {
      const currentMessage = db.get(`${message.author.id}.afk`);
      if(currentMessage) {
        db.set(`${message.author.id}.afk`);
        message.reply({ content: "You are no longer AFK." });
      } else {
        const afkMessage = args.splice(1).join(' ');
        db.set(`${message.author.id}.afk`, afkMessage ?? "This user is AFK.");
        message.reply({ content: "You are now AFK." });
      }
    }
  }

  if(message.mentions.users.size > 0) {
    let memberStatuses = [];
    message.mentions.users.forEach(user => {
      if(user === message.author) return;
      const afkMessage = db.get(`${user.id}`);
      if(afkMessage) memberStatues.push(user.username + `: ${afkMessage}`);
    });

    if(message.mentions.users.size === 1) {
      message.reply({ content: `This user is AFK!\n${memberStatues.join('')}` });
    } else if(message.mentions.users.size > 1) {
      message.reply({ content: `These users are AFK!\n${memberStatuses.join('\n')}` });
    }
  }
});

client.login(process.env.DISCORD_TOKEN);

What I recommend doing is using what I've provided as a reference, rather than just blatantly copying and pasting it into your code. Of course, mine will work, however, it's always better to learn new things rather than rely on others to do them for you.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 itsatelo