gpt4 book ai didi

javascript - 您如何检查机器人是否被 ping 通?

转载 作者:行者123 更新时间:2023-12-04 15:06:59 27 4
gpt4 key购买 nike

我正在创建一个“杀死”人的命令。我希望机器人返回消息“哈!你以为!@Author 死了!”如果他们 ping 机器人。 (我如何让机器人查看它是否被 ping 过?)答案已更新并且现在可以正常工作。

const Discord = require('discord.js');
const bot = new Discord.Client();

module.exports = {
name: 'kill',
description: 'kills',
execute(message, args, bot) {
message.delete({ timeout: 30000 });

if (message.content.startsWith('-kill')) {
const target = message.mentions.users.first();
const memberTarget = message.guild.members.cache.get(target.id);

if (message.mentions.has(bot.user)) {
return message.channel.send(`HA! SIKE! <@${message.author.id}> died.`);
}
message.channel.send(`<@${memberTarget.user.id}> has died!`);
console.log(`<@${memberTarget.user.id} died.`);
}
},
};

最佳答案

您可以使用 message.mentions.has(bot.user) 检查是否提到了机器人。此外,请确保将机器人传递给 execute() 方法,而不是实例化一个新方法:

module.exports = {
name: 'kill',
description: 'kills',
execute(message, args, bot) {
message.delete({ timeout: 30000 });

if (message.content.startsWith('!kill')) {
const target = message.mentions.users.first();
const memberTarget = message.guild.members.cache.get(target.id);

if (message.mentions.has(bot.user)) {
return message.channel.send(`HA! SIKE! <@${message.author.id}> died.`);
}
message.channel.send(`<@${memberTarget.user.id}> has died!`);
console.log(`<@${memberTarget.user.id} died.`);
}
},
};

然后在您的主 JS 文件中传递 bot 对象:

// ...
client.commands.get('kill').execute(message, args, bot)
// ...

enter image description here enter image description here

关于javascript - 您如何检查机器人是否被 ping 通?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65942391/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com