discord.js send message on specific channel - discord

I want to send a message on a specific channel. but the thing is my command should be like this +post CHANNELID message then it will post the message on this channel that I've put the id.
so can anyone help me to do that??
example command I want

This is pretty simple actually. There are a few things you need to do.
Separate the arguments
Find channel with first argument (id)
Send message to the channel with content of other arguments joined with spaces
This is a working example of your command
client.on("message", msg => {
const args = msg.content.split(' ');
const [, chanId, ...message] = args;
const channel = msg.guild.channels.resolve(chanId);
channel.send(message.join(" "))
})

Related

Why isn't my code working? Discord.js index.js client.on

It's not sending a message, there are no errors I tried modifying the code nothing is working the bot welcome message still works I think that it's not updating with replit.
I tried modifying the code switching some stuff around basically doing stuff if I see any flaws I made.
client.on('message', message => {
// We'll want to check if the message is a command, and if it is, we'll want to handle it
if (!message.content.startsWith(prefix) || message.author.bot) return;
// Split the message into an array of arguments, with the command being the first element
const args = message.content.slice(prefix.length).split(/ +/);
const command = args.shift().toLowerCase();
// Check if the command is "privatevc"
if (command === 'Privatevc') {
// Check if the member is in a voice channel
if (!message.member.voice.channel) {
// If the member is not in a voice channel, send a message letting them know they need to be in one to use the command
return message.channel.send('You need to be in a voice channel to use this command.');
}
// If the member is in a voice channel, create a private voice channel for them
message.member.voice.createChannel({type: 'Voice'}).then(channel => {
// Send a message to the member letting them know the private voice channel has been created
message.channel.send(`Private voice channel created for you: ${channel}`);
}).catch(console.error); // If there was an error creating the private voice channel, log it to the console
}
});
This code is for Discord.js v12. If using the newest (v14) you need to use the messageCreate event instead of message. So the code would be:
client.on('messageCreate', message => {
You might also enable the MessageContent and GuildMessages intents.
IntentsBitField.Flags.GuildMessages,
IntentsBitField.Flags.MessageContent
To make it all work you need to enable at least the third option, as you might see in the screenshot.

Redirect to a channel as well as ping a role then post a message using args

I'm making an announcement command for my bot and want it to go to the announcements channel automatically, then ping the first role mentioned and finally display the message.
For now I have it going to staff commands as a placeholder to get it working, this does not work or throw an error. It properly pings the role, but nothing after that shows up, even the "Debug". What am I missing. I've seen the channel part work but it doesn't for me, and I can't find a similar situation online for the message itself.
module.exports.run = async (client, message, args) => {
if (message.author.bot) return;
let prefix = config.prefix;
if(!message.content.startsWith(prefix)) return;
const channel = message.guild.channels.cache.find(c => c.name === '🚔|staff-cmds');
let pingRole = message.mentions.roles.first();
let messageContent = args.slice(1).join(' ');
channel.send(pingRole, messageContent, "Debug");
This is what happens when the command is run
Try this channel.send(`${pingRole} ${messageContent}`)
The channel.send() function takes only two parameter "content" and "options" you can read more about it here

My discord.js embed message isnt sending to a specific channel

I don't know what the problem is, here is the code I am using, including the line that is meant to send it to the specified channel:
const Discord = require('discord.js')
const client = new Discord.Client();
const Discord = require('discord.js')
const client = new Discord.Client();
client.on("message", message => {
const embed = new Discord.messageEmbed()
embed.setAuthor(`Phaze Bot`)
embed.setTitle(`Commands List`)
embed.setDescription(`$kick: kicks a member \n $ban: bans a member \n $help music:
displays music commands \n $help: displays the help screen`)
client.guild.channels.cache.get(`801193981115367496`).send(embed)
})
client.login('login in here');
It is not sending this embed to the channel (by ID). Can anyone see where I am wrong?
update
it is now working, but still does not send the code to the channel when i message in it. the Terminal also shows this:
TypeError: Cannot read property 'channels' of undefined
This bot would send an embed every time a user sent a message in any channel.
You also have a typo at your client.on("message", (_message) => {
This needs to be:
client.on("message", message => {
Also, you need the client#channels#cache#get to be:
message.guild.channels.cache.get('801193981115367496').send(embed);
Since the ID is a string, it needs to be held in inverted commas or quotation marks.
As mentioned by Rémy Shyked, quoting the linter from VSCode:
Numeric literals with absolute values equal to 2^53 or greater are too large to be represented accurately as integers
The ID value is too large to be treated as an integer accurately
As mentioned above, your bot is going to send this embed every time a message is sent. Here is how you would make it respond to a basic help command (without using a handler):
const Discord = require('discord.js')
const client = new Discord.Client();
client.on("message", message => {
if (message.content.toLowerCase() === '!help') {
const embed = new Discord.messageEmbed()
embed.setAuthor(`Phaze Bot`)
embed.setTitle(`Commands List`)
embed.setDescription(`$kick: kicks a member \n $ban: bans a member \n $help music:
displays music commands \n $help: displays the help screen`)
message.guild.channels.cache.get('801193981115367496').send(embed);
};
});
client.login('client login here');
Also, please use semi-colons, and stop using backticks (`) when they aren't necessary - it saves a lot of errors later.

send DM to user specified by command arg in discord.js

I am making a bot for my server and want it to send a DM to a user, I know about message.author.send("Your message here.") but what I want is for it to be done via a command. E.G. !dm {user} {message}. how do i do that?
Once you parse your arguments you can use this code within your command (make sure this is inside of an async function).
let mention = args[1].match(/^<#!?(\d+)>$/)[1];
if (!mention) return message.channel.send('Invalid user.');
let recipient = await client.fetchUser(mention);
recipient.send(args.slice(2));

How to fetch a message from the entire guild

I would like to know how to find a message with its id from the entire guild and not just the current channel.
I have tried
message.guild.channels.fetchMessage(message_ID)
client.channels.fetchMessage(message_ID)
message.channel.guild.fetchMessage(message_ID)
message.fetchMessage(message_ID)
message.guild.fetchMessage(message_ID)
I want it to look through the entire guild and find the message by ID then send what the message has said. Also, I have tried to have it look for the channel that I want the message from but that didn't work either.
If it's possible to get more specific with the code, it should look into a channel called #qotd-suggestions with the ID of 479658126858256406 and then find the message there and then send the message's content.
Also, the message_ID part of fetchMessages gets replaced with what I say after the command so if I say !send 485088208053469204 it should send Message content: "Wow, the admins here are so lame."
There's no way to do that with a Guild method, but you can simply loop through the channels and call TextChannel.fetchMessage() for each of them.
In order to do that, you could use Collection.forEach() but that would be a little more complex if you want to work with vars, so I suggest you to convert Guild.channels to an Array with Collection.array().
Here's an example:
async function findMessage(message, ID) {
let channels = message.guild.channels.filter(c => c.type == 'text').array();
for (let current of channels) {
let target = await current.fetchMessage(ID);
if (target) return target;
}
}
You can then use this function by passing the message that triggered the command (from which you get the guild) and the ID you're looking for. It returns a Promise<Message> or, if the message is not found, Promise<undefined>. You can get the message by either using await or Promise.then():
let m = await findMessage(message, message_ID); // Message or undefined
// OR
findMessage(message, message_ID).then(m => {/* your stuff */});

Resources