I was making this new bot and used awaitmessage function.When a user use the cmd '!gangsug', the bot is supposed to ask a question like - "Please choose a option - RP or Fight" and when the user uses an option it is supposed to give a reply. (Which is a embed one). I have given two different replies for the 2 answers given by the user.(One for the answer "RP" and the other for the answer "Fight"). In this case, by bot is giving same answer for both answer. If the user types "RP" or "Fight" same reply is being given
by the bot. I hope you understood the problem.The code will be given below (ofc only the part where the problem comes)
if (message.content === `${prefix}gangsug`) {
message.reply('Hey there! Please choose a option.\n'
+ 'Confirm with `RP` or with `Fight`.');
// First argument is a filter function - which is made of conditions
// m is a 'Message' object
message.channel.awaitMessages(m => m.author.id == message.author.id,
{max: 1, time: 30000}).then(collected => {
// only accept messages by the user who sent the command
// accept only 1 message, and return the promise after 30000ms = 30s
// first (and, in this case, only) message of the collection
if (collected.first().content.toLowerCase() == 'RP') {
message.reply(suggestgang1);
}
else
message.reply(suggestgang2);
message.reply(suggestgang3);
}).catch(() => {
message.reply('No answer after 30 seconds, req canceled.');
});
} }
);
**Yea, this is the code..It would be great of you give an answer by stating the problem and a fixed code to replace this one. Thank you :) **
I believe the problem is in this line:
if (collected.first().content.toLowerCase() == 'RP')
Since you used the .toLowerCase() method on the message content, it's physically impossible to match anything uppercase, such as 'RP'.
if (collected.first().content.toLowerCase() == 'rp')
Related
I'm new to programming and i'm trying to make a Discord bot for me and my friends, for now I have set up two commands, the first works fine, but the second one is the problem.
client.on('message', msg => {
if (msg.content === `#usertag#0001`) {
msg.channel.send(`<#${msg.author.id}> <#${msg.author.id}> <#${msg.author.id}> <#${msg.author.id}> don't tag me`);
}
});
So what it should do is when a specific user is tagged it sends a message, the message sent from the bot is working (it tags the user who sent the message 3 times and then it says something else), but the part where the bot recognizes the tag doesn't work.
I tried by putting the Discord id, using the full #, and other stuff, but it doesn't seem to work.
For this you could use the message.mentions method, and here is how you could use it for example:
// For Discord V13 'message' is deprecated and
// will be removed in the future, use 'messageCreate' instead.
client.on("message", msg => {
// Disable messages from bot to avoid endless loops / replies
if(msg.author.bot === true) return;
// Check if anyone is mentioned.
// Skip #everyone mentions to be checked.
if(msg.mentions.users.size > 0 && msg.mentions.everyone === false) {
if(msg.mentions.users.find(user => user.id === "ENTER USER ID HERE")) {
msg.channel.send(`<#${msg.author.id}> <#${msg.author.id}> <#${msg.author.id}> <#${msg.author.id}> don't tag me!`);
}
}
});
Hope this will help you! ! I have tested this with Discord V13, i'm not 100% sure if this will work on Discord V12 as well.
I recently heard of .awaitMessages in Discord.JS and I did search it up and look at some tutorials, but I'm still not really sure of how it exactly works and how to use it properly. I would really appreciate if someone could tell me how it works and how to use it. Thanks a lot!
I've got you covered my man/woman, I wrote some example code and commented on it like crazy explaining everything I put down for you :)
client.on('message', message => {
// This just converts all messages to complete lowercase for the bot to interpret.
const command = message.content.toLowerCase();
// This variable simply stores the User ID of the person who sent the message.
const messageAuthor = message.author.id;
if (command == 'what is your name?') {
// This is the filter we will use for the Message Collector we're going to use A.K.A ".awaitMessages".
const messageFilter = message.author.id == messageAuthor;
/* This is the command you asked about in all its glory. The first parameter you give it is the filter to use (although a filter isn't required if I recall correctly.).
The next parameter is "max", this purely dictates how many messages (applying to the filter if applicable) for the collector to collect before ending (unless of course the timer runs out, which we'll touch on next).
Second to last parameter is "time", this is, like max, pretty straightforward and dictates the amount of time the collector attempts to collect messages before ending (as stated above, if the collector-
reaches the max for collected messages before the timer runs out it will end prematurely, however if for whatever reason the collector doesn't receive the max amount of collected messages, it will continue to attempt collect messages-
until the timer runs out.)
Now onto the last parameter, "errors". This parameter basically tells the collector to treat the timer running out as if it was an error, that way you can then reference said error in the ".catch" method you'll see below and give the bot-
instructions on what to do if the time does end up running out.
*/
message.channel.awaitMessages(messageFilter, { max: 1, time: 10000, errors: [time] })
.then(collected => {
// Checks to see if the message we collected is a number, and if so, responds with the message in the command below.
if (isNaN(collected) == false) {
// The response for if the collected message IS a number.
message.channel.send(`${collected} is definitely not your name!`);
}
// This runs as long as the message we collected is NOT a number.
else {
// The response if the collected message is NOT a number.
message.channel.send(`${collected} is an awesome name!`);
}
})
.catch(collected => message.channel.send('You never gave me your name ):'));
// ^ This is the ".catch" method where you'll give the instructions for what to do if the timer runs out, in this case I have it just reply to the user to tell them that they never gave me their name.
}
});
So I'm making this thing where you can do this command and it checks if you purchased a thing or not (it gets sent to staff). So I have that bit working but I'm stuck on how to do something like where the bot says, "Are you done with this?" and it reacts to that message with the ❎ and ✅. And when you press one of them, it does the code.
Or make it so it only reacts with the tick and detects when someone reacted to it.
Currently I have:
message.channel.send(new Discord.RichEmbed().setTitle("Rank-Up Application:").setDescription(`**If you wish to send an application to get Ranked-Up in the Discord & ROBLOX Group, this is the right place to do it! **`).setFooter("When you have done that, say done.").setColor("#ff4757")).then(() => {
message.channel.awaitMessages(filter, { maxMatches: 1, time: 90000, errors: ['time']})
.then(collected => {
message.channel.send(":grey_exclamation: **Sending...**")
client.channels.get(`622386044914106388`).send(new Discord.RichEmbed().setTitle("New Rank-Up!").addField(`**The user ${username} has sent in an application to get ranked. Please check the following links to see if you should rank him. Remember: True = Owns Class, False = Doesn't own Class.**`).addField(`Plus: ${plus}`).addField(`Advanced: ${advanced}`).setTimestamp().setFooter("When you have done that, say done.").setColor("#ff4757"))
So that last line of code is the bit it should say the message under. I'm quite stuck on this and don't even know what start code I should put.
Maybe something like this is what you're looking for. I use it on tickets for people to close them. Works fine for me.
message.channel.send(`Message that will have reactions on it.`).then(msg => {
msg.react('❌');
// Wait 300ms to make sure the tick emoji is made after the first reaction. Just incase the API is on the slow side.
setTimeout(function(){msg.react('✅');},300);
// Create a filter to collect, making sure it only checks for non bot users.
// If you want to check for the message author use user.id == message.author.id
const filterEmojis = (reaction, user) => {
return ['❌', '✅'].includes(reaction.emoji.name) && user.bot == false;
};
// Await for a user to react as per our filter above.
msg.awaitReactions(filterEmojis, {time: 60000}).then(collected => {
const reaction = collected.first();
// Check if the reaxtion is an X.
if(reaction.emoji.name === '❌') {
// Do something when this is reacted too.
}
// Check if the reaxtion is a tick.
if(reaction.emoji.name === '✅') {
// Do something when this is reacted too.
}
}).catch(() => {
// Catch errors.
});
}).catch(console.error);
I've been looking around, can't quite seem to find the answer to this issue I am having with a discord bot I am making with Typescript. I have all of my commands in their own folder using a separate file for each command. Helps to keep things organised.
I've seen people say client.channels.get(`channelID`).send(`Text`)
but that's giving me
Object is possibly 'undefined'. and Property 'send' does not exist on type 'Channel'.
I'm actually trying to make a bot message every text channel (given from a list) whenever someone runs a reboot command because for whatever reason people keep rebooting the bot. I implemented it as a funny thing to do every now and again as a troll if someone needs to use it. The bot goes offline for 3 minutes but I don't like having people spam it and pretty much have the bot un-usable.
I'm using client.channels.get(channels.channelnames[5]).send("This is a message.")
Solution:
if(msgObject.member.guild.channels.find(channel => channel.name === channels.channelnames[5]) as Discord.TextChannel) {
var txtchannel = msgObject.member.guild.channels.find(channel => channel.name === channels.channelnames[5]) as Discord.TextChannel
txtchannel.send("This is a message in a channel. Don't know why you read this.")
}
so I was on the right track mostly. just had to do as Discord.TextChannel and I think that's why Cynthia was saying about casting the variable as a TextChannel
This code works. thanks for all your help guys!
According to https://discord.js.org/#/docs/main/stable/class/Collection it seems like there is no get method.
Try client.channels[channels.channelnames[5]].send("This is a message.")
In other words try to replace .get with the square brackets.
EDIT: Sorry I was a bit quick, I think the issue is type casting, try casting the Channel to a TextChannel if you know that it is a text channel.
This should work assuming your channels are text ones.
client.on('ready',(e)=>{
let channel_ids = ['123','456','789'];
// loop through the list of channel ids.
for(let i=0, l=channel_ids.length; i<l; i++){
let channel_id = channel_ids[i];
let this_channel = client.channels.get( channel_id );
// if exists, and type in list send message
if(this_channel && ['dm', 'group', 'text'].indexOf( this_channel.type ) != -1){
this_channel.send('a cool message')
.then(message => console.log(`Sent message: ${message.content}`))
.catch(console.error);
}
}
});
I'm creating a discord bot that will run a function based on questions asked, of which multiple are yes/no questions. Answering "yes" will do a certain function while "no" should end the question.
I do not want the bot to run a random function every time it reads "yes".
The only solution I could think of is once the question has been asked (based on a word trigger), the user gets stuck in a loop where the bot will not respond to any trigger other than "yes" / "no".
If yes, the related function will run.
If no, the user gets out of the loop and is able to trigger another question.
Other than yes/no, the bot will say "please reply with yes or no"
How can I code this in node.js/discord.js?
If this is a command that the users must run to initiate this conversation...I recommend referencing this in the discord.js official documentaion that talks about awaitMessages. Basically, you could use this followed by a series of .then() and if statements that will take the user down multiple paths based on their answers that can be filtered out.
If this is a default function of the bot without needing a trigger command, I recommend you tread carefully in this area as something like this could be quite spammy.
An example:
message.channel.send('Is this a great bot?').then(async (start) => {
message.channel.awaitMessages(filter, { maxMatches: 1, time: 60000, errors: ['time']}).then(async (collected) => {
if (collected.first().content === "yes") {} else if (collected.first().content === "no") {}
})
You can filter messages with the filter or another way so that yes/no are the only acceptable answers.