Discord.JS Message in a specific Channel - javascript

So i'm new to the whole Discord.JS thing and i am trying to figure out how to get the bot to write a message to the General Chat when a new person joins.
I have seen a lot of people doing it like this:
const channel = client.channels.cache.find(channel => channel.name === channelName)
channel.send(message)
But this isnt working for me. Whenever i am trying to send a message with the channel.send(message) it gives me a error message.
I have also tried the version where you do it with the client.channels.cache.get(<Channel-ID>)
But this also didnt work for me.

Use this code similar to the post: how to send a message to specific channel Discord.js
<client>.channels.fetch('<id>').then(channel => channel.send('<content>'))
Example
client.channels.fetch('922880975074127872')
.then(channel => channel.send(`Welcome, ${user}`))

Related

discord.js Command that gives/removes role from everyone, and only an Admin can use it

I would like to make a command that gives everyone a role, and only an admin can use it.
I found this piece of code on the internet and I tried to modify it, but nothing is helping me out, and I've been reading the error, and I still get nothing
client.on("message", message => {
if (message.content === 'grimm!rainbow') {
let role = message.guild.roles.cache.find(r => r.name == 'Rainbow')
if (!role) return message.channel.send(`a Rainbow role was not found, create one and set it on top of all roles for this command to work!`)
message.guild.members.filter(m => !m.user.bot).forEach(member => member.addRole(role))
message.channel.send(`**${message.author.username}**, The Rainbow has been turned on!`)
}});
And another thing, I would like it if this command can only be used by an admin, but I've been struggling with code in that range, and I have no clue on how to work it.
If someone could help me out a little bit? and explain what I'm doing wrong, I would really appreciate it! Thanks!
To check if user have admin permission you need to using .hasPermission() docs here:
if(message.member.hasPermission('ADMINISTRATOR'){
// Doing Something
}
To add roles to all user you need to access guild members cached , doc here and then loop through all members , access their roles and you will have .add() for add specific role docs here. Similar for remove
message.guild.members.cache.filter(m => !m.user.bot).forEach(member => member.roles.add(role))

How do I make my Discord.JS bot react to it's own Direct Message?

When I'm making my bot, I want to make it so that when you type afv!support it will send a DM like this:
DM example
Thanks for contacting AFV support. Please react below for which problem you have.
:one: Ban Appeal
:two: Bug Report
:three: Report Staff/User
This command will expire in two minutes.
and then react with :one:, :two:, and :three:. I've looked around but haven't found the answer.
Thanks for the help!
Elitezen's answer is correct, I just wanted to elaborate on their use of <emote>.
If you replace the first usage of <emote> with :one:, for example, it will not work correctly. You will have to enter the physical unicode emoji, or emoji ID if it's a custom emoji.
For example:
message.member.send(<theMessage>).then(msg => {
await msg.react('1️⃣') // instead of :one:
await msg.react('2️⃣') // instead of :two:
msg.react('3️⃣') // instead of :three:
})
Read this discord.js guide on reactions for more information
Send a message to your target, pass the message into a .then(), Where the bot will react to the message
message.member.send(<theMessage>).then(msg => {
msg.react('<emoji>')
msg.react('<emoji>')
msg.react('<emoji>')
})

How do I get a discord bot to mention a role and a user?

I am trying to make a bot that, whenever someone says something, it sends a message in the channel saying that that person is there and is waiting for someone to join. However, it only prints #ne1 (the role I want it to mention) as plain text. Also, when I have it mention the user, it only writes the user ID instead of actually sending #(username).
const Discord = require('discord.js');
const keepAlive = require('./server');
const client = new Discord.Client();
client.on('message', message => {
if (message.toString().toLowerCase().includes('ne1 here')) {
message.channel.send('#ne1, ' + message.author + ' is online and waiting for you to join!');
}
});
keepAlive();
client.login(process.env.TOKEN);
What I want is for it to mention the #ne1 role and the person who used the command to run the bot. It only prints, "#ne1 571713056673890324 is online and waiting for you to join!"
Discord mentions (role, user, etc.) have a special format as detailed in the documentation here.
So, to mention a user you need to use their user ID <#USER_ID>/<#!USER_ID> and for a role it is similarly <#&ROLE_ID>.
In order to mention a user, you must have their User ID and use the following format:
<#USERID> or <#!USERID>. For roles do <#&ROLEID> instead.
Make changes to your message to accommodate it:
message.channel.send(`<#&${ROLEID}> <#${message.author}> is online and waiting for you to join!`);
I guess you are using discord.js v12
If you want the easiest way to mention user you can use
message.channel.send(`#ne1, ${message.author} is online and waiting for you to join!`);
Also I'd use ID of role instead of just #role
You can get the ID by using #role and then just using the thing discord outputs
Usually something like <#&randomnumbers>

how to get a bot discord to respond on a specific text channel

I'm trying to get the bot to respond in a specific text channel, and not in the one where they wrote the message:
I tried with
case 'provino':
message.channel.find(channel => channel.name === "provino")
but then I wouldn't even know how to go on; I also searched for a video but I only find people using the ID of the text channel, which I don't know how to find on Discord.
You need to find the channel, like you did, and then assign the result to a new variable for further usage :
case 'provino':
const newChannel = message.guild.channels.cache.find(channel => channel.name === "provino");
newChannel.send("Hello world.");

discord.js sending message to specific channel

I've been looking around, can't quite seem to find the answer to this issue I am having with a discord bot I am making with Typescript. I have all of my commands in their own folder using a separate file for each command. Helps to keep things organised.
I've seen people say client.channels.get(`channelID`).send(`Text`)
but that's giving me
Object is possibly 'undefined'. and Property 'send' does not exist on type 'Channel'.
I'm actually trying to make a bot message every text channel (given from a list) whenever someone runs a reboot command because for whatever reason people keep rebooting the bot. I implemented it as a funny thing to do every now and again as a troll if someone needs to use it. The bot goes offline for 3 minutes but I don't like having people spam it and pretty much have the bot un-usable.
I'm using client.channels.get(channels.channelnames[5]).send("This is a message.")
Solution:
if(msgObject.member.guild.channels.find(channel => channel.name === channels.channelnames[5]) as Discord.TextChannel) {
var txtchannel = msgObject.member.guild.channels.find(channel => channel.name === channels.channelnames[5]) as Discord.TextChannel
txtchannel.send("This is a message in a channel. Don't know why you read this.")
}
so I was on the right track mostly. just had to do as Discord.TextChannel and I think that's why Cynthia was saying about casting the variable as a TextChannel
This code works. thanks for all your help guys!
According to https://discord.js.org/#/docs/main/stable/class/Collection it seems like there is no get method.
Try client.channels[channels.channelnames[5]].send("This is a message.")
In other words try to replace .get with the square brackets.
EDIT: Sorry I was a bit quick, I think the issue is type casting, try casting the Channel to a TextChannel if you know that it is a text channel.
This should work assuming your channels are text ones.
client.on('ready',(e)=>{
let channel_ids = ['123','456','789'];
// loop through the list of channel ids.
for(let i=0, l=channel_ids.length; i<l; i++){
let channel_id = channel_ids[i];
let this_channel = client.channels.get( channel_id );
// if exists, and type in list send message
if(this_channel && ['dm', 'group', 'text'].indexOf( this_channel.type ) != -1){
this_channel.send('a cool message')
.then(message => console.log(`Sent message: ${message.content}`))
.catch(console.error);
}
}
});

Categories

Resources