मैं एक कमांड बनाने की कोशिश कर रहा हूं जहां एक उपयोगकर्ता दूसरे उपयोगकर्ता को सिक्के कमाने के लिए चुनौती दे सकता है। मैंने इसे इस तरह से किया है कि जो व्यक्ति चुनौती देना चाहता है वह p1 है और वह व्यक्ति जो @s है वह p2 है। मैं उस विशिष्ट (पी 2) से एक संदेश की प्रतीक्षा कैसे कर सकता हूं ताकि यह जांचा जा सके कि वह स्वीकार करता है या इनकार करता है? यहाँ मेरा कोड अब तक है।
name: "duel",
description: "Challenge a player to win extra <:kcoin:810191130385317913>",
type: "Shop",
minArgs: 2,
maxArgs: 2,
argsError: `<@user> <amount>`,
callback: async (message, args) => {
if (args < minArgs || args > maxArgs){
message.channel.send(argsError)
return
}
if (message.mentions.members.first() == undefined){
message.channel.send("**Challenge someone by @ them.**")
return
}
if (isNaN(args[1])){
message.channel.send("**Use a number for the amount.**")
return
}
const p1id = message.author.id
const p2id = message.mentions.members.first().user.id
const amount = args[1]
message.channel.send(`**<@${p1id}> has challenged <@${p2id}> to a duel for ${amount*2}** <:kcoin:810191130385317913>`)
message.channel.awaitMessages({})
}
}
1 उत्तर
अधिक जानकारी के लिए discord.js गाइड देखें, लेकिन अगर मैं ठीक से समझ लें, तो आपको बस एक फ़िल्टर बनाना है।
इस मामले में, आपको जो फ़िल्टर लागू करना चाहिए वह सरल है। यह फ़िल्टर करने के लिए कि संदेश दूसरे व्यक्ति का है या नहीं, आप बस यह कर सकते हैं:
const filter = response => {
//this checks whether the person who wrote the message is the @ person
return response.author.id === p2id;
};
यह केवल एक साधारण फिल्टर है, और भी चीजें हैं जिन्हें आप जांच सकते हैं, जैसे कि लक्ष्य वाले व्यक्ति ने इसे ठीक से प्रारूपित किया है, और आप वापस जवाब दे सकते हैं।
जहां तक awaitMessages
को लागू करने का सवाल है, आप डिसॉर्ड के फ़ॉर्मेटिंग का पालन कर सकते हैं। js गाइड प्रदान किया है।
कोड:
const filter = response => {
//this checks whether the person who wrote the message is the @ person
return response.author.id === p2id;
};
message.channel.send(`**<@${p1id}> has challenged <@${p2id}> to a duel for ${amount*2}** <:kcoin:810191130385317913>`).then(() => {
//this sets a time constraint, you can remove it, it's not required
message.channel.awaitMessages(filter, { time: 30000 /* time is in ms */, errors: ['time'] })
.then(collected => {
//responds back to channel that the user has responded
message.channel.send(`${collected.first().author.username} has responded!`);
})
.catch(collected => {
message.channel.send("Time to respond timed out.")
});
});
message.channel.send(`${collected.author.username} has responded!`);
काम नहीं करता हैcollected
के बजाय, आपcollected.first()
का उपयोग करेंmessage.channel.send(`${collected.first()author.username} has responded!`);
से पहले एक लाइन पर क्या आपconsole.log(collected.content);
जोड़ सकते हैं और देख सकते हैं कि क्या कंसोल में कुछ है जो प्रिंट करता है?