bot keeps repeating - javascript

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. :)

Related

Message reaction

I am trying to get my Discord bot to react to people's messages. For example, if somebody said "welcome", the bot will react to their message, because it has the word welcome, which I would set the keyword too. I understand how to get the bot to reply to messages with a response, but not reactions.
In discord.js you can use something like this:
const Discord = require("discord.js");
const client = new Discord.Client();
client.on("message", message => {
if(message.content === "welcome") {
message.react("😄");
}
}
client.login("YOUR_TOKEN");
This in the Discord.js Docs might be helpful as well.

Adding a 'I am already in the channel function' to a bot. [Discord.js]

I'm trying to add a feature where if a bot is already in a voice chat with someone and the person tries to make it join, the bot replies 'I am already in the voice chat'.
Here's the part where it would fit in
client.on('message', async message => {
// Voice only works in guilds, if the message does not come from a guild,
// we ignore it
if (!message.guild) return;
if (message.content === '>join') {
// Only try to join the sender's voice channel if they are in one themselves
if (message.member.voice.channel) {
const connection = await message.member.voice.channel.join();
} else {
message.reply('You need to join a voice channel first!');
}
}
});
A person that I asked, suggested I used I compare the 'voiceState.channelID' of the bot and the user. If its the same, then the bot returns the message 'I am already in the channel!' where and how would I fit this into the code. I am very new to programming and JavaScript.
Extra info: I am using node.js and a library called Discord.js. If you could not tell, this is a Discord bot.
I have only included a segment of the code dedicated to voice chat, please tell me if you need the rest and I will provide!
in Discord.JS you can do member.voice.channelID to get the channel ID. SO, in your code you could add:
const clientMember = message.guild.members.cache.get(client.user.id);
if(!clientMember.voice.channel) {
//The bot is in no voice channel, go ahead and join it.
} else if(clientMember.voice.channelID == message.member.voice.channelID) {
//They're already in the same channel
} else {
//They're both connected to a voice channel, but they are in different channels.
}
Put that inside the if (message.member.voice.channel) { to make sure the person that ran the command is actually in a VC.

How do i get the id of the channel the channel my discord bot is in and the channel id of the message author

Im trying to make a discord bot leave command but i cant find what i have to put to get the channel id of the bot and the user sending the message to compare and make sure they are the same
Crashbot.on('message', async message =>{
//creates an array called args and removes the first amount of charactors equal to the length of the prefix variable
let args = message.content.substring(PREFIX.length).split(" ");
//the switch equals true if the first word after the prefix is "leave"
switch (args[0]) {
case 'leave':
//if the message author is not in the same voice channel as the bot the bot replies and tells them that that have to be in the same channel to use that command
if (message.member.voice.channel.id != Crashbot.voice.channel.id) {
message.reply("You must be in the same channel as me to use this command");
return;
}
//if the message author is in the same voice channel as the bot it leaves the channel it is in
if (message.member.voice.channel.id === Crashbot.voice.channel.id) {
const connection = await message.member.voice.channel.leave()
message.channel.send("Successfully left the voice channel");
}
break;
}
});
Have a look at the Discord.js Docs here.
You can use client.voice.connections to get a collection of voice connections. You can use this to check if the channel id the user is in matches any in the connections.

How to make a discord bot read another bot's embed

so I am trying to make a companion bot for a bot called "EPIC RPG" it is a game bot and there are some events that I would like my bot to ping a role so people notice there is an event going on but I just can't make my bot read the embed, any ideas?
You can check if the message author is that specific bot using it's ID then you can check the embeds of the message sent by that bot, if any, using Message.embeds
client.on((message) => {
if (message.author.id === 'BOT_ID') {
if (message.embeds) {
const embed = message.embeds[0]
if (embed.title === 'EVENT') {
return message.channel.send('EVENT STARTED')
}
}
}
})

Discord.js Delete Single Message

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
}
});

Categories

Resources