Discord Bot Can't Get Channel by Name - javascript

I have been making a discord bot and wanted to make it send a message to a specific "Welcome" channel. Unfortunately, I have been unable to do so. I tried this.
const welcomeChannel = bot.channels.get("name", "welcome")
welcomeChannel.sendMessage("Welcome\n"+member.user.username);
However in this "welcomeChannel is undefined".
Edit:
I tried using
const welcomeChannel = bot.channels.get("id", "18NUMBERIDHERE")
welcomeChannel.sendMessage("Welcome\n"+member.user.username);
but this is still undefined, strangely

You should use the channnel id instead of it's name.
How to get the channel id of a channel:
Open up your Discord Settings
Go to Advanced
Tick Developer Mode (And close the Discord settings)
Right click on your desired channel
Now there's an option Copy ID to copy the channel id
Also checkout the discord.js documentation for (channel) collections
Furthermore your approach won't work because .get wants a channel id (see the linked documentation above). In case you REALLY want to get an channel by its name, use .find instead for that.
This is however a really bad idea in case your bot runs on more than one server since channel names can now occur multiple times.

You can also use
bot.channels.find("name","welcome").send("Welcome!")

I tried a lot with the same error, and that's how I fixed it. I used client as my Client().
client.channels.cache.get("18NUMBERIDHERE").send("Welcome!");

Your error could come from the fact that you're using bot.channels.get(), which isn't the best idea, because discord.js isn't very friendly when it comes to using .send() on multiple items.
Instead, try to use member.guild.channels.find("name", "channel").send();, if possible. If this is in the client.on("message"), then simply use message.member.channels.find("name", "channel").send();
Sidenote: My memory is mixed up, so if that doesn't work, try .get() instead of find.

Related

How to get a embed from a message in Discord.js v14

Im getting a message that got reacted with a emoji, and want to send it in another channel, but I cant get the embed information from that message reaction.message.embeds is empty.
Do I need to set some intents to be able to read that data? Like GatewayIntentsBits.MessageContent for messages
Want to re-send a message with embed to another channel
[EDIT]
When I do some weird shit: reaction.message.channel.messages.fetch(reaction.message.id) the embeds tag seems to work, is there any way or reason for that? Or should I use this weird way, can someone explain to me why the first example doesnt work?
In regards of the Intents:
This property requires the GatewayIntentBits.MessageContent privileged intent in a guild for messages that do not mention the client. (https://discord.js.org/#/docs/discord.js/main/class/Message?scrollTo=embeds)
The return is an Array of embeds. Therefore you would need to access it with reaction.message.embeds[0] for example. Of course if your reaction.message.embedsis completly empty this would not help.
A good way to check for the information is to console.log(reaction.message)
You can also fetch() the reaction directly to get the full MessageReaction information.
reaction.fetch().then((fetchedReaction) => {
fetchedReaction.message.embeds[0]; //or
fetchedReaction.message.embeds;
});
Your solution
reaction.message.channel.messages.fetch(reaction.message.id)
is indeed the best and correct way if the message may not be in the cache. This way it is still being retrived.

Discord.js using ids instead of mentions?

So, I've recently started developing a bot for a server that I staff in however I can't get the bot to use IDs instead of mentions.
For mentions, I have been using message.mentions.users.map but I don't know how I can make it so it also accepts IDs (Using ||).
If anyone could help it would be really appreciated!
You would need to fetch the user by their id then display their avatar
Replace <Client> with either bot or client
(depending on what you set up your Discord Client as)
const target = <Client>.users.fetch('ID goes here')
message.channel.send(target.displayAvatarURL())

Can Discord Bot send a message on a channel to everyone

I'm coding a discord Bot with the discord.js library on a nodeJs server.
Here is my question: is that possible when an event occure (like someone sending a message) that the bot answers to members of a role, or to everyone. The message.reply("my reply") method only answer to the author of the message as I use it for now...
Your problem is that message.reply() is a very limited method: it always mentions the author of the message, and you can't override that.
You need to use a more generic method, channel.send(), and construct the mention yourself. .reply() is just a shortcut for a frequently used form of it, but you need something custom.
Presumably, you want it to happen in the same channel as the message, so you need message.channel.send("Whatever content you want").
Now, to add a role, you need to decide how to pick one. Is it fixed? Then you could hard-code a role mention by role ID: <#&134362454976102401> (of course, it needs to be the role ID you require).
If you want to find a role, for example by name, you need to do that via searching through the guild in question. You can access it though message.guild, but be warned that it will be undefined for DMs.
Then you can do something like
const role = message.guild.roles.find(role => role.name === "NameYouWant");
message.channel.send(`${role} something something`);
because Role objects become mentions when converted to strings.

How to remove a role from a user in a specified server using discord.js?

I'm trying to implement a command for my bot that gives a role to a user for a determined amount of time, but I don't know how to make the bot remove or add a role to a user.
I need it to involve the server ID cause I plan on using the bot in multiple servers.
This is clearly wrong, but I hope it can help you all understand what I'm trying to do:
client.guilds.get(config.serverID).message.guild.members.get(userID).removeRole(config.donatorRole)
You almost have it! The only thing wrong is that message isn't a property of a Guild. Also, make sure to catch any errors if the Promise returned by GuildMember.removeRole() is rejected.
Here's a cleaned up example:
const guild = client.guilds.get(config.serverID);
const member = guild.members.get(userID);
member.removeRole(config.donatorRole)
.catch(console.error);

Discord Bot announcement channel

I want to make my discord bot make an announcement to a certain channel, based on the Channel ID.
I know how to make the bot respond in whatever channel it's talked to, using message.channel.send(), but I was wondering if there was a way to do message.channelID.send() to have the bot talk in a specific channel to make an announcement.
Thanks!
Nathan
client.channels.find("id", "what ever").send(/*...*/)
All channels are stored in a Collection under the Client.channels prpperty which you can use to get it.
Using client.channels.find("id", "what ever").send(/*...*/); will work to send a message to a certain channel, but in the console, an alert is thrown. Using client.channels.get("what ever").send("/*...*/"); does the same job, but without using a discord.js deprecated function.
Thanks to Jonas Wilms!

Categories