针对discord.js v12的改进的AFK命令

时间:2020-09-15 18:53:50

标签: javascript node.js discord discord.js

我最近从另一个线程查看了AFK命令。这是代码:

client.on('message', (message) => {
 // checks if the message author is afk
 if (db.has(message.author.id + '.afk')) {
  message.channel.send(`Welcome back ${message.author} I removed your AFK.`);
  db.delete(message.author.id + '.afk');
  db.delete(message.author.id + '.messageafk');
 }
 if (message.content.startsWith('+afk')) {
  message.channel.send(
   'Aight, I have set your AFK. I will send a message to the users who mention you..'
  );
  // then here you use the database :
  db.set(message.author.id + '.afk', 'true');
  db.set(
   message.author.id + '.messageafk',
   message.content.split(' ').slice(2)
  );
 }
 if (message.content.includes('+afk off')) {
  db.delete(message.author.id + '.afk');
  db.delete(message.author.id + '.messageafk');
 }
});

用户ping时:

client.on('message', (message) => {
 // If one of the mentions is the user
 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 + '.afk'))
   message.channel.send(
    `${message.author}, the user you mentioned is currently AFK.. Leave a message if urgent by DMing him`
   );
 });
});

我想添加一个AFK原因,而不是在提到用户时显示该消息

预期输出:

User: +afk <reason>
BOT: Aight, I have set your AFK. I will send a message to the users who mention you..
User2: @User
BOT: @User2, the user you mentioned is currently AFK: <reason>

2 个答案:

答案 0 :(得分:2)

获取此信息的第一种也是最简单的方法:使用Array.prototype.splice()

const reason = [...message.content].splice(5).join('')

const message = '+afk doing homework';
const reason = [...message].splice(5).join('');
console.log(reason);


第二种方法:使用String.prototype.split()

const reason = message.content.split(/ +/).splice(1).join(' ');

const message = '+afk doing homework';
const reason = message.split(/ +/).splice(1).join(' ');
console.log(reason);


最后,也使用String.prototype.split()

const reason = message.content.split('+afk ')[1]

const message = '+afk doing homework';
const reason = message.split('+afk ')[1];
console.log(reason);


一旦有原因,您可以将其添加到数据库中,并在任何时候获取它

答案 1 :(得分:0)

此处:

    if (message.content.startsWith('+afk')) {
      message.channel.send('Aight, I have set your AFK. I will send a message to the users who mention you..')
// then here you use the database :
db.set(message.author.id + '.afk','true')
db.set(message.author.id + '.messageafk', message.content.split(' ').slice(2))
    }
    if (message.content.includes('+afk off')) {
db.delete(message.author.id + '.afk')
db.delete(message.author.id + '.messageafk')
    }
});

有这行:

db.set(message.author.id + '.messageafk', 'THE MESSAGE TO BE SENT')

您将设置询问原因,并在提到用户时显示它

client.on('message', message =>{
if(message.author.bot) return;
if(message.channel.type === 'dm') return;
// if the message author was afk
if(db.get('afkusers').includes(message.author.id){
message.channel.send('Oh you're back ! Removed your afk')
db.delete('afkusers[afkusers.indexoF(message.author.id)]')
}
// if one of the mentions is a afk user
// Oh and you need to use 'if(user.bot) return;' because a bot can't use your commands ^^'
// if there is afk users (don't delete it's important so we are such that the variable will work and we can use normal js to manipulate the variable like .includes())
if(db.has('afkusers')){
// If one of the mentions is afk
if(message.mentions.users.find(u => db.get('afkusers').includes(u.id)) message.channel.send(`${message.author}, the user you mentioned is currently AFK.. Leave a message if urgent by DMing him`)

}
})