Discord.js Delete Single Message - javascript

I am currently working on creating a Discord bot using Discord.js, and I want to have a command that you can tell it ||say Hello or something and it will delete your comment, then say what you told it to.
My current code is
client.on('message', message => {
if (message.content.startsWith("||say ")) {
message.delete(1000); //Supposed to delete message
message.channel.send(message.content.slice(5, message.content.length));
}
});
But this is not working.

It turns out that I had the correct code, but my bot had to have moderator permissions.

client.on('message', message => {
if (message.content.startsWith("||say ")) {
let input = message.content.split(" ").slice(1).join(" ") // Removes the prefix
message.delete() // Deletes the message
message.channel.send(input))//.then(msg=>msg.delete({timeout:"5000"}) <- if you want delete it with delay and sends the finished text
}
});

Related

My bot can't take errors when failling to run a "join" voice channel command

I'm currently coding a discord.js bot and I made this join command so the bot can join my voice channel
module.exports.run = async (client, message, args) => {
let membervc = message.member.voice.channel;
let botvc = message.guild.me.voice.channel;
if(!membervc) return message.send('You need to join a voice channel first.');
if(botvc){
if(!(botvc.members.size - 1)) return message.reply(`I am already on ${botvc}`);
if(membervc.id == botvc.id) return message.reply('We are already on the same voice channel.');
};
membervc.join();
}
The problem is that I have no idea how to make it so that if the last function gets an error or doesn't work at all It can send an error message to the user like
#User, I could not join the channel, "THE ERROR" :/ I don't want my bot to crash and having to run it again just because of one small detail. Is there a way to fix it? It would help me a lot! Thanks in advance!
I would not let the user know about the error, because it might just confuse them, but you can do try catch to check if it goes through and if not message is sent.
try {
membervc.join();
} catch(error){
console.log(error);
message.reply(`Something went wrong while joining voice channel`);
}

Command supposed to delete author's message after the command is called but instead spams chat [discord.js]

I'm trying to make a command where it's supposed to say something but then delete the author's message, but it instead spams the chat with the output and does not delete the message.
This is my current code:
client.on('message', message => {
if (message.content === "###square")
message.delete(100);
message.channel.send("Gaming");
});
And this is the output I get: image
You have not checked to see if the user posting the message is the bot so when the bot posts "Gaming", the message event is fired for that message, creating a loop.
This issue can be fixed by ignoring all bots (recommended and best practice:
client.on('message', message => {
if (message.author.bot) return; // If the author is a bot, return.
if (message.content === "###square") message.delete(100);
message.channel.send("Gaming");
});
... or by ignoring just your bot:
client.on('message', message => {
if (message.author.id === client.user.id) return; // If the author's ID is the same as the bot's, return.
if (message.content === "###square") message.delete(100);
message.channel.send("Gaming");
});
Although, it does seem like you've messed up your if statement: your current code will send "Gaming" every time a message is sent, no matter it's content. To fix this, put the code you want to run in curly brackets:
client.on('message', message => {
if (message.author.bot) return; // If the author is a bot, return.
if (message.content === "###square") {
message.delete(100);
message.channel.send("Gaming");
}
});
For more information on JavaScript if statements, see this W3Schools page.

How do I get a Discord Bot to delete a user command and replace it with a reply

Expectation: The bot deletes the user command and replies
Result: The bot replies but does not delete the user command
bot.on('message', msg => {
// Broken
if(msg.content === 'replace me'){
msg.reply('🤖 replaced');
msg.delete(500);
}
})
Thank you in advance,
From what I understand, when you say 'user command' you are referring to the message of the user. In that case the issue is with msg.delete(500).
.delete() takes a timeout object as a parameter.
Change your code to this
msg.reply('🤖 replaced');
msg.delete({timeout: 500});
For an instant delete with no delay use
msg.delete()

How to setup an if statment, so if the user has DMs blocked the bot sends an error

In my discord bot, there is a /help command that DMs the user a list of commands. My problem is, that some users have their DMs locked. I want it so that IF the user has DMs blocked, the bot will just send the messages in the channel. I've tried using a try catch block but nothing happens. Here's the code:
// defined embeds and stuff before this lime I am also using a command handler
/*try {
message.channel.send(gettingcmds);//.then; message.edit(gotcmds);
message.author.send({embed: modAdmin});
message.author.send({embed: botcmds});
message.author.send({embed: extras});
message.author.send({embed: neededperms});
message.channel.send(gotcmds);
}
catch(e) {
message.channel.send(problem);
} */
module.exports.help = {
name: "help"
}
To summarise:
=> A /help command works
=> If the user has blocked their DMs, they will not receive the messages the bot will send.
=> If there is an error, how would I do it so the bot just sends the messages in the channel the command was executed in?
Also, if I spelt anything wrong please tell me.... #PrepearingForGCSEs
This code should work as you described. I also incorporate an array of the embeds and use a loop for efficiency.
let embeds = [modAdmin, botcmds, extras, neededperms];
try {
for (let embed of embeds) message.author.send(embed);
message.channel.send('Check your DMs!');
} catch(e) {
for (let embed of embeds) {
message.channel.send(embed)
.catch(e => {
return console.error();
});
}
}

bot keeps repeating

This discord bot keeps repeating what it just said concatenating two strings! Please help!
const Discord = require('discord.js');
const client = new Discord.Client();
const prefix = "*";
client.on('message', (message) => {
if (message.content == prefix + 'test') {
message.channel.sendMessage("```User Bot is a way to get more users in your discord server. When you first invite the bot, it will create a channel called #invites. You can put your message in #invites by doing the command *message <message>. Doing this command will make the bot broadcast your message and your invite link so all people with the bot in there #invites channel will get the message.```");
} else if (message.content == prefix + 'setup') {
message.channel.sendMessage("```To setup User Bot, you must join the user bot discord listed here. https://discord.gg/amuzrvr, then in #bots type your message!");
} else if (message.channel.guild.name == 'UserBot' && message.channel.name == 'bots') {
channel(message);
}
});
function channel(message) {
message.channel.sendMessage("***A server! " + message.content + "!***")
}
I found an answer, it was triggering every time the bot spoke cause the conditions were and are the same.
As you have found out, the message event checks every single message inputted into the bot's guilds. To avoid it picking up the bot's messages, you can use
if (msg.author.bot) return;
or something along those lines. :)

Categories

Resources