我已经做出了一个AFK命令来更改用户的昵称。这是我的代码:
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."));
}
});
我希望它添加一个AFK原因,以便在AFK用户被ping通时显示,并且如果AFK用户讲话,则他的AFK将被删除。我尝试过,但是不确定如何做。
答案 0 :(得分:1)
如果您希望机器人记住谁是AFK及其原因,则必须使用数据库来存储这些信息。
您可以使用npm i quick.db
,这是一个非常简单的数据库。
这是示例代码:
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')
}
});
然后,每当用户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 :(得分:-1)
反正就是这个主意