Discord.js sending message with #person and #channel - javascript

With my discord bot, i am trying to create a welcome message.
I got the following working:
Sending a message when someone joins
Getting the name of the person who joins
Now the problem I have is with doing a # or # in that message. I use the following code for the message:
const welcomeMessage = `Welcome #${member.user.username} to the server! Please look through the #:page_with_curl:rules and assign yourself a role at #:mortar_board:role-assignment`;
But I have also tried:
Welcome #${member.user.username} to the server! Please look through the #📃rules and assign yourself a role at #🎓role-assignment
Both of them do not what i want them do to. I am getting the following result from this code:
What you can see there is that the #MEE6 and the 2 channel # is not blue, and not clickable. But when I copy the full text, past it and send it in the chat it does show blue.
The result I would like to have is the result of when I copy, paste and send it. That is the following:
Here is a little more of the code:
const welcomeChannel = client.channels.cache.get('738678076174630922');
const welcomeMessage = `Welcome #${member.user.username} to the server! Please look through the #📃rules and assign yourself a role at #🎓role-assignment`;
welcomeChannel.send(welcomeMessage)
What should I do to fix this?

const welcomeMessage = `Welcome ${member}! Please look through the <#id-of-rules-channel-here> and assign yourself a role at <#id-of-role-assignment-channel-here>`
You can auto-embed Discord classes like channels and members if you already have them stored in a variable (like you do for member), or you can use their IDs in text directly in the form <#id> for channels or <#id> for users.

Related

Discord js message sending to specific channel

first off all, i know there are similar threads, but i can't find a single one that uses the same method as i would like. My code:
ticketChannel.send({
embeds: supportEmbed,
components: new MessageActionRow().addComponent(supportButton).addComponent(claimButton),
message.ticketChannel.send("<#&874175844422135808> Prosím o vyřešení ticketu");
})
The last line should send a message that pings a role with that id. Well, it doesn't. The message must be sent to the ticketChannel channel.
Thanks for helping me
if you want to always send it to a specific channel, you'll need to use the id of the channel to define it like so: (assuming you have guild defined)
let channelid = "your channel id"
let ticketchannel = guild.channels.fetch(channelid)
ticketchannel.send("<#&874175844422135808> Prosím o vyřešení ticketu")

How to make a bot ping a user using discord.js

I'm kinda new to bot developing, but I've made a command called &pat and I want it to work like this:
You do &pat #user and I want it to respond like this: #user-executing-the-command, you have successfully patted #user-mentioned-in-the-command. I've come up with this code so far and it works all well apart from pinging the patted person. I does ping them, but it shows #invalid-user, and idk how to fix that. My code:
var pattedone = message.mentions.members.first();
if(!pattedone) return message.reply('please mention the one who should be patted');
message.reply(`you have successfully patted ${pattedone}`);
Do you know how to fix that #invalid-user thing? Thanks. Here is screenshot of the bot's response
According to the Discord.js Documentation
Discord uses a special syntax to embed mentions in a message. For user mentions it is the user's ID with <# at the start and > at the end, like this: <#86890631690977280>
With this in mind. Simply change
message.reply(`you have successfully patted ${pattedone}`);
to this
message.reply(`you have successfully patted <#${pattedone.id}>`);
EDIT
You'll also need to update
var pattedone = message.mentions.members.first();
to
var pattedone = message.mentions.users.first();
you can mention a user with the following "method":
let mention = `<#${userId}>`;
Discord mentions work like this: <#user-id>. Simply replace user-id with the ID of the "victim". You can also mention roles using <&channel-id> or text channels using <#channel-id>.

Issue with Discord.js avatarURL

.avatarURL for discord.js is not displaying any image.
I'm trying to make a command for my discord bot that lets someone ping another and have the bot display their profile picture. I quickly homed in on .avatarURL being the solution for this, but it doesn't seem to be displaying any image at all. Any help would be appreciated.
if (message.content.startsWith(`${prefix}showpfp`)) {
let embed = new Discord.RichEmbed().setTitle(tMember.displayName + "'s Profile Picture").setImage(tMember.avatarURL).setColor('#add8e6')
message.channel.send(embed);
}
after typing in command _showpfp #user the bot will respond with
user's Profile Picture
and thats it...
The issue is that tMember doesn't have a property avatarURL. In your comment you say that you get the value of tMember by doing message.mentions.members.first(), however message.mentions.members is a collection of GuildMembers. A GuildMember doesn't have a property avatarURL.
However, the class User does have the property avatarURL. Therefor you need to fetch the member as an instance of User first. Luckily for you, the class GuildMember has a property .user which does just that.
So the fix for your problem is to change the parameter of the setImage to tMember.user.avatarURL, as can be seen below.
if (message.content.startsWith(`${prefix}showpfp`)) {
let embed = new Discord.RichEmbed().setTitle(tMember.displayName + "'s Profile Picture").setImage(tMember.user.avatarURL).setColor('#add8e6')
message.channel.send(embed);
}

Assigned Roles by Reactions

I have recently been trying to make a Reaction-to-Role bot and have been struggling with some criteria for making it.
I have researched and looked at other peoples' versions of a similar bot on it, yet I have not been able to use it purposefully in my own code.
The goal of the bot is to have multiple options on a message. Here's an example of what it would look like as a message.
**What do you drive?**
🚙 - A car
🚲 - A bicycle
❌ - Nothing
There would be reactions under the message of each emoji that was specified. Once a user clicked on one of these emojis, it would add a role to them such as "Drives a Bike" or "Rides a Bicycle."
I have managed to go as far as making an addrole command (which receives the message the bot will react to, the emoji, and the role it assigns) and stores some info to a JSON file.
The area in which I have been having issues is actually capturing each user that reacts to the specific message, finding out which reaction they sent, and assigning the role assigned to that emoji.
Here is my current code, I'm thank you for any help you may provide :)
addrole.js
if(!args[0] || !args[1] || !args[2] || !args[3]) return message.reply("please use the following format: `autorole #channel messageID icon #role`.");
let channel = message.mentions.channels.first();
let messageID = args[1];
let icon = args[2];
let role = message.mentions.roles.first();
if(!channel.fetchMessage(messageID)) return message.reply("could not find the specified message. Please check the channel and message ID again.");
channel.fetchMessage(messageID).then(msg => {
msg.react(icon);
info[role.name] = {
roleID: role.id,
channelID: channel.id,
icon: icon,
};
fs.writeFile("./configs/info.json", JSON.stringify(info), (err)=>{
if(err) console.log(err);
});
});
I know that creating an event named messageReactionAdd and messageReactionRemove are needed, but I'm not sure how to find their variables and match them accordingly.
Additionally, I am not sure how to make it constantly watch one message even after a restart. From some testing, I noticed that messageReactionAdd would not continue watching the specified messages reactions after a restart.
I am looking for any guidance/help that anyone may provide, thank you!
I've seen on: https://github.com/Sam-DevZ/Discord-RoleReact/blob/master/roleReact.js that there is an event. I've tried this out and it's working.

How do I get a bot to mention a channel?

I am making a welcome message but I can’t seem to make it say the rule channel. I want the bot to say #rules and make it so you can click it to go to the rules channel. I know you can do this with a normal user, but I want to do it with my bot. Every time I try, it can’t be clicked like a normal player. I’ve tried doing #rules, <#channelID>, and other stuff. None of them are clickable.
You need to send a GuildChannel for the channel name to be clickable.
You can achieve this by finding the channel in guild.channels.cache
This returns a Collection, which you can filter.
If you have an ID (easier):
var message = "Make sure to check the rules at " +
message.guild.channels.cache.get('channelID').toString();
If you want find the channel by ID (might break if you have multiple channels with the same name):
var message = "Make sure to check the rules at " +
message.guild.channels.find(channel => channel.name === "rules").toString();
EDIT:
Much easier way: in Discord mention the channel and put a \ (backslash) before the channel name \#rules. You'll get something like <#channelID>.
Use that like this: var message = "Make sure to check the rules at <#channelID>";

Categories