Discord.js using ids instead of mentions? - javascript

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())

Related

Discord JS v13 - Get internal user ID from a user tag in discord js v13

There have been similar questions but I havent been able to find an exact match so I apologize in advance.
I have am application form that someone fills out and provides their discord user tag (ex: Bob#1111). This form then gets sent to my discord (which that user has already joined). I then process it with my bot which creates a channel, copies the application to that channel and uses the discord user tag of the applicant to get their discord information and give them permissions on that newly created channel.
This worked for a few years prior to v13 but now its no longer working. Prior code was simply :
await guild.members.fetch()
let userID = client.users.cache.find(user => user.tag === userTag)
Any thoughts would be greatly appreciated.
Since you didn't explain what the actual issue is, I can only assume that the problem is when you try to give the user permissions for the channel.
Make sure to find the member in your server:
const member = await guild.members.cache.get(userID.id)
and then give the permissions for the found member.
For what you're trying to do, I would recommend getting the member by their ID. You can do this by doing:
const member = await guild.members.fetch('user-id')
For your code, userID will in fact give you the entire user class, not just the ID. You can access the user's ID by doing userID.id.
Additionally, you can view an example of how to set channel overwrites in discord.js guide.
To find a user by their tag you can do:
const member = guild.members.cache.find(member => member.user.tag === userTag);

Send a custom emoji to another server with Discord.js

So lets say a Discord bot is a member of server A and server B, and there is a custom emoji being hosted on server A. How do I get the bot to send the emoji on server B? I have seen bots such as MEE6 that can send custom emojis in DMs, so this surely must be possible.
I have tried both this:
message.channel.send('Test <:[emoji_name]:[id]>');
and this:
message.channel.send(`Test ${client.guilds.cache.get('[server_id]').emojis.cache.first()}`);
I used .first() since there is only one emoji in the server.
Both of these do not work. The emoji will appear in the server it is hosted in, but if I try to send it in any other server, it just shows the emoji name and/or ID. I cannot find any articles or questions that provide any other way of doing this. So in short, how do I get the bot to send an emoji to another server that the emoji isn't being hosted on?
in discord.js v12 it is
const someEmoji = client.emojis.cache.get("<Emoji ID>");
For everyone facing the same issue, you can use client.emojis.get("<Emoji ID>").
Here's a quick example:
const someEmoji = client.emojis.get("<Emoji ID>");
message.channel.send(someEmoji);

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!

Discord Bot Can't Get Channel by Name

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.

Categories