Im trying to delete the .ping after i just used it, while the answer, 'pong' still remains.
The code below is what i can think of, but it doesn't seem to work, can anyone correct my mistake?
client.on('message', msg => {
if (msg.content === '.ping') {
message.delete(1000);
msg.reply('pong!');
}
});
Like what #NullDev mentioned in the comment, you should be using msg rather than message since that's what you declared the variable.
Second, is how you set up the timeout function. In your code, you set it as:
message.delete(1000);
But if you reference the discord.js documentation, it shows you the proper way to delete a message with a timeout is like this:
message.delete({ timeout: 1000 });
This is because the timeout is a property of the options parameter, thus you have to use the curly braces.
But for good measure, I would suggest that you add a catch method in order to avoid your code from breaking down, like so:
message.delete({ timeout: 1000 })
.catch(console.error);
Note:
From discord.js v13 and beyond, the delete method will not accept any options, therefore you have to do this instead:
setTimeout(() => message.delete(), 1000);
well this one code should do the job :
client.on('message', msg => {
if (msg.content === '.ping') {
message.channel.bulkDelete(1)
msg.reply('pong!');
}
});
Related
I have a kick command in my discord.js bot, and I wanted to make the bot send a DM to the person that got kicked. I can not do it like this:
user.send(message)
target.kick(reason).then((m) => {
// do the other stuff here
});
With this code, the DM does not get sent.
This is what I did instead:
user.send(message).then((msg) => {
target.kick(reason).then((m) => {
// do the other stuff here
});
});
Now the issue is that if the target blocks the bot, the bot can not DM them, making the code to kick them not run.
How can I solve this?
You can use .finally, which runs regardless if the promise is fulfilled or rejected.
user.send(message).finally(() => {
target.kick(reason).then((m) => {
// do the other stuff here
});
});
So I was trying to make my bot send me a DM to every server it joins but I keep getting the API error: Unkown Channel
My code:
bot.on("guildCreate", async guild => {
guild.channels.first().createInvite().then(inv =>
bot.users.get(ownerID).send(`I have been added to **${guild.name}** | ${inv.url}`)
)
});
Okay, this is your problem. I have made the same mistake a few months ago, here's how to fix it.
Since you are using discord.js version 11, guild.channels is indeed a Collection, which you can use .first() on. In this case, you can't do that.
Here's my workaround:
bot.on("guildCreate", async guild => {
var channel;
guild.channels.forEach(c => {
if (c.type === "text" && !channel) channel = c;
});
channel.createInvite({ maxAge: 0 }).then(inv => bot.users.get(ownerID).send(`I have been added to **${guild.name}** | https://discord.gg/${inv.code}`));
});
This basically loops through each channel and finds a valid TextChannel to create an invite.
Hope this helps.
const prefixtest = ">>"
msg = message.content.toLowerCase();
if(msg.startsWith(prefixtest + "test")) {
message.delete();
setTimeout(function() {
if{message.channel.author.id == "240254129333731328"}{ //this is the bots id
message.delete();
}, 3000);
}
Sorry if my english is bad. But I can't solve this problem, i'm trying to find it on internet but nothing. How can I delete other bot messages by using their Bot ID?
There is an issue in your code, you are trying to get .author property on a Channel instead of a Message.
So you have to modify your if statement as following :
if (message.author.id === "240254129333731328")
Also, you can delay the deletion of a Message by adding .delete() method first parameter an object with a .timeout property in it that will represent the delay in milliseconds before the message being deleted. (See docs)
message.delete({ timeout: 3000 });
So you can modify your code this way :
if(msg.startsWith(prefixtest + "test")) {
if (message.author.id === "240254129333731328") {
message.delete({ timeout: 3000 });
}
}
msg.guild.channels.find(c => c.id == "693088078168064051").send(tempVars("absence_review"))
.then(function(message) {
const approved = message.react("✅")
.then(() => message.react("❌"))
.catch(error => {});
const filter = (reaction, user) => {
return ['✅', '❌'].includes(reaction.emoji.name) && user.id === message.author.id;
};
message.awaitReactions(filter, {
max: 1
})
.then(collected => {
const reaction = collected.first();
if (reaction.emoji.name === '✅', reaction.user.bot === false) {
message.reply('reacted with yee.');
} else if (reaction.emoji.name === '❌', reaction.user.bot === false) {
message.reply('reacted with nah dude.');
}
})
.catch(collected => {
message.reply('you didnt react idiot');
});
That is the code I am currently using for the command. What I am making this command to do is to record an absence, send it to another channel with reactions, have someone react to it and if they react with the checkmark, it will send it back to the channel where the command was originally sent, but if they react to the cross, the person who originally made the command gets DM'd by the bot. Those last few things are easy, but my main issue is the reactions. I have made the reactions appear, but the last bit of the code seems to break it. Everything after the 'const filter' bit. If I remove all that, then it will just send the embed message with the reactions, but the reactions won't do anything. However, if I keep that in. It won't even send the message, I get an error stating "SyntaxError: Unexpected end of input" Now I have googled for this for around an hour, getting friends to help but unfortunately nothing is helping. If anyone could help it would be greatly appreciated, thanks in advance.
Although I cannot see the whole code, I believe your error is thrown not from the code above. The syntax error 'unexpected end of input' is usually caused by not finishing a bracket or brackets i.e )}; in somewhere in your code - commonly after the 'client on' function in your main.js file. Go through your whole code and make sure every bracket is properly represented. Maybe put your code into code.vb - it detects and points out syntax errors like the one you have above.
Let me know of progress.
I have spent yet another 3 hours in my mini dungeon figuring this out and got something to somewhat work.
msg.guild.channels.find(c => c.id == "693088078168064051")
.send(tempVars("absence_review")).then(function(message){
message.react("✅")
.then(() => message.react("❌"))
.catch(error => {});
});
const filter = (reaction, user) => {
return ['✅', '❌'].includes(reaction.emoji.name) && user.id === message.author.id;
};
message.awaitReactions(filter, {
max: 1
})
.then(collected => {
const reaction = collected.first();
if (reaction.emoji.name === '✅', reaction.user.bot === false) {
message.reply('reacted with yee.');
} else if (reaction.emoji.name === '❌', reaction.user.bot === false) {
message.reply('reacted with nah dude.');
}
})
.catch(collected => {
message.reply('you didnt react idiot');
});
This is a fixed version of it, although the last bit doesn't necessarily work, I will try figure the rest out by myself.
First post on this site so my bad if I don't format something correctly. I've been trying to make a change prefix command and I don't know how to check next message content for a variable to use instead of the old prefix.
msg.reply("Which prefix do you want?");
const collector = new Discord.MessageCollector(msg.channel, m => m.author.id === msg.author.id, { time: 5000 });
console.log(collector)
collector.on('collect', msg => {
newprefix=collector.content();
msg.reply('Prefix succesfully changed!');
});
I strongly advise using the awaitMessages method. First you need to define a filter, a delegate function that returns true if the reaction is one you want. Typically in a command/response setting, you want a filter that indicates the original command invoker replied.
const filter = (reaction, user) => {
return user.id === msg.author.id;
};
You can then define options for your awaitMessages. (It's quite common to do this in-line with the awaitMessages method, that's fine if that is more clear to you)
const options = {
max: 1,
time: 30000,
errors: ['time']
}
The above example accepts 1 message, if given in 30 seconds, otherwise it throws an exception.
Finally, you are ready to begin...
msg.reply("Which prefix do you want?").then(() => {
msg.channel.awaitMessages(filter, options)
.then(collected => {
newprefix = collected.first();
// Record newprefix to a persistent config here
msg.reply('Prefix successfully changed!');
})
.catch(collected => {
msg.reply("You didn't respond fast enough.");
});
}
Keep in mind, this occurs asynchronously, so if you need to write newprefix somewhere, a JSON config file for example, you should do from within the scope of the .then