Private chat user on server join on discord - discord.js

how can code a discord bot to private chat a new user that just join my server. this is my code but not working
client.on('guildMemberAdd', member =>{
member.send("welcome to this server");
})

Your code looks fine, but you need to check to see if you have the right guild intents.
// Make sure you have Intents.FLAGS.GUILDS and Intents.FLAGS.GUILD_MEMBERS
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MEMBERS] });
Then you should be able to add the listener.
client.on('guildMemberAdd', member =>{
member.send("Welcome to Sparta!");
})
Privileged Gateway Intents
Also, this is a privileged intent, so you have to make sure you are allowing it in the Discord Developer Portal. To do that, navigate to Developer Portal>Select your bot> Select "Bot" (instead of General Information) > Enable Privileged Gateway Intents: Members.

Related

Im coding a discord bot for the first time and when i type node . my discord bot wont go online i dont know why

this is my code
const Discord = require("discord.js")
const client = new Discord.Client({ intents: 32767 });
client.login("my token")
and when i run it and type node . into my terminal this is what happens
Error [DISALLOWED_INTENTS]: Privileged intent provided is not enabled or whitelisted.
at WebSocketManager.createShards (/Users/sinanfarista/Desktop/discordBot#1/node_modules/discord.js/src/client/websocket/WebSocketManager.js:250:15)
at async Client.login (/Users/sinanfarista/Desktop/discordBot#1/node_modules/discord.js/src/client/Client.js:254:7) {
[Symbol(code)]: 'DISALLOWED_INTENTS'
}
you're probably attempting to use an intent that hasn't been enabled.
You must go to the Discord Developer Portal, select your application, navigate to the Bot section, and enable all of the intents. (Or the ones you're currently using.)

Discord.js login as user after 2auth

How can I receive the user's information from an OAuth2 access token?
I tried doing this but it didn't work.
const client = new Client({ intents: [Intents.FLAGS.GUILDS] });
client.login('access_token');
I got the following error, though the access token was valid.
Error [TOKEN_INVALID]: An invalid token was provided.
On StackOverflow, I found some other solutions. But, it didn't work either.
const client = new Client({ intents: [Intents.FLAGS.GUILDS], _tokenType: '' });
OAuth2 does not work like that. You can't just log in to the user's account to access their information.
What you need to do is fetch the data from the API directly with the right endpoint.
There is a guide about how OAuth2 works right here.

discord.py bot can't dm multiple users with a role

#client.command()
async def eventsoon(ctx):
role = discord.utils.get(ctx.message.guild.roles, name='Golden God')
for member in ctx.message.guild.members:
if role in member.roles:
await member.send("ur mom")
I want the bot to dm users with golden god role, it doesnt work aand gives no errors
I tested your code and it's probably happening because you didn't enable Privileged Gateway intents and your bot can't get all members. Enable Server Members Intent on the Discord Developer site in the Bot section.
Then, add them to your code. Example:
intents = discord.Intents.default()
intents.members = True
client = commands.Bot(command_prefix="!", intents=intents)

I'm wondering how this is possible? Discord bot

I recently receive a 2 message in discord with invite link to server I received it from bot who doesn't share the same server with me or anything
How this is possible, I find some py codes but only work if the user and bot share same server
from discord.ext.commands import Bot, Greedy
from discord import User
bot = Bot(command_prefix='!')
#bot.command()
async def pm(ctx, users: Greedy[User], *, message):
for user in users:
await user.send(message)
bot.run("TOKEN")
How can I make one send messages to list of IDs not in same server?
Maybe the bot left the server after he sent the invite to everyone.
You could check if one of the bots was in the servers in the audit-log.
you could either get the user by id with the guild.get_member(id). That requires the user to be on that specific guild you try to get the user.
Or you can use bot.get_user(id) / await bot.fetch_user(id). There the bot tries to get the user. If found you can do sth with it else it will return None or raises the command Error UserNotFound.
A bot that has no mutal guild with a specific user cannot send a dm to them.
Also what purpuse is it to send dms to multiple users from a list?
Keep in mind that its against the Discord TOS to mass dm users without permissions. If they did not opt in sth to get dms I would not send random dms do multiple users.

How to autoassign roles to new members joining Discord guild?

I am writing a Discord bot that assigns a role to new users when they join my Discord guild. Let's say the role is called "Friend".
The bot is written in Python and uses the Discord.py library.
I am overwriting the on_member_join() function.
#bot.event
async def on_member_join(member):
assign member to Friend role
I see that there is a add_roles() function from the Member class, but it hasn't worked in this case because the new member doesn't have permissions to manage roles. For security concerns, the default #everyone role shouldn't have the ability to manage roles and be able to assign whatever role it wants to be.
The Discord Bot does have permissions to manage roles.
Is there a way to have the bot automatically assign a new member to the Friend role?
Thank you.
Try this :
#bot.event
async def on_member_join(member):
role = discord.utils.get(member.server.roles, id="<role ID>")
await bot.add_roles(member, role)
Solved! Thanks Bagle for helping out.
Here is what worked:
#bot.event
async def on_member_join(member):
default_role = discord.utils.get(member.guild.roles, id=DEFAULT_ROLE_ID)
await member.add_roles(default_role)
DEFAULT_ROLE_ID is an integer of the role id I'm trying to assign.
Make sure that your bot has "Manage Roles" permission set AND is above the role you're trying to set in your Guild's role hierarchy.
For example, if your bot has a role of Bot and the role you're trying to auto assign is Friend, make sure Bot is above Friend in the "Roles" tab. If Bot is not above Friend, you'll hit an error.

Resources