Reputation: 33
I am a beginner at Node.js Coding and recently coded my bot on repl.it
I have specific commands for moderation, basically warn and mute. When I am running the bot, then write something in chat (everything, just '.' for example) it says I have to mention and then warn himself and then warn again for his previous warn msg and etc. I don't know if you did understand but anyway.
Here is my code:
client.on("message", async (message) => {
let victim = message.mentions.users.first()
if(!victim) message.reply("mention someone to warn.")
else {
let embed = new Discord.MessageEmbed()
.setTitle("Warnings")
.setDescription(`${victim} got warned by ${message.author}!`)
.setColor("GREEN")
.setFooter(`Moderator : ${message.author.username}`)
.setTimestamp()
message.channel.send(embed)
}
});
client.on("message", async (message) => {
if(message.content.startsWith("!mute")) {
if(message.member.hasPermission("ADMINISTRATOR")) {
let member = message.mentions.members.first()
if(!member) {
message.channel.send("Mention someone to mute!")
} else {
member.roles.add("813876389475385394")
let embed = new Discord.MessageEmbed()
.setTitle("Mutes")
.setDescription(`${victim} got kicked by ${message.author}!`)
.setColor("GREEN")
.setFooter(`Moderator : ${message.author.username}`)
.setTimestamp()
message.channel.send(embed)
};
};
};
});
If there are some questions ask it I will really appreciate it if you will help! <3
Upvotes: 0
Views: 148
Reputation: 1245
The problems are that you have two onMessage
events in your code (you only need one) and that you never check if the author of a message is a bot it's self. That check is important because the bot responds to any message because of your first onMessage
event.
This one:
client.on("message", async (message) => {
let victim = message.mentions.users.first()
if (!victim) message.reply("mention someone to warn.")
else {
let embed = new Discord.MessageEmbed()
.setTitle("Warnings")
.setDescription(`${victim} got warned by ${message.author}!`)
.setColor("GREEN")
.setFooter(`Moderator : ${message.author.username}`)
.setTimestamp()
message.channel.send(embed)
}
});
In that block you never check for any commands so it always fires.
Now the actual problem and consequent fix. Note: I know it's a long sentence but bear with me 😅
First your bot reacts on any message and checks if someone was mentioned in that message. If thats not the case, it replies which mentions the original author, which in turn triggers the event again. This time someone is mentioned (because of the reply) and the bot sends the embed, to which it reacts again and the cycle restarts.
Now to fix your particular issue, all you need to do is, check if the author of a message is a bot. You do that with if (message.author.bot) return;
.
So it should look like this:
client.on("message", async (message) => {
if (message.author.bot) return;
let victim = message.mentions.users.first()
if (!victim) message.reply("mention someone to warn.")
else {
let embed = new Discord.MessageEmbed()
.setTitle("Warnings")
.setDescription(`${victim} got warned by ${message.author}!`)
.setColor("GREEN")
.setFooter(`Moderator : ${message.author.username}`)
.setTimestamp()
message.channel.send(embed)
}
});
That fixes your original issue however now you have the problem that your bot always reacts with both events.
As you can see, the bot sends two messages. This is why you only need one onMessage
event. All you have to do is put both your commands into one event. It should look a little something like this:
client.on("message", async (message) => {
if (message.author.bot) return;
if (message.content.startsWith("!mute")) {
if (message.member.hasPermission("ADMINISTRATOR")) {
let member = message.mentions.members.first();
if (!member) {
message.channel.send("Mention someone to mute!")
} else {
member.roles.add("813876389475385394");
let embed = new Discord.MessageEmbed()
.setTitle("Mutes")
.setDescription(`${victim} got kicked by ${message.author}!`)
.setColor("GREEN")
.setFooter(`Moderator : ${message.author.username}`)
.setTimestamp();
message.channel.send(embed);
};
};
} else if (message.content.startsWith("!warn")) {
let victim = message.mentions.users.first();
if (!victim) return message.reply("mention someone to warn.");
let embed = new Discord.MessageEmbed()
.setTitle("Warnings")
.setDescription(`${victim} got warned by ${message.author}!`)
.setColor("GREEN")
.setFooter(`Moderator : ${message.author.username}`)
.setTimestamp();
message.channel.send(embed);
}
});
This is a very basic version of a command handler but it works. If you want to learn about a little more advanced version, see here.
Upvotes: 1