Why is my guildCreate not firing when my guildDelete is? - javascript

Joining the server doesn't work, leaving it does. This is driving me nuts. Any thoughts?
//Join server message **console logging nothing**
client.on('guildCreate', (guild) => {
console.log(`Server: Joined \"${guild.name}\"!`);
guild.author.send(`Yo, use ${prefix}help to get started.`);
});
//Leave server message **console working**
client.on('guildDelete', (guild) => {
console.log(`Server: Left \"${guild.name}\" :(`);
});

This could be happening due to you not giving bot enough intents. If that's the case, try to give bot intent named "GUILDS".

OK I found my own answer, because of how I laid out my code, guildCreate was not running until the first person sent a command to the bot, a simple movement of lines fixed it. God I hate programming

Related

How do I create a discord.js command in index.js [The bot's startup file] that deletes the message when a message that contains "hi" was sent

I've tried to create a command where the bot waits for a message that was sent until a message contained "thebotdeletesthismessage", the bot doesn't seem to do anything, the language I've used is node.js.
The bot doesn't really responds neither delete the message, I've tried to change the code a little bit, Still nothing, I need to know what I've done wrong here.
If you didn't understand something, Please let me know in the comments, And please don't close this topic, I really need help.
bot.on('message', message => {
if (message.content.starts("thebotdeletesthismessage")) {
message.delete
message.channel.send('No, dont send that.');
}
});
Change message.delete to message.delete().
First, there is no .starts() method, you probably want to use .startsWith(). Also, message.delete is not a property but a method, so you'll need to use parentheses after that.
The following code will delete every message that starts with "thebotdeletesthismessage" and sends a message instead:
bot.on('message', (message) => {
if (message.content.startsWith('thebotdeletesthismessage')) {
message.delete();
message.channel.send("No, don't send that.");
}
});

I made a bot blacklist but the bot gets stuck in a loop when anyone uses anything

I made a blacklist for the bot, it "works", it blocks the blacklisted user from using the bot but ended up causing a loop and the blacklisted message to get spammed, both when a blacklisted user uses it and when someone that isnt on the blacklist uses it
heres the code
client.on('message', async message => {
let blacklist = new Discord.MessageEmbed()
.setColor("#e31212")
.setDescription(
"ERROR: You are not allowed to use this bot | Reason: BLACKLISTED"
);
var blacklistids = ["402639792552017920", "711957885722296360"];
if (blacklistids.includes(message.author.id)) {
message.channel.send(blacklist).then(msg => {
msg.delete({ timeout: 3000 })
})
}
// Rest of the code
})
Your code seems correct - but by how you've described it, it would seem to me your code is wrapped in a loop somewhere else. Have you tried creating another client.on() with different conditions to see if those messages also get looped/spammed?.
This is something that can easily occur, especially if you are a beginner.
EDIT: you are however missing }); at the end of your client.on() declaration.
It looks like one of your IDs in the array might be the bot's ID... if this is the case, you might also want to have a if(message.author.bot) return after the client.on(...

discord.js sending message to specific channel

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

Discord Bot Development: How do I stop this infinite loop?

This calls and retrieves a dog url from random.dog, when posting the link to log it stops at one, however when using message.channel.send below it runs an infinite loop of the call, what would be the best way to prevent this and to only send one link then stop till it is called again?
const animals = require('relevant-animals')
client.on("message", (message) => {
if(message.content.includes("dog")){
animals.dog().then(s => message.channel.send(s)) ;
animals.dog().then(s => console.log(s)) ;
};
Below is console log after one request it sends one link
Below is after it is sent to the channel, it just posts links non stop rather than just the one as shown in the console
Your bot is responding to itself. You can exclude it for replying to itself by using message.author.bot.
if(!message.author.bot) {
// Do something if message doesn't come from a bot.
}
I hope this code will help you getting on the right path, good luck!
You could just do this:
if(message.author.bot) return;
This wouldn't only stop bot from executing commands etc. on itself, it would prevent any bot from using your bot.
Since it checks for author of message if bot property returns true it would return;.
And if bot property returns false it would work as normally - only for users like you and me, and many others!
You can try this by doing the following code:
console.log(message.author.bot);
It will log the boolean value of bot property of the messages author.
You can read more about booleans (true/false) here.

discord.js bot replies to itself

I am currently coding my first discord bot, it can already play YouTube music.
if (message.content.includes("Good Job") ||
message.content.includes("good job")) {
message.channel.sendMessage("Good Job everyone :smirk:");
}
As you see, if someone types "good job" (this is just an example) then the bot will reply with "good job everyone :smirk:), but then the spam will begin: the bot reads his own message and replies to it.
How can I prevent the bot from answering itself?
Use this in the on message event:
if (message.author.bot) return;
for more info:
https://anidiotsguide.gitbooks.io/discord-js-bot-guide/coding-guides/a-basic-command-handler.html
The reason why your bot replies to yourself is because where you put:
if (message.content.includes("Good Job") ||
message.content.includes("good job"))
It is basically checking the chat if a piece of text includes the words "Good Job" or "good job". As your bot sends:
message.channel.sendMessage("Good Job everyone :smirk:");
as an answer, it creates a loop because that message includes the words "Good Job", basically running the code again and again and again.
Of course, the easiest way of fixing this is for you to change the answer it gives to not include the words Good Job, but there is better solution to make sure it doesn't happen for all of the commands you might make.
As #Jörmungandr said, under the message event include:
if (message.author.bot) return;
to make sure it doesn't happen. It essentially checks if the author of the message is a bot and if it is, it ignores it.
discord.js
// In message event
if(message.author.id === client.user.id) return;
// I would recommend a variable like this for splits on words
// const args = message.content.trim().split(/\s+/g);
// You could also .slice() off a prefix if you have one
if(/good job/i.test(message.content)) {
message.channel.send('Good job everyone :smirk:'); // sendMessage is deprecated, use send instead
}
You can simply just check if the user sending the message is a bot. For example:
if (!msg.author.bot) {
<Your code to execute if the user is not a bot.>
}
Hope this was helpful, thank you!
You may use this code which avoids doing anything if the author is a bot:
if(message.author.bot) return;
There are quite a lot of ways to solve this.
1: Stopping the Bot from Responding to ANY bot.
Right under your message callback (the client.once('message')), you can just add:
if (message.author.bot) return;
2: Stopping the Bot from Responding to itself
Once again, right under your message callback, you just add:
if (message.author.id == client.user.id) return;
Please note the client that I am referring to is your bot's client, created when you type
const client = new Discord.Client()
...or whatever your client is. It's basically the thing that you login to (ex: client.login(yourtokenhere))
Most tutorials will call this Client, client, or maybe even bot. The discord.js guide will call it a Client.

Categories

Resources