Discord.js specific channel - discord.js

I have such a problem that I want to send a message via the bot only to a given channel. How can I do this?
Client.channels.cache.get(id).send(text)
This don't work

You can do like so with discord.js v12:
let channel = client.guilds.cache.get('ID_of_the_guild_in_which_the_channel_is_in').channels.cache.get('channel_ID');
if(channel) channel.send('your message');

Related

Discordjs: Specifying channel for an interaction.reply

I am using Discordjs v13. I have created a slash command and I am able to "print" a message using
await interaction.reply(messageObj);
I need to send the reply to a different channel where the command was triggered, is this possible?
Something like:
interaction.setChannel(channelId).reply(...)
OR
interaction.reply({
channel: ....
....
})
What you want is not possible. The Discord API does not allow to specify the channel where the app interaction should be replied in: https://discord.com/developers/docs/interactions/receiving-and-responding#responding-to-an-interaction
However if you are concerned with the reply being shown to everyone, you can make the reply ephemeral. If you want to log interactions, you can reply to the interaction then send another message using the solution provided in the comments of your question.
this is actually possible in discordjs v14 and may be in v13 as well. Carl-bot does it with suggestions.but its not an actual reply
use
interaction.guild.channels.cache.get('channel-id').send('message')
this will send a message in a the select channel you may still want to
interaction.reply({Content:'replied in #channel' [embed], ephemeral: true })
so the user knows the reply was redirected . ephemeral: true makes the replay only visible to the user that evoked the interaction.
and if you need the message id for the new message use
const msg = await interaction.guild.channels.cache.get('channel-id').send('message');
to send the message and you can do something like const messageid = msg.id

How to make bot change channel permissions?

So, the Discord bot that I'm making is sort of a server manager kind of thing and I'm making a channel lock command which basically makes it so people can't send messages in the channel.
How would I make it so that when someone types the command it turns the 'Send Messages' permission FALSE?
Any help is appreciated! Thanks.
const Channel = client.channels.cache.get("ChannelID");
if (!Channel) return console.log("Invalid channel ID.");
Channel.updateOverwrite(message.guild.roles.everyone, {SEND_MESSAGES: false});

Discord.js - Get Another Channel's Topic

I'm trying to basically do what the title says.
I've tried
let channel = message.channel.get(68352726916713XXXX)
message.channel.send(channel.topic)
and stuff like that, But nothing works.
Any suggestions or answers?
If you are trying to get a channel in the same guild as the message was sent in, use message.guild.channels.get. Otherwise, use client.channels.get.
let channel = message.guild.channels.get("68352726916713XXXX");
message.channel.send(channel.topic);

How do I "Link" a channel like a mention in my Discord Bot message?

I'd like our Discord Bot to mention a specific channel, and let it be clickable. I understand mentioning a user you use the user ID. I do have the channel Id, just unsure how to implement it.
You just have to do the following:
message.channel.send('Please take a look at this Discord Server channel <#CHANNELID>')
or if you get the channel id from the bot
const channel = message.guild.channels.find(channel => channel.name === 'Name of the channel');
message.channel.send(`Please take a look at this Discord Server channel <#${channel.id}>`)
Then the channel is clickable like in this screenshot:
It is simple :^)
<#channel.id>
Channels on Discord have this special kinda syntax here:
<#channel id>
As commented by Elitezen here, running toString() on a Channel can do that mention for you. Then, just send that string yourself. It's much simpler than manually doing it.
Like this if you're answering a message:
message.channel.send(message.channel.toString());
Also, like answered by others in this question, you can do that yourself if you feel like it.
If you have a Channel object already, you can do something like this (imagine your channel is named myChannel - it doesn't have to be named that, though):
message.channel.send(`<#${message.channel.id}>`);

Reply to anyone who sent a DM

EDIT: Another question with better answers: How to reply to any DMs sent to the bot?
Is it possible to reply to someone who sent a message to my Discord.js bot?
For example, when someone sends hi to my bot's DMs, the bot should reply Please use !help for the commands' list.
I've tried a lot of attempts to use the MessageCollector but I failed doing it.
Any help would be appreciated.
Thanks!
You can just tell the bot to do that with client.on('message').
Try this:
client.on('msg', () => {
if (msg.channel.type == 'dm' && msg.content.toLowerCase() == 'hi')
msg.channel.send('Please use !help for the commands' list.')
})
This is just a quick example, but you can also add checking for commands and all the other stuff that you do on normal guild channels. To check if the message comes from a DMChannel is to check Channel.type

Resources