Im trying to dm a user but I only have there id. This is the code im using:
const member = bot.users.cache.find(ID)
member.send('TEXT')
it isn't working. Does anyone know why?
If the user is not cached you can do this.
const member = bot.users.fetch('user-id',false,true)
That helps to fetch member data directly from API. more at here
Like somebody else has stated, the user probably isn't cached. There isn't anything you can do about this as far as I can tell.
If you want to avoid the undefined error your having, and to catch any errors that may occur when sending the message:
const member = bot.users.cache.find(ID);
if (member) member.send('TEXT').catch(console.error);
Related
I'm trying to check if the message author has a specific role or not. The most common way I came across is the message.mentions.members.first();. But this would require the author to be mentioned in the message. So,
Is there a way to populate member collection and find if the message author has the role or not without mentioning him?
My current code looks like this:
//Some auth and import stuff here
let member = message.author;
//Some pre-validation stuff here
if(member.cache.roles.some(m => m.id === '608238654095360010')){
message.channel.send("You are already verified!");
return;
}
Before this I tried member.roles.cache.some but later found that discordJS got an update and became like above.
But I still get undefined error.
Edit : I tried using message.member.roles.some but that returns
TypeError: message.member.roles.some is not a function
So, I was using DiscordJS v12 which has quite a few updates.
Instead of using message.member.roles.some
I had to use , message.member.roles.cache.some(role => role.id === '608238654095360010')
if (message.content === "!test"){
client.users.cache.get('id').send('message');
console.log("message sent")
}
This method doesn't work and I wasn't able to find any other methods that worked
Is there a spelling mistake or this method is outdated?
I've actually encountered this error myself once. There are two problems you might be facing here:
The user isn't cached by discord.js yet
There isn't a DMChannel open for that user yet
To solve the first one, you have to fetch the user before doing anything with it. Remember this is a Promise, so you'll have to either await for it to complete or use .then(...).
const user = await client.users.fetch('id')
Once you've fetched your user by their ID, you can then create a DMChannel which, again, returns a promise.
const dmChannel = await user.createDM()
You can then use your channel like you normally would
dmChannel.send('Hello!')
Try to create a variable like this:
var user = client.users.cache.find(user => user.id === 'USER-ID')
And then do this:
user.send('your message')
I'm coding a server-info command for my discord bot, and I want to get the owner username or tag from the actual guild. I found a way to do it in discord js 11, but it's not working anymore under 12th version :
const guild = client.guilds.get(message.guild.id);
message.channel.send(message.guild.member(guild.owner) ? guild.owner.toString() : guild.owner.user.tag);
// if the user is in that guild it will mention him, otherwise it will use .tag
So in discord js 12, client.guilds.get isn't a function, and guild.owner returns null.
message.guild.owner.user.usernameis also returning Cannot read property 'user' of null.
I took a look at the documentation, and message.guild.owner seems to be a real property (https://discord.js.org/#/docs/main/stable/class/Guild?scrollTo=owner). So I don't know why it's returning null.
I'd recommend that you first get the guild and make sure that it's available before trying to modify or access it. Therefore, your bot won't hit any errors. Additionally, as far as I know, guild.owner returns null but there is a way around this. You can get the guild owner ID through guild.ownerID and fetch the member as a guildMember. Here is the code:
const guild = message.guild; // Gets guild from the Message object
if(!guild.available) return; // Stops if unavailable
await message.guild.members.fetch(message.guild.ownerID) // Fetches owner
.then(guildMember => sOwner = guildMember) // sOwner is the owner
message.channel.send(guild.member(sOwner) ? sOwner.toString() : guild.owner.user.tag);
This is how you would get the owner's tag when you have the guild. This also does not rely on the cache, so if your bot restarts or the cache is cleared, it will work regardless.
let ownerTag = undefined;
<client>.users.fetch(<guild>.ownerID).then(user => ownerTag = user.tag);
.fetch() returns a promise, which is why I declared a variable and then assigned the value to it.
Make sure you enable these intents in your discord developer portal so your bot could access your server
After that, you could access your user owner tag.
Pretty much the title. Every time I try adding a role to someone using this:
const member = message.author;
member.roles.add('732727208774205460');
I end up with this TypeError:
TypeError: Cannot read property 'add' of undefined
However, if I use it like this:
const member = message.mentions.members.first();
member.roles.add('732727208774205460');
It works completely fine. Problem is, that only works if the person I'm adding a role to was mentioned/pinged by the user. I'm trying to add a role to the user himself where the user doesn't ping anyone (for censorship, mainly). Is there a way I could do this without getting the TypeError?
You are receiving an error because message.author.roles is undefined, so trying to use .add() on it causes an error.
message.author is of type User, which represents a discord user, and is not associated with any particular server.
You're looking instead for message.member which has type GuildMember, which represents a particular user's profile inside of a guild (message.member is just message.author but as a GuildMember instead of a User). It is the GuildMember type that has roles.
Solution:
const member = message.member;
member.roles.add('732727208774205460');
Official Documentation:
User,
GuildMember
What I'm trying to do is make a log for my bot, (you know, something to record server events as they happen), and I've been doing alright so far, but I just can't seem to figure out how to get the reason for a ban/kick or whatever else can record reasons. I've checked the documentation, and I just can't really figure out what some of the stuff there means. There isn't really code to show off, because I have no clue where to start here, and it's about time I ask somewhere for help.
Edit: I do know where to start, I can find the audit log entry, but I can't get the reason for the entry
You can use guild.fetchAuditLogs()
const guild = client.guilds.cache.get('Guild_ID')
const fetchedBan = await guild.fetchAuditLogs({ user: 'User_ID), type: 'MEMBER_BAN_ADD' })
You can also use message.guild instead of const guild = client.guilds.cache.get('Guild_ID')
To get the reason for the latest ban of that member
const banReason = fetchedBan.entries.first().reason