Discord.js | guild.owner.send() not working - discord.js

I searched something for a code that will make the bot message on the owner after it got invited and it's like this:
client.on('guildCreate', guild => {
guild.owner.send('Thanks for invting me to the server!');
}):
But suddenly, it's giving me an error that says, cannot read propert send() of null, am I using old way to do this? if so, how do I make it work?
Does this means it can't find the owner of the server?

client.on('guildCreate', guild => {
guild.owner.user.send('Thanks for invting me to the server!');
}):
If you were to console log guild.owner it would give you a json of the guild owner's id, username etc all within the user object, and to access this we need to do guild.owner.user

Related

How to get bot to reply to user with a (#)ping

I am trying to get a bot to respond to people with a ping in the message, for example: "#user", but everything I've tried have given me not a function error or undefined error. All the stuff I can find about it is either outdated for discord.js v14 or it is for discord.py
Here is my code:
client.on("messageCreate", (message) => {
if (message.content.startsWith("test")) {
const user = message.author.userId();
message.channel.reply(`Hello <#${user}>`)
}
});
I've also attempted variations of the .userId() part - such as .tag, .user.id, and .username but all of them have come back with some sort of undefined error. I know it says userId is a snowflake on discord.js but I am unsure on how to use that for I am fairly new to javascript and discord.js. Also, please know that I am using Replit to host the bot and have discord.js#14.5.0 installed.
So, there are a couple issues here.
Issue 1
message.author.userId() is not a function. When trying to get a user's ID, you want to know what each property is returning.
message -> returns the Message object, which contains the data for the message.
message.author -> returns the User object, which contains the data for the user profile.
message.member -> returns the Member object, which contains the data for the guild member profile.
And so on.
In this case, you're going to want to get the User object: being message.author. You've already figured this out.
Now, the User Object has it's own set of properties (which you can find in the documentation at https://discord.js.org/).
The property that you are looking for is: .id, or in your use-case, message.author.id.
Your other attempts, message.author.tag returns the tag, message.author.user.id will throw an error, and message.author.username returns the user's username.
Issue 2
Your second issue is the .reply() method that you're using. The Channel Object doesn't have a .reply() method, but the message does.
So, how you would write this code:
client.on("messageCreate", async(message) => {
if(message.content.toLowerCase().startsWith("test")) {
// Note that I added the .toLowerCase() method to ensure you can do tEST and it still works!
message.reply({ content: `Hello, ${message.author}!` });
}
});
Also, a cool feature that discord.js has is that you can supply the User or Member Object to the content of a message, and it will automatically mention the desired person.
Hope this helps!

Set Role Color for Discord.js bot

I am trying to make my bot's role a pink color for the current server using the Client.on("ready") However whenever I run the bot with what I currently have the console returns:
(node:6504) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'cache' of undefined
Here is my code I am currently using. I know I need to for loop on every guild but I'm not sure how I would do that, I could use a for or just a .forEach however I can't find the correct way to do such.
const role = client.guild.roles.cache.get('Aiz Basic+');
role.edit({ name: 'Aiz Basic+', color: '#FFC0CB' });
In advance thank you to anybody who replies and helps with my message.
Client#Guild Isn‘t existing. Only Client#Guilds. You can fix this problem by looping through the cache of all your guilds with:
client.guilds.cache.forEach((g) => { })
And inside the loop you can use your code after fixing the .get function, because the get function needs a role id not a role name. You could find the role via the name by writing:
const role = <Guild>.roles.cache.find((r) => r.name === /* The role name */);
Make sure you replaced <Guild> with your guild variable.
Try this code if client.guild is not defined, it could also be because you are using bot instead of client
let role = message.guild.roles.cache.find(role => role.name === "Aiz Basic+");

Is there a way to add a role to someone without mentioning/pinging them? [discord.js]

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

How to get #everyone role ID?

I need to get #everyone role ID to create a private chat for to server members (I'll restrict others except for those 2 users from reading and sending messages). How?
I tried typing
\#Admin
to get Admin's role ID as an example.
But then I typed
\#everyone
and it creates a regular message mentioning everyone on the server (like typing #everyone in chat). No ID.
How do I get #everyone role ID?
The ID for the everyone role is the Guild ID.
If you try to do <#&123123> replacing 123123 with your guild id, it will almost tag everyone
Update: 10th April 2020
Now to mention everyone, all you need is to send the string #everyone: message.channel.send("#everyone");
To obtain the ID for the everyone role, you need to have the guild, which you get on some events like: message, guildCreate, and others.
From there: <something>.guild.roles.everyone, and with that you should be able to get the ID with <something>.guild.roles.everyone.id.
If you don't have access to the guild on a event, you can get the guild by it's ID with something like:
client.guilds.cache.get('guildID').roles.everyone.id, if you know that it will be in cache. If you want to be safe, you can do it like this:
client.guilds.fetch('guildID').then(guild => {
let id = guild.roles.everyone.id;
// do something with id
}
or with await:
let guild = await client.guilds.fetch('guildID');
let id = guild.roles.everyone.id;
Need to get #everyone Role?
<guild>.roles.everyone
It will get a role object. You can check that in Role Class
Discord.js
V12
The guild class has a roles property, which is a RoleManager.
This manager has the property everyone:
The #everyone role of the guild
You can get its id with the id property of the role class:
client.on('message', (msg) => {
console.log(msg.guild.roles.everyone.id);
});
V11
In V11 the guild class has a defaultRole property.
So the code is slightly different:
client.on('message', (msg) => {
console.log(msg.guild.defaultRole.id);
});
Discord
On Discord side, with dev option activated, you can right click in almost everything to grab it ID. For example go in the server settings, in role and right click on a role, you should have a copy ID option.

Using emotes to give roles Discord.js

In my discord server, as a verification method, I want my bot to have all users react to the message and then get given the verified role, and remove the old role. The current code I have doesn't grant or remove roles, but doesn't error.
client.on("messageReactionAdd", function(users) {
users.addRole(users.guild.roles.find("name", setup.verify));
users.removeRole(users.guild.roles.find("name", setup.default));
});
There is a problem in the current code. The messageReactionAdd event returns a User object, which does not contain the property .guilds which you attempt to access: users.guilds.r- .
Instead of this perhaps you should the id of the User object and plug that into message.guild.members.get("id here"). Then use the returned Member object with your current code. That should work!
References:
Guild Member
, Message Reaction Event, User Object
Would something like this work?
I'm not sure it will I haven't tested it. This should add the role if the message they reacted to consists of the word "role1" and if they react to a different message it will remove the role. I don't know if this is what you're going for but in theory this should work.
client.on("MessageReactionAdd", function(users) {
if (message.content === "role1") {
users.addRole(users.guild.roles.find("name", setup.verify))
} else if (!message.content === "role1") {
user.removeRole(users.guild.role.find("name", setup.default))
}
});
Are the names of the roles; setup.verify & setup.default?
If not, that's why it is not working.
Try:
client.on("messageReactionAdd", function(users) {
users.addRole(users.guild.roles.find("id", ROLEIDHERE));
users.removeRole(users.guild.roles.find("id", ROLEIDHERE));
});
where ROLEIDHERE put the role id to find the role id just tag the role and before you hit send put \ before the tag and it will supply the id

Resources