discord.js 用于与其他玩家决斗的机器人
discord.js bot for dueling other players
我正在开发一个允许用户挑战他人进行决斗的机器人。目前我坚持让机器人只显示来自受挑战用户的决斗。照原样,它只允许来自原始邮件发件人的回复。任何帮助将不胜感激。
if(command === `${prefix}fight`) {
//checks if the username to fight is in the message
let author1 = message.author.username;
let user = message.mentions.users.first();
if(!user) return message.reply("you did not specify who you would like to fight!");
//checks if the users is trying to fight themselves
if(user.id == message.author.id) return message.reply('you cannot fight yourself!');
//checks if the user is trying to fight the bot
if(user.bot == true)
return message.reply('you cannot fight a bot!');
//saves the two user ids to variables
var fighter1 = message.author.id;
var fighter2 = user.id;
//announces challenge and awaits response
var challenged = user.toString();
message.channel.send(`${challenged}, ${author1} has challenged you to a duel. Do you accept the challenge, yes or no?`)
.then(() => {
message.channel.awaitMessages(response => response.content == 'yes' && message.author.id == fighter2 || response.content == 'no' && message.author.id == fighter2, {
max: 1,
time: 60000,
errors: ['time'],
})
.then((collected) => {
if (collected.first().content == 'yes') {
message.channel.send(`${challenged} has accepted the challenge!`);
}
else if(collected.first().content == 'no') {
message.channel.send(`nope`);
}
})
.catch(() => {
message.channel.send(`No response. Fight has been cancelled.`);
});
});
}
那是因为您使用的是 message.author
而不是 response.author
message.channel.awaitMessages(response => return (response.content == 'yes' && message.author.id == fighter2)
|| (response.content == 'no' && message.author.id == fighter2),
{
max: 1,
time: 60000,
errors: ['time'],
})
看起来你的过滤器函数在检查 content
时只查看 response
消息,但是当你检查 .author.id
时,你会从原始 message
开始战斗(来自挑战者,而不是被挑战者)。
尝试将您的过滤器函数更改为使用 response.author.id
而不是 message.author.id
,如下所示:
message.channel.awaitMessages(response => response.content == 'yes' && response.author.id == fighter2 || response.content == 'no' && response.author.id == fighter2, {
...
})
我正在开发一个允许用户挑战他人进行决斗的机器人。目前我坚持让机器人只显示来自受挑战用户的决斗。照原样,它只允许来自原始邮件发件人的回复。任何帮助将不胜感激。
if(command === `${prefix}fight`) {
//checks if the username to fight is in the message
let author1 = message.author.username;
let user = message.mentions.users.first();
if(!user) return message.reply("you did not specify who you would like to fight!");
//checks if the users is trying to fight themselves
if(user.id == message.author.id) return message.reply('you cannot fight yourself!');
//checks if the user is trying to fight the bot
if(user.bot == true)
return message.reply('you cannot fight a bot!');
//saves the two user ids to variables
var fighter1 = message.author.id;
var fighter2 = user.id;
//announces challenge and awaits response
var challenged = user.toString();
message.channel.send(`${challenged}, ${author1} has challenged you to a duel. Do you accept the challenge, yes or no?`)
.then(() => {
message.channel.awaitMessages(response => response.content == 'yes' && message.author.id == fighter2 || response.content == 'no' && message.author.id == fighter2, {
max: 1,
time: 60000,
errors: ['time'],
})
.then((collected) => {
if (collected.first().content == 'yes') {
message.channel.send(`${challenged} has accepted the challenge!`);
}
else if(collected.first().content == 'no') {
message.channel.send(`nope`);
}
})
.catch(() => {
message.channel.send(`No response. Fight has been cancelled.`);
});
});
}
那是因为您使用的是 message.author
而不是 response.author
message.channel.awaitMessages(response => return (response.content == 'yes' && message.author.id == fighter2)
|| (response.content == 'no' && message.author.id == fighter2),
{
max: 1,
time: 60000,
errors: ['time'],
})
看起来你的过滤器函数在检查 content
时只查看 response
消息,但是当你检查 .author.id
时,你会从原始 message
开始战斗(来自挑战者,而不是被挑战者)。
尝试将您的过滤器函数更改为使用 response.author.id
而不是 message.author.id
,如下所示:
message.channel.awaitMessages(response => response.content == 'yes' && response.author.id == fighter2 || response.content == 'no' && response.author.id == fighter2, {
...
})