简体   繁体   English

AFK 命令 discord.js v12

[英]AFK command discord.js v12

I have already made an AFK command that changes the user's nickname.我已经做了一个 AFK 命令来改变用户的昵称。 Here is my code:这是我的代码:

client.on('message', (message) => {
 if (message.content.includes('start-afk')) {
  message.member
   .setNickname(`${message.author.username} [AFK]`)
   .catch((error) => message.channel.send("Couldn't update your nickname."));
 }
 if (message.content.includes('end-afk')) {
  message.member
   .setNickname('')
   .catch((error) => message.channel.send("Couldn't update your nickname."));
 }
});

I want it to add an AFK reason to be shown whenever the AFK User is pinged and if the AFK user talks then his AFK is removed.我希望它添加一个 AFK 原因,以便在 AFK 用户被 ping 时显示,如果 AFK 用户说话,则他的 AFK 将被删除。 I tried however I was unsure how to do it.我试过了,但我不确定如何去做。

If you want the bot to remember who is AFK and maybe the reason for it, you'll have to use a database to store these pieces of information.如果您希望机器人记住谁是 AFK 及其原因,则必须使用数据库来存储这些信息。

You could use npm i quick.db , which is a really simple database to use.您可以使用npm i quick.db ,这是一个非常简单易用的数据库。

Here's an example code:这是一个示例代码:

client.on('message', message => {
    // checks if the message author is afk
    if (db.has(message.author.id + '.afk')) {
        message.reply("Oh you're back ! i removed your afk")
        db.delete(message.author.id + '.afk')
        db.delete(message.author.id + '.messageafk')
    }
    if (message.content.includes('start-afk')) {
        message.member.setNickname(`${message.author.username} [AFK]`).catch(error => message.channel.send("Couldn't update your nickname."));
        // then here you use the database :
        db.set(message.author.id + '.afk', 'true')
        db.set(message.author.id + '.messageafk', message.content.split(' ').slice(2))

        // I made .slice(2) so that in the message array it also delete the command and the "start-afk"
    }
    if (message.content.includes('end-afk')) {
        message.member.setNickname('').catch(error => message.channel.send("Couldn't update your nickname."));
        // Here you delete it
        db.delete(message.author.id + '.afk')
        db.delete(message.author.id + '.messageafk')
    }
});

Then, whenever the user gets pinged:然后,每当用户被 ping 时:

client.on('message', message => {
    // If one of the mentions is the user
    message.mentions.users.forEach(user => {
        if (db.has(user.id + '.afk')) message.reply('the user ' + user.tag + ' is afk !')
    })
})
  1. Well first you would need a place to store the reason.那么首先你需要一个地方来存储原因。
  2. Then you get the reason from the end of the message.然后你从消息的末尾得到原因。
  3. Then store the reason with the user id of the user.然后将原因与用户的用户 ID 一起存储。
  4. Then inside of the message listener, check if the author of the message is inside the array/map or whatever u choose to store the data in.然后在消息侦听器内部,检查消息的作者是否在数组/映射中或您选择将数据存储在的任何内容中。
  5. If the user doesnt exist inside the afk "list" just ignore如果用户不存在于 afk“列表”中,则忽略
  6. If the user exist inside the afk "list" just remove them from the afk "list".如果用户存在于 afk“列表”中,只需将其从 afk“列表”中删除。
  7. Then just check if there is a mention inside the message by doing message.mentions.users.first() if a mentions exists just get the users afk reason from the list and send it to the channel.然后只需通过执行 message.mentions.users.first() 检查消息中是否有提及,如果提及存在,只需从列表中获取用户 afk 原因并将其发送到频道。
  8. Then if the user does end afk just remove them from the "list".然后,如果用户确实结束了 afk,只需将它们从“列表”中删除。

That's the idea anyways反正就是这个想法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM