Javascript Discord.js,检测是否存在';是某个用户发送的消息吗?
因此,我在胡闹着创建一个discord机器人,它会反复ping用户,直到他们在聊天中做出回应/说任何话(讨厌,对吧?)。如有必要,还可以调整用户ping的次数和每次ping之间的时间。然而,我似乎找不到一种方法来检测ping用户是否在聊天中说了什么,也找不到一种方法来停止循环 代码的实际ping部分在此for循环中:Javascript Discord.js,检测是否存在';是某个用户发送的消息吗?,javascript,discord,discord.js,Javascript,Discord,Discord.js,因此,我在胡闹着创建一个discord机器人,它会反复ping用户,直到他们在聊天中做出回应/说任何话(讨厌,对吧?)。如有必要,还可以调整用户ping的次数和每次ping之间的时间。然而,我似乎找不到一种方法来检测ping用户是否在聊天中说了什么,也找不到一种方法来停止循环 代码的实际ping部分在此for循环中: const ping = async () => { for(var i = 1; i <= pingAmount; i++){
const ping = async () => {
for(var i = 1; i <= pingAmount; i++){
//the wait() command
await new Promise(r => setTimeout(r, pingTime * 1000));
//the actual ping
message.channel.send(`hey <@${userID}> let\'s play minecraft`);
}
//sends a message once pinging is finished
message.channel.send("Pinging Complete.");
};
感谢您的帮助
完整代码如下:
module.exports = {
name: 'Ping',
description: "Pings specified user until they appear",
execute(message, args, Discord){
//initialize variables
const client = new Discord.Client();
const taggedUser = message.mentions.users.first();
const userID = message.mentions.users.first().id;
//splits the command
const slicedString = message.content.split(' ');
//grabs specific numbers from command as input
const pingAmount = slicedString.slice(4,5);
const pingTime = slicedString.slice(5);
//display confirmation info in chat
message.channel.send(`So, ${message.author.username}, you want to annoy ${taggedUser.username}? Alright then lol`);
message.channel.send(`btw ${taggedUser.username}\'s user ID is ${userID} lmao`);
message.channel.send(`amount of times to ping: ${pingAmount}`);
message.channel.send(`time between pings: ${pingTime} seconds`);
//checks to make sure pingTime isnt too short
if(pingTime < 5){
if(pingTime == 1){
message.channel.send(`1 second is too short!`);
return;
} else {
message.channel.send(`${pingTime} seconds is too short!`);
return;
}
}
//timer and loop using pingAmount and pingTime as inputs
const ping = async () => {
for(var i = 1; i <= pingAmount; i++){
//the wait() command
await new Promise(r => setTimeout(r, pingTime * 1000));
//the actual ping
message.channel.send(`hey <@${userID}> let\'s play minecraft`);
const pingedUsers = [taggedUser];
// doodle message
const msg = {author: {id:1}};
// message event
const onMessage = (message) => {
if (pingedUsers.indexOf(message.author.id) != -1) {
console.log("user replied");
}
}
onMessage(msg); // nothing
pingedUsers.push(msg.author.id); // push the author id
onMessage(msg); // he replied!
}
//sends a message once pinging is finished
message.channel.send("Pinging Complete.");
};
//runs the ping function
ping();
}
}
module.exports={
名称:'平',
description:“ping指定的用户直到它们出现”,
执行(消息、参数、不一致){
//初始化变量
const client=new Discord.client();
const taggedUser=message.indications.users.first();
const userID=message.indications.users.first().id;
//拆分命令
const slicedString=message.content.split(“”);
//从命令中获取特定的数字作为输入
常量pingAmount=切片字符串切片(4,5);
常量pingTime=slicedString.slice(5);
//在聊天室中显示确认信息
message.channel.send(`So,${message.author.username},你想骚扰${taggedUser.username}吗?好的,然后lol`);
message.channel.send(`btw${taggedUser.username}的用户ID是${userID}lmao`);
message.channel.send(`ping的次数:${pingAmount}`);
message.channel.send(`ping之间的时间:${pingTime}秒`);
//检查以确保ping时间不会太短
如果(pingTime<5){
如果(pingTime==1){
message.channel.send(`1秒太短了!`);
返回;
}否则{
message.channel.send(`pingTime}秒太短!`);
返回;
}
}
//使用pingAmount和pingTime作为输入的计时器和循环
常量ping=async()=>{
对于(var i=1;i设置超时(r,pingTime*1000));
//实际的ping
message.channel.send(`hey let's play minecraft`);
常量pingedUsers=[taggedUser];
//涂鸦信息
const msg={author:{id:1}};
//消息事件
const onMessage=(message)=>{
if(pingedUsers.indexOf(message.author.id)!=-1){
console.log(“用户回复”);
}
}
onMessage(msg);//没有
pingedUsers.push(msg.author.id);//推送作者id
onMessage(msg);//他回答!
}
//ping完成后发送消息
message.channel.send(“ping完成”);
};
//运行ping函数
ping();
}
}
在这种情况下,您应该比较作者的雪花(id)。
您可以将ping用户放在列表中,查看消息作者是否在该列表中
const pingedUsers=[];
//涂鸦信息
const msg={author:{id:1}};
//消息事件
const onMessage=(message)=>{
if(pingedUsers.indexOf(message.author.id)!=-1){
console.log(“用户回复”);
}
}
onMessage(msg);//没有什么
pingedUsers.push(msg.author.id);//推送作者id
onMessage(msg);//他回答说代码>在这种情况下,您应该比较作者的雪花(id)。
您可以将ping用户放在列表中,查看消息作者是否在该列表中
const pingedUsers=[];
//涂鸦信息
const msg={author:{id:1}};
//消息事件
const onMessage=(message)=>{
if(pingedUsers.indexOf(message.author.id)!=-1){
console.log(“用户回复”);
}
}
onMessage(msg);//没有什么
pingedUsers.push(msg.author.id);//推送作者id
onMessage(msg);//他回答说代码>我尝试将该代码添加到循环中,并将pingedUsers设置为TaggedUsers的id,但不是在特定用户说话时打印到控制台,而是在机器人每次标记该用户时打印。我想这里有一些我不明白的地方。@Joodoon是消息事件侦听器aka中的循环。Joodoon刚刚编辑了我的原始帖子以包含我的完整代码,希望这能澄清问题。我认为我没有事件侦听器,所以这可能是问题所在?我将如何在代码中实现这一点?再次感谢您的帮助,我尝试将该代码添加到循环中,并将pingedUsers设置为TaggedUsers的id,但不是在特定用户说话时打印到控制台,而是在机器人每次标记该用户时打印。我想这里有一些我不明白的地方。@Joodoon是消息事件侦听器aka中的循环。Joodoon刚刚编辑了我的原始帖子以包含我的完整代码,希望这能澄清问题。我认为我没有事件侦听器,所以这可能是问题所在?我将如何在代码中实现这一点?再次感谢你的帮助
module.exports = {
name: 'Ping',
description: "Pings specified user until they appear",
execute(message, args, Discord){
//initialize variables
const client = new Discord.Client();
const taggedUser = message.mentions.users.first();
const userID = message.mentions.users.first().id;
//splits the command
const slicedString = message.content.split(' ');
//grabs specific numbers from command as input
const pingAmount = slicedString.slice(4,5);
const pingTime = slicedString.slice(5);
//display confirmation info in chat
message.channel.send(`So, ${message.author.username}, you want to annoy ${taggedUser.username}? Alright then lol`);
message.channel.send(`btw ${taggedUser.username}\'s user ID is ${userID} lmao`);
message.channel.send(`amount of times to ping: ${pingAmount}`);
message.channel.send(`time between pings: ${pingTime} seconds`);
//checks to make sure pingTime isnt too short
if(pingTime < 5){
if(pingTime == 1){
message.channel.send(`1 second is too short!`);
return;
} else {
message.channel.send(`${pingTime} seconds is too short!`);
return;
}
}
//timer and loop using pingAmount and pingTime as inputs
const ping = async () => {
for(var i = 1; i <= pingAmount; i++){
//the wait() command
await new Promise(r => setTimeout(r, pingTime * 1000));
//the actual ping
message.channel.send(`hey <@${userID}> let\'s play minecraft`);
const pingedUsers = [taggedUser];
// doodle message
const msg = {author: {id:1}};
// message event
const onMessage = (message) => {
if (pingedUsers.indexOf(message.author.id) != -1) {
console.log("user replied");
}
}
onMessage(msg); // nothing
pingedUsers.push(msg.author.id); // push the author id
onMessage(msg); // he replied!
}
//sends a message once pinging is finished
message.channel.send("Pinging Complete.");
};
//runs the ping function
ping();
}
}