Discord.js limit awaitMessage to 1 channel - javascript

I got a issue with a ticket system I made. Currently if the bot has 2 tickets open with questions and is awaiting for a response it takes the response from other channels. Is there a way to only accept the message in the destination channel? The bot has 3 reactions on a message, if you react it makes a new channel and asks you a few questions. These questions interfere with each other
bot.on('messageReactionAdd', (reaction, user) => {
if (user.bot) {
return;
}
reaction.users.remove(user.id)
bot.users.fetch(user.id).then((user) => {
let channelId = "";
let query = `SELECT * FROM ticketMessage`;
db.get(query, (err, row) => {
let ticketMessage = row.MessageId;
if (reaction.emoji.name == "🦖" && reaction.message.id == ticketMessage) {
const startTicket = async (name) => {
reaction.message.channel.guild.channels.create(user.username + name, {
type: 'text'
})
.then(channel => {
let category = "";
category = reaction.message.channel.guild.channels.cache.find(c => c.name.toLowerCase() == "tickets" && c.type == "category");
if (!category) {
user.send("Couldn't Process your ticket since there is no category setup for it!")
} else {
channel.setParent(category.id)
channelId = channel.id
sendQuestions()
}
}).catch(console.error);
}
startTicket(" Dino Lines");
const sendQuestions = async () => {
ticketChannel = bot.channels.cache.get(channelId)
const filter = m => m.author.id === user.id && m.channel === ticketChannel;
ticketChannel.send("What are the names of the dino's you would like to buy, Example => (T-rex, Bronto)?")
ticketChannel.awaitMessages(filter, {
max: 1,
time: 120000
})
.then(collected => {
let dinoName = collected.first().content
ticketChannel.send("How many dino's would you like to buy, Example => (Dino1: 5, Dino2: 3)?");
ticketChannel.awaitMessages(filter, {
max: 1,
time: 120000
})
.then(collected => {
let amount = collected.first().content
ticketChannel.send("What kind of currency would you like to use, Example => (Money, Items, Recources)?");
ticketChannel.awaitMessages(filter, {
max: 1,
time: 120000
})
.then(collected => {
let currencyKind = collected.first().content
ticketChannel.send("Do you want to submit this ticket? => (Yes, No)");
ticketChannel.awaitMessages(filter, {
max: 1,
time: 120000
})
.then(collected => {
let yn = collected.first().content
if (yn.toLowerCase() == "yes") {
embed1 = new Discord.MessageEmbed
ticketChannel.bulkDelete(8, true)
embed1
.setTitle(user.username + "'s Ticket")
.addFields({
name: "Buying Dino Lines",
value: "\u200b \n **Dino Names:** " + dinoName + "\n **Dino Count:** " + amount + "\n **Payment Methode:** " + currencyKind,
inline: true
}, )
.setFooter(settings["Embeds"].displayname, settings["Embeds"].image)
.setColor(settings["Embeds"].color)
.setTimestamp()
ticketChannel.send(embed1)
} else {
ticketChannel.send("Okay ticket canceled!");
}
})
})
})
})
}
} else if (reaction.emoji.name == "⚔️" && reaction.message.id == ticketMessage) {
const startTicket = async (name) => {
reaction.message.channel.guild.channels.create(user.username + name, {
type: 'text'
})
.then(channel => {
let category = "";
category = reaction.message.channel.guild.channels.cache.find(c => c.name.toLowerCase() == "tickets" && c.type == "category");
if (!category) {
user.send("Couldn't Process your ticket since there is no category setup for it!")
} else {
channel.setParent(category.id)
channelId = channel.id
sendQuestions()
}
}).catch(console.error);
}
startTicket(" Boss Fights");
const sendQuestions = async () => {
ticketChannel = bot.channels.cache.get(channelId)
const filter = m => m.author.id === user.id && m.channel === ticketChannel;
ticketChannel.send("What are the names of the bossfights you would like to buy, Example => (Dragon, Broodmother)?")
ticketChannel.awaitMessages(filter, {
max: 1,
time: 120000
})
.then(collected => {
let bossName = collected.first().content
ticketChannel.send("How many people are joining?, Example => (Fight1: 5, Fight2: 3)?");
ticketChannel.awaitMessages(filter, {
max: 1,
time: 120000
})
.then(collected => {
let amount = collected.first().content
ticketChannel.send("What kind of currency would you like to use, Example => (Money, Items, Recources)?");
ticketChannel.awaitMessages(filter, {
max: 1,
time: 120000
})
.then(collected => {
let currencyKind = collected.first().content
ticketChannel.send("Do you want to submit this ticket? => (Yes, No)");
ticketChannel.awaitMessages(filter, {
max: 1,
time: 120000
})
.then(collected => {
let yn = collected.first().content
if (yn.toLowerCase() == "yes") {
embed1 = new Discord.MessageEmbed
ticketChannel.bulkDelete(8, true)
embed1
.setTitle(user.username + "'s Ticket")
.addFields({
name: "Buying Dino Lines",
value: "\u200b \n **Dino Names:** " + bossName + "\n **Dino Count:** " + amount + "\n **Payment Methode:** " + currencyKind,
inline: true
}, )
.setFooter(settings["Embeds"].displayname, settings["Embeds"].image)
.setColor(settings["Embeds"].color)
.setTimestamp()
ticketChannel.send(embed1)
} else {
ticketChannel.send("Okay ticket canceled!");
}
})
})
})
})
}
} else if (reaction.emoji.name == "🖨️" && reaction.message.id == ticketMessage) {
const startTicket = async (name) => {
reaction.message.channel.guild.channels.create(user.username + name, {
type: 'text'
})
.then(channel => {
let category = "";
category = reaction.message.channel.guild.channels.cache.find(c => c.name.toLowerCase() == "tickets" && c.type == "category");
if (!category) {
user.send("Couldn't Process your ticket since there is no category setup for it!")
} else {
channel.setParent(category.id)
channelId = channel.id
sendQuestions()
}
}).catch(console.error);
}
startTicket(" Blue Prints");
const sendQuestions = async () => {
ticketChannel = bot.channels.cache.get(channelId)
const filter = m => m.author.id === user.id && m.channel === ticketChannel;
ticketChannel.send("Which blueprint would you like to buy?, Example => (Bow, Sadle)?")
ticketChannel.awaitMessages(filter, {
max: 1,
time: 120000
})
.then(collected => {
let bluePrint = collected.first().content
ticketChannel.send("What kind of currency would you like to use, Example => (Money, Items, Recources)?");
ticketChannel.awaitMessages(filter, {
max: 1,
time: 120000
})
.then(collected => {
let currencyKind = collected.first().content
ticketChannel.send("Do you want to submit this ticket? => (Yes, No)");
ticketChannel.awaitMessages(filter, {
max: 1,
time: 120000
})
.then(collected => {
let yn = collected.first().content
if (yn.toLowerCase() == "yes") {
embed1 = new Discord.MessageEmbed
ticketChannel.bulkDelete(8, true)
embed1
.setTitle(user.username + "'s Ticket")
.addFields({
name: "Buying Dino Lines",
value: "\u200b \n **Dino Names:** " + bluePrint + "\n **Payment Methode:** " + currencyKind,
inline: true
}, )
.setFooter(settings["Embeds"].displayname, settings["Embeds"].image)
.setColor(settings["Embeds"].color)
.setTimestamp()
ticketChannel.send(embed1)
} else {
ticketChannel.send("Okay ticket canceled!");
}
})
})
})
}
} else if (reaction.emoji.name == "❓" && reaction.message.id == ticketMessage) {
const startTicket = async (name) => {
reaction.message.channel.guild.channels.create(user.username + name, {
type: 'text'
})
.then(channel => {
let category = "";
category = reaction.message.channel.guild.channels.cache.find(c => c.name.toLowerCase() == "tickets" && c.type == "category");
if (!category) {
user.send("Couldn't Process your ticket since there is no category setup for it!")
} else {
channel.setParent(category.id)
channelId = channel.id
sendQuestions()
}
}).catch(console.error);
}
startTicket(" Random Things");
const sendQuestions = async () => {
ticketChannel = bot.channels.cache.get(channelId)
ticketChannel.send("How can we help you?")
}
} else if (reaction.emoji.name == "✉️" && reaction.message.id == ticketMessage) {
const startTicket = async (name) => {
reaction.message.channel.guild.channels.create(user.username + name, {
type: 'text'
})
.then(channel => {
let category = "";
category = reaction.message.channel.guild.channels.cache.find(c => c.name.toLowerCase() == "tickets" && c.type == "category");
if (!category) {
user.send("Couldn't Process your ticket since there is no category setup for it!")
} else {
channel.setParent(category.id)
channelId = channel.id
sendQuestions()
}
}).catch(console.error);
}
startTicket(" Support");
const sendQuestions = async () => {
ticketChannel = bot.channels.cache.get(channelId)
ticketChannel.send("How can we help you?")
}
}
})
})
})

Related

discord.js trying to collect number after a button click

I want my bot to recognize that if my collection equals to 1 then do something but the bot is not getting if it was 1 here is my code
if(button.id === 'policeticket') {
let member = button.clicker.user.id;
button.channel.send("type a number")
const filter = m => m.author.id == button.clicker.user.id;
button.channel.awaitMessages(filter, { max: 1, time: 60000, errors: ['time'] })
.then(async collection => {
let number = collection.first();
if(number === "1") {
button.channel.send("test")
}
})
}
})
I don't no if is correct, but i think do you are comparate a number with a string, and that's comparate always return false if value was number.
in your case i would try this code.
if (button.id === 'policeticket') {
let member = button.clicker.user.id;
button.channel.send("type a number")
const filter = m => m.author.id == button.clicker.user.id;
button.channel.awaitMessages(filter, { max: 1, time: 60000, errors: ['time'] })
.then(async collection => {
let number = collection.first();
if (number === 1) {
button.channel.send("test")
}
})
}
if (button.id === 'policeticket') {
let member = button.clicker.user.id;
button.channel.send("type a number")
const filter = m => m.author.id == button.clicker.user.id;
button.channel.awaitMessages(filter, { max: 1, time: 60000, errors: ['time'] })
.then(async collection => {
let number = parseInt(collection.first());
if (number === 1) {
button.channel.send("test")
}
})
}

My discord bot doesn't always trigger on reaction event

I got a problem with my ticket bot. I have a ticket bot that gives you 5 options, but the bot doesn't really work. Sometimes it does make a ticket on reaction sometimes it just adds the reaction but doesn't make a ticket. I have 2 bots with this issue now so I'm guess I'm just doing it wrong.
Also I'm getting no errors in my console, and I never have the issue my self but other people sometimes do!
bot.on('messageReactionAdd', (reaction, user) => {
if (user.bot) {
return;
}
reaction.users.remove(user.id)
bot.users.fetch(user.id).then((user) => {
let channelId = "";
let query = `SELECT * FROM ticketMessage`;
db.get(query, (err, row) => {
let ticketMessage = row.MessageId;
if (reaction.emoji.name == "🦖") {
const startTicket = async (name) => {
reaction.message.channel.guild.channels.create(user.username + name, {
type: 'text'
})
.then(channel => {
let category = "";
category = reaction.message.channel.guild.channels.cache.find(c => c.id == settings["Categories"].ticketcategory && c.type == "category");
if (!category) {
user.send("Couldn't Process your ticket since there is no category setup for it!")
} else {
channel.setParent(category.id).then(() => {
channelId = channel.id
sendQuestions()
})
}
}).catch(console.error);
}
startTicket(" Dino Lines");
const sendQuestions = async () => {
ticketChannel = bot.channels.cache.get(channelId)
await ticketChannel.updateOverwrite(user, {
VIEW_CHANNEL: true
});
const filter = m => m.author.id === user.id && m.channel === ticketChannel;
embed2 = new Discord.MessageEmbed();
embed2
.setDescription("What are the names of the dino's you would like to buy, Example => (T-rex, Bronto)?")
.setFooter(settings["Embeds"].displayname, settings["Embeds"].image)
.setColor(settings["Embeds"].color)
.setTimestamp()
ticketChannel.send(embed2);
ticketChannel.awaitMessages(filter, {
max: 1,
time: 120000
})
.then(collected => {
let dinoName = collected.first().content
embed3 = new Discord.MessageEmbed();
embed3
.setDescription("How many dino's would you like to buy, Example => (Dino1: 5, Dino2: 3)?")
.setFooter(settings["Embeds"].displayname, settings["Embeds"].image)
.setColor(settings["Embeds"].color)
.setTimestamp()
ticketChannel.send(embed3);
ticketChannel.awaitMessages(filter, {
max: 1,
time: 120000
})
.then(collected => {
let amount = collected.first().content
embed4 = new Discord.MessageEmbed();
embed4
.setDescription("What kind of currency would you like to use, Example => (Money, Items)?")
.setFooter(settings["Embeds"].displayname, settings["Embeds"].image)
.setColor(settings["Embeds"].color)
.setTimestamp()
ticketChannel.send(embed4);
ticketChannel.awaitMessages(filter, {
max: 1,
time: 120000
})
.then(collected => {
let currencyKind = collected.first().content
embed5 = new Discord.MessageEmbed();
embed5
.setDescription("Do you want to submit this ticket? => (Yes, No)")
.setFooter(settings["Embeds"].displayname, settings["Embeds"].image)
.setColor(settings["Embeds"].color)
.setTimestamp()
ticketChannel.send(embed5);
ticketChannel.awaitMessages(filter, {
max: 1,
time: 120000
})
.then(collected => {
let yn = collected.first().content
if (yn.toLowerCase() == "yes") {
embed1 = new Discord.MessageEmbed
ticketChannel.bulkDelete(8, true)
embed1
.setTitle(user.username + "'s Ticket")
.addFields({
name: "Buying Dino Lines",
value: "\u200b \n **Dino Names:** " + dinoName + "\n **Dino Count:** " + amount + "\n **Payment Methode:** " + currencyKind,
inline: true
}, )
.setFooter(settings["Embeds"].displayname, settings["Embeds"].image)
.setColor(settings["Embeds"].color)
.setTimestamp()
ticketChannel.send(embed1)
} else {
ticketChannel.send("Okay ticket canceled!");
}
})
})
})
})
}
Discord only fires events to cached messages. You will either have to fetch the message on startup, or use the raw event instead, for example:
bot.on('raw', (packet) => {
if (packet.t == "MESSAGE_REACTION_ADD") {
// Your code here
}
});
Keep in mind that you would have to re-work it if you used packets, as the data structure is different from what the messageReactionAdd event returns.

discord.js awaitReactions filter to check if the user is in the same channel like bot

my filter doesn't work. I want, filter OUT if the bot reacts and if the channel isn't same as bot's, but it filters out all other reactions. If other member (without bot) is in the channel and probably isn't bot, it will filter out the reaction.
I hope you find what you need in my code, and help me :)
pauseFalse(True)Reactions is array, serverQueue is JSON object, song is string
var filter = (reaction, user) => {
return pauseFalseReactions.includes(reaction.emoji.name) && message.guild.member(user.id).voice.channel == message.guild.member(botID).voice.channel && user.id != botID && user != bot.user;
};
awaitReact(message, filter, 1, Number(song.secs), pauseFalseReactions, serverQueue);
function awaitReact(message, filter, max, time, reactions, serverQueue) {
message.awaitReactions(filter, { max: max, time: time, errors: ['time'] })
.then(collected => {
const reaction = collected.first();
if (reaction.emoji.name == '⏸' && reactions.includes('⏸')) {
Guild.findOneAndUpdate({
guildID: message.guild.id
}, {
musicBotPaused: true,
musicBotPlaying: false
}, function (err, result) {
if (err) {
return console.error(err);
}
});
serverQueue.connection.dispatcher.pause();
var newFilter = (reaction, user) => {
return pauseTrueReactions.includes(reaction.emoji.name) && message.guild.member(user.id).voice.channel == message.guild.member(botID).voice.channel && user.id != botID && user != bot.user;
};
message.reactions.removeAll().catch(error => console.error(error));
addReactions(message, pauseTrueReactions);
awaitReact(message, newFilter, max, (time + 250), pauseTrueReactions, serverQueue);
} else if (reaction.emoji.name == '▶️' && reactions.includes('▶️')) {
Guild.findOneAndUpdate({
guildID: message.guild.id
}, {
musicBotPaused: false,
musicBotPlaying: true
}, function (err, result) {
if (err) {
return console.error(err);
}
});
serverQueue.connection.dispatcher.pause();
var newFilter = (reaction, user) => {
return pauseFalseReactions.includes(reaction.emoji.name) && message.guild.member(user.id).voice.channel == message.guild.member(botID).voice.channel && user.id != botID && user != bot.user;
};
message.reactions.removeAll().catch(error => console.error(error));
addReactions(message, pauseFalseReactions);
awaitReact(message, newFilter, max, (time + 250), pauseFalseReactions, serverQueue);
} else if (reaction.emoji.name == '⏭') {
if (!serverQueue) {
const Embed = new Discord.MessageEmbed()
.setColor('#ff9745')
.setURL('https://discord.js.org/')
.setAuthor('Zigger', 'https://cdn.discordapp.com/app-icons/763859712340262972/010d590a764af0c59b999ad024cf89cb.png?size=128')
.setDescription('Ve frontě nic není.');
return message.channel.send(Embed);
}
message.channel.send(':fast_forward: **Přeskočeno** :thumbsup:');
serverQueue.connection.dispatcher.end();
} else if (reaction.emoji.name == '⏹') {
if (serverQueue) {
serverQueue.connection.dispatcher.end();
queue.delete(message.guild.id);
}
message.channel.send(':stop_button: **Zastaveno!** :thumbsup:');
}
})
.catch(user => {
if (user.id != botID || user != bot.user) {
return;
} else {
message.reactions.removeAll().catch(error => console.error(error));
}
});
}
This will always be false:
message.guild.member(user.id).voice.channel == message.guild.member(botID).voice.channel
Use
message.guild.member(user.id).voice.channelID == message.guild.member(botID).voice.channelID

Bot responds to its own reactions

someone can help?
5️⃣ = 5️⃣
var embed1 = new Discord.RichEmbed()
.setTitle("hjgsadgv")
message.channel.send(embed9)
.then(function (message) {
message.react("5️⃣")
.then(() => message.react("4️⃣"))
.then(() => message.react("3️⃣"))
.then(() => message.react("2️⃣"))
.then(() => message.react("1️⃣"))
const filter = (reaction, user) => {
return ['5️⃣', '4️⃣', '3️⃣', '2️⃣', '1️⃣'].includes(reaction.emoji.name) && user.id === message.author.id;
}
message.awaitReactions(filter, { max: 1, time: 60000, errors: ['time'] })
.then(collected => {
const reaction = collected.first();
if (reaction.emoji.name === '5️⃣') {
message.reply('123');
}
else {
message.reply('321');
}
var embed2 = new Discord.RichEmbed()
.setTitle("uysygadk")
message.channel.send(embed10)
})
})
Bot responds to its own reactions
You can ignore bots by changing your filter to this:
const filter = (reaction, user) => {
return ['5️⃣', '4️⃣', '3️⃣', '2️⃣', '1️⃣'].includes(reaction.emoji.name) && user.id === message.author.id && !user.bot;
}
It basically checks the bot property of user and if it's true then the filter blocks it.

How do I declare a variable in the global scope for one command, and use in another?

here is my code first of all:
if (command === "detain") {
let member = message.mentions.members.first();
let det = new Discord.RichEmbed();
detainer = message.member;
let role = message.guild.roles.find(r => r.name === "Detained");
var a = function none() {
if (!member.roles.find(r => r.name === "Court")) {
message.members.removeRole(message.guild.roles.find(r => r.name === "Officer")).catch(console.error);
member.removeRole(role).catch(console.error);
det.setDescription("${message.author}, you have been impeached because no judges approved this detainment in the 5 minutes.");
}
};
if (args.length < 2) {
det.setDescription("Please add the reason this user has been detained.");
} else {
if (!message.member.roles.find(r => r.name === "Officer") && !message.member.roles.find(r => r.name === "Chief of Police")) {
det.setDescription("You must be an officer to use this command.");
} else {
det.setDescription(message.member + ", I have detained " + member + ". A judge must **!!approve** this detain within **5 minutes** or you will be IMPEACHED!");
member.addRole(role).catch(console.error);
setTimeout(a, 300000);
}
}
message.channel.send(det);
}
if (command === "approve") {
let member = message.mentions.members.first();
let detained = message.guild.roles.find(r => r.name === "Detained");
const approve = new Discord.RichEmbed();
if (message.member.roles.find(r => r.name === "Judge") || message.member.roles.find(r => r.name === "Chief Justice")) {
if (member.roles.find(r => r.name === "Detained")) {
member.removeRole(detained).catch(console.error);
member.addRole(message.guild.roles.find(r => r.name === "Court"));
approve.setDescription("${message.author}, " + member + " has been PUT IN COURT.");
let roleName = message.guild.roles.find(r => r.name === "Judge");
let membersWithJudge = message.guild.members.filter(member => {
return member.roles.find("name", roleName);
}).map(member => {
return member.user.username;
});
var membersWithActualJudge = membersWithJudge.filter(e => e !== message.member);
var judgeNum = (Math.random() * ((membersWithActualJudge.length) - 1) + 1)-1;
var judgeToUse = membersWithActualJudge[judgeNum];
var cj = message.guild.roles.find(r => r.name === "Chief Justice");
var cp = message.guild.roles.find(r => r.name === "Chief of Police");
message.guild.createChannel(detainer + "-vs-" + member, {
type: 'text',
permissionOverwrites: [{
id: 644313191962247178,
deny: ['SEND_MESSAGES'],
},
{
id: detainer.id,
allow: ['SEND_MESSAGES'],
},
{
id: cj.id,
allow: ['SEND_MESSAGES'],
},
{
id: cp.id,
allow: ['SEND_MESSAGES'],
},
],
});
let category = message.guild.channels.find(c => c.name == "Court" && c.type == "category"),
channel = message.guild.channels.find(c => c.name == (detainer + "-vs-" + member));
if (category && channel) {
channel.setParent(category.id);
const sendThing = new Discord.RichEmbed().setTitle("Court case: " + detainer + " vs. " + member + ". \n" + judgeToUse + " will be looking over this case.");
sendThing.setDescription(judgeToUse + ", remember to read the laws, rights, and interpretations before delivering your verdict.");
sendThing.addField("And always remember, feel free to ping [CENSORED], the President, or VP to get any help needed.");
channel.send(sendThing);
approve.setDescriiption("Approved successfully.");
}
} else {
approve.setDescription("This user is not detained.");
}
} else {
approve.setDescription("You must be a Judge to use this command.");
}
message.channel.send(approve);
}
So basically, I want a detain command and an approve command. Someone detains a user where they are muted, and someone else approves it. It then should create a brand new channel and sned something there. The problem here is 1. I can't get the #everyone role ID to work, 2. I can't store the detainer in global scope, and 3. ${message.author} wont work.
I don't really have much else, but thanks in advance

Categories

Resources