具有工作集状态 quick.db 的 AFK 命令

AFK command with a working set status quick.db

很长一段时间以来,我一直在努力使这个命令起作用。我一直在努力让它发送作者关于“尝试修复”的状态。

我不知道我是否可以为此使用 quick.db,但我一直在尝试使用 db.set(message.author.id + statusmessage) 来保存作者的状态,但我没有不知道怎么把它插入到代码中。

“尝试修复”应该是作者的状态,所以当人们 ping 他们时,它会显示他们的设置状态。

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

quick.db 要求您在使用 set() 方法时提供一个 key 和一个 value。我也不建议向您的数据库添加空格 (" "),但这取决于您。

您的脚本示例如下:

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

那么你可以通过以下方式获得它:

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

像这样的机器人的完整示例(使用 discord.js v13)将是:

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

推荐做的是使用我提供的作为参考,而不是直接将其复制并粘贴到您的代码中。当然,我的也行,但是,学习新事物总是比依赖别人为您做的更好。