How do I get a bot to mention a channel? - javascript

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>";

Related

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>.

Discord.js sending message with #person and #channel

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.

How to make a bot find a channel owner / compare to the author's ID

I'm making a bot where if you do d!move, the bot will move the channel where the message was sent in under a category via ID. I also want to make it so that whoever does the command has permissions such as MANAGE_CHANNELS, which I've already added. The problem is that when I want to confirm whoever created that channel is the person that activated the command, the bot says yes. I did this on an alt account, where I made the channel and my alt was the one initializing it, and the bot said "success!" I also wanted to make it so if someone else made the channel, and when I did it, it would work because I made the bot know my ID.
I've researched Google and found nothing.
I've tried using a function with fetchAuditlog but get anywhere.
if(!message.channel.client.user.id == message.author || !message.author.id == `329023088517971969`) return message.channel.send("You don't own this channel!")
else message.channel.send("success!");
message.channel.setParent(`576976244575305759`);
I expect the bot to be able to check if the author created the channel, and lead to You don't own this channel if they don't own it. But if they do then the bot moves the channel.
The actual result is the bot moving the channel anyway regardless if they own the channel or not.
As #André has pointed out, channel.client represents the client itself, not the user who created the channel. Also, the last line in your code is not part of the else statement, so that's why it's run regardless of the conditions you defined.
To reach a solution, you can make use of the guild's audit logs. You can search for entries where the user is the message author and a channel was created. Then, all you have left is to check if one of those entries is for the current channel, and run the rest of your code if so.
Sample:
message.guild.fetchAuditLogs({
user: message.author,
type: 'CHANNEL_CREATE'
}).then(logs => {
if (!logs.entries.find(e => e.target && e.target.id === message.channel.id)) return message.channel.send('You don\'t own this channel.');
else {
// rest of code
}
}).catch(err => console.error(err));
When you go <anything>.client.user it will return the bot client.
If you want to see who created the channel you would have to check the Audit Logs or save it internally.
I've checked the documents. Here's what it says for .client about the
channel. It says the person that initialized the channel, or the
person that created it.
On documentation I see this:
The client that instantiated the Channel
instantiated is different of initialized

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.

Return the channel ID of a channel mentioned in a command

The title says all. I'm making a Discord bot in node.js, and one part that I'm trying to add is a .setup command, to make the bot less dependent on manually changing the value of client.channels.get().send(), allowing it to be more easily set up in the future.
Anyway, in my case, I'm trying to have the user reply to a message with a channel mention (like #welcome for example), and have the bot return that ID and save it to a variable.
Currently, I have this:
function setupChannel() {
client.channels.get(setupChannelID).send('Alright. What channel would you like me to send my reminders to?');
client.on('message', message => {
checkChannel = message.mentions.channels.first().id;
client.channels.get(setupChannelID).send('Ok. I\'ll send my reminders to that channel.');
});
}
message.mentions.channels returns undefined.
I know that the message.mentions.channels.first().id bit works when its a user instead of a channel, is there a different way of getting a channel mention in a message?
Here is the most easy and understandable way i found :
message.guild.channels.cache.get(args[0].replace('<#','').replace('>',''))
But it not really what we want we could use <#####9874829874##><<<547372>> and it will work. So i figured out this :
message.guild.channels.cache.get(args[0].substring(2).substring(0,18))
I like to just remove all non-integers from the message, as this allows for both IDs and mentions to be used easily, and it will return the ID for you from that, as mentions are just IDs with <# and > around them. To do this you would use
message.content.replace(/\D/g,'')
With this, your code would look like this:
function setupChannel() {
client.channels.get(setupChannelID).send('Alright. What channel would you like me to send my reminders to?');
client.on('message', message => {
checkChannel = message.content.replace(/\D/g,'');
client.channels.get(setupChannelID).send('Ok. I\'ll send my reminders to <#' + checkChannel + '>.');
});
}
From this you will always get an ID. And we can surround the ID to trun it into a mention.

Categories