[英]Improved AFK command for discord.js v12
I have recently viewed a AFK command from a different thread.我最近查看了来自不同线程的 AFK 命令。 Here is the code:
这是代码:
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');
}
});
When user pinged:当用户 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`
);
});
});
I want to add an AFK reason to be shown instead of that message when the user is mentioned我想添加一个 AFK 原因,而不是在提到用户时显示该消息
Expected output:预期输出:
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>
The first and maybe the easiest way of getting this info: using Array.prototype.splice()
获取此信息的第一种也是最简单的方法:使用
Array.prototype.splice()
const reason = [...message.content].splice(5).join('')
const message = '+afk doing homework'; const reason = [...message].splice(5).join(''); console.log(reason);
Second method: use String.prototype.split()
:第二种方法:使用
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);
Lastly, also using String.prototype.split()
:最后,还使用
String.prototype.split()
:
const reason = message.content.split('+afk ')[1]
const message = '+afk doing homework'; const reason = message.split('+afk ')[1]; console.log(reason);
Once you have the reason, you can add it to the database, and fetch it whenever 👍有了理由就可以把它加到数据库里,随时取用👍
here :这里 :
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')
}
});
there is this line :有这一行:
db.set(message.author.id + '.messageafk', 'THE MESSAGE TO BE SENT')
There you will set the ask reason,and you will display it when the user is mentionned在那里你将设置询问原因,当用户被提及时你将显示它
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`)
}
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.