How to send a message to a specific channel? - javascript

I'm building a reminder bot for Discord.
The server owner or moderator should subscribe to the server via a message.
I'm having trouble finding the way to send the message to the specific server and channel.
Any idea if there is a way?

If you have the channel ID all you need to do is find the channel and send a message to it:
let channel = client.channels.get('THECHANNELID');
if(channel) {
channel.send("My Message");
}

Related

Using Discord.js, how can I trigger a bot when a webhook is triggered?

I have a bot in a server (for example purposes, let’s say the server is called “John’s Server”), so, in John’s Server, there is also a webhook.
What I want to do is, whenever a webhook message is sent in John’s server, the bot sends a message in a channel. (For the example, let’s say the channel ID id 1234567890)
So, if the webhook in John’s Server with the ID 0000000000 is used to send a message, the bot sends a message in channel 1234567890.
How is this possible? (Using Discord.js, by the way)
To do this you'll have to listen to the message event, when it fires you'll check the Message#webhookID property. If it has a truthy value then the message is sent from a webhook, otherwise it's sent from a user.
Then all you need to do is get the channel from the server and send the message.
Here's an example:
client.on("message", (message) => {
if (!message.webhookID) return; // If the message isn't from a webhook, don't do anything
const channel = message.guild.channels.fetch("0123456789");
// Using the fetch method, discord.js will check the cache first, if the channel is there
// then it will return it, otherwise discord.js will hit the Discord API and get the channel
channel.send(`
A message from ${message.author.username} (${message.webhookID}) has been sent in
${message.channel.name}.
Message: ${message.url}
`);
});

How to send message to a specific channel with discord bot?

I have searched the internet and I haven't found a solution to send a simple message to a specific channel. All the answers that I found were outdated. What's the newest way of sending a message to a specific channel?
I tried this but it doesn't work:
const channel = <client>.channels.cache.get('<id>');
channel.send('<content>');
a channel does not nessecairy have the send function like a voice channel. we have to check it actually is one, with the following code you should be able to:
const channel = <client>.channels.cache.get('<id>');
if(channel.isText()){
channel.send('<content>');
}

Link to message posted by Bot in MSTeams

How can I get the link( which I got on clicking the 3 dots of a message) of all messages posted to the channel by my bot?
After getting this link, I want to DM the link to the user through the bot.
You can take a look at list channel messages API. This will give you the message_id and channel _id later you can try to deep link https://teams.microsoft.com/l/message/{channel_id}/{message_id} to same message using message id or channel. There is also alternate solution for this, subscribe to channel using change notification API. You will be getting all the messages of channel to notification endpoint, you can Deserialize the message payload you will find the copy link url in the payload it self.

How to send message to all member who's have a specific role on our server?

i am make a server bot in discord.js and i need a command from which i can send msg to all member who h X role.. where X be any random role in server
/roledm #admin hello
First, you need to fetch the role from message.mentions.roles. Then you need to filter client.guild.members by members that have such role. Once you have that, you can do a simple for loop to send a message to each of those members.

Sending a message on a guildMemberAdd event in the server on Discord

i'm coding a discord bot with JavaScript. It's running pretty good, but the only thing i can't do is sending a message in the server when a new member joins the server. (Not a private message, directly a message in the server). I don't find any way to do that, any ideas ?
bot.on('guildMemberAdd', function(member){
member.send("Hello there !");
});
This one sends a private message, it does exactly what i don't want it to do ^^'
The member parameter that gets passed into the callback function is exactly that--an object representing a discord member. If you want to send a message to the guild the member just joined, you will need to get the guild property of the member, member.guild and then get the channel you want from channels and send the message you wish. All together it should look something like this:
bot.on('guildMemberAdd', function(member){
member.guild.channels.get("put-channel-id-here").send("Hello there!")
});
I think you may know by now but in Discord.js v12 (current stable version), you would use
member.guild.channels.cache.get("put-channel-id here").send("Hello there!")

Categories