How to trigger a block of code when a user joins a guild - discord.js

I've made a bot with discord.js and I want to run some code when a user joins my guild. The obvious answer here is the client event guildMemberAdd, which worked until about a month ago. For some odd reason it stopped working and I've been trying to fix it, but I have no idea what the problem is.
Here is a super simplified version of my script since it's over 3000 lines long:
client.on('guildMemberAdd', member => {
console.log(`${member.user.tag} joined`);
});
I've even tried a try and catch block but everything seems fine. What am I doing wrong here?

They changed it so you have to go on the developer portal, click on your bot en turn Privileged Gateway Intents on then it should work again (it is in the bot tab)

Related

UptimeRobot working but replit dies in short period of time

After like 10 minutes or so, bot goes offline but the UptimeRobot is always running, i think replit is shutting down behind the scene. although, i have tried to implement this piece of code
client.on('disconnect', function(erMsg, code) {
console.log('----- Bot disconnected from Discord with code', code, 'for reason:', erMsg, '-----');
client.connect();
});
but even then it shut down!!
I am trying to have my bot up and running all the time, i need some information or some code to fix have replit restart again automatically. Or maybe, i can use some other free service to get my bot running.
This is because they are blocking requests coming from UptimeRobot. Which means they don't want you to uptime your repl. So you shouldn't be using these.

Not managing to transfer my code onto a different bot

So I have my bot which I now transferred onto a Heroku server, with this I made a new bot which I named ex Test Bot, but for some reason when I went into my config.json with the code
{
"token": "DISCORD BOT ID THING"
}
Now when I change the following to my test bots ID which I will run from my PC, it gives me this wierd error
I really have no clue searched online couldnt fined anthing, I hope one of you guys could help. Thanks!
You're probably using an intent that you don't have permission to use or isn't enabled.
You'll have to go to Discord Developer Portal, choose your application, go to the Bot section, and enable all the intents. (Or the ones you are using.)
Note that once your bot reaches 100 or more servers, this will require verification and whitelisting.
Bot Verification and Data Whitelisting

Enable intents for discord.py bot

Im making a discord bot, and every time it joins a new server, the bot sends me the server info and an invite. However, when I've tested this, it always shows one member which is itself. I have both presence and members intents enabled in the discord developer portal, but it still isn't working.
I then changed some code to Guild.fetch_members() to which I received the error:
discord.ext.commands.errors.CommandInvokeError:
Command raised an exception:
ClientException:
Intents.members must be enabled to use this.
As I said before, on the discord developers website member intents are enabled and I have had this code before without this issue.
When I had this code working before, it was using 1.5.x (I cant remember exactly) and now its using 1.6.0, so something in the new update may prevent this from working, but this is unusual to me, so how can I resolve this issue?
intents were introduced in 1.5.0 so I don't think they were working before, but answering your question
intents = discord.Intents.default()
intents.members = True
bot = commands.Bot(..., intents=intents)
Also remember to enable privileged member intents in the developer portal
How to enable privigeled intents

Does discord limit what information can you get using a bot?

I wrote a quick discord bot as a joke, that just sends a gag message to my friend every 3 or 4 days. However, I could not find my friend's account using client.users.cache.get() or client.users.fetch(). I made sure the ID I put in was correct, to no avail. I then tested it with my own account, which worked perfectly fine. The only difference was that I was in a server with this tester bot, and everyone else in that bot-dev server could receive messages from this bot. I also realized it is impossible to get other information, (friends, etc.). Is this intentional, or is this simply a bug?

How to delete all channels In a discord server(js)

I am attempting to delete channels on my discord server using my bot and this is the code:
if (message.content === 'tanbu')
message.guild.channels.forEach(channel => channel.delete())
.then(console.log)
.catch(console.error);
the error is :
TypeError: message.guild.channels.forEach is not a function
What do I do?
This would've worked in Discord JS V11, but not in V12.
This is what you're looking for:
message.guild.channels.cache.forEach(channel => channel.delete());
https://discord.js.org/#/docs/main/stable/class/GuildChannelManager?scrollTo=cache
Maybe you should think twice before doing such actions. Because this leads to violation of the Discord tos. And it even spams the discord api. which could lead you to a api ban to a particular endpoint or globally sometimes. Even if the library has ability to slow down it , you could still easily bypass it. The ideal solution here is using a sleep function , and not using a forEach loop, maybe a for loop would do better.
Still this is not recommended doing , if your intention is to nuke servers.
I believe that that is for raiding and self bots witch is against discord TOS
i defiently would think w

Resources