Send message to specific channel (command on module.exports) - javascript

So I don't wanna make a mess out of my Main.js so I try to make every possible command through module.exports in other documents.js
Basically I need that if I send a command, the bot will delete my message and post a comment+embed on a specific channel.
This is what I have (making it simple):
module.exports = {
name: 'chtest',
execute(message, args, Discord) {
let chComment = 'Normal comment';
chComment += '\nLine2';
message.channel.send(chComment)
const chEmbed = blablaEmbedCode
message.channel.send(chEmbed)
message.delete();
},s
};
I've read another Questions and they use
client.channels.cache.get(`Channel_ID`).send('Text')
I tried using it but I got an error ReferenceError: client is not defined
I added Client to my execute line:
execute(client, message, args, Discord) {
And now I have another error TypeError: Cannot read property 'cache' of undefined
And... I don't know what to do now. Any solutions?
Thank you in advance.

Try this using the Message class' client property. Here are the docs for it.
module.exports = {
name: 'chtest',
execute(message, args, Discord) {
let channel = message.client.channels.cache.get('CHANNEL_ID');
//channel is now the channel, unless it could not be found.
channel.send('Message');
/*let chComment = 'Normal comment';
chComment += '\nLine2';
message.channel.send(chComment)
const chEmbed = blablaEmbedCode
message.channel.send(chEmbed)
message.delete();*/
},
};

Related

ReferenceError: msg is not defined

Hello i wanted to mentions the user that are running the command but i am getting a Error
Here is the Code!
const Discord = require("discord.js")
module.exports = {
name: 'not-dropping',
description: 'sets the dropping status!',
execute(message, args) {
if (message.channel.id === '1059798572855476245') {
message.delete(1000);
const name = ("dropping-🔴")
message.channel.setName(name)
message.channel.send(`Successfully set the dropping status to **${name}**\n<#${msg.author.id}> is not Dropping anymore!\nDont Ping Him in your Ticket.`)
}
}
}
I do not understand waht the Problem is
As the error states "msg" is not defined.
In the code "{msg.author.id}" you are using msg while the actual variable is "message".
Change it to {message.author.id}
"msg" There is no such use
You should use "message"
I changed where you need to change in the code :)
const Discord = require("discord.js")
module.exports = {
name: 'not-dropping',
description: 'sets the dropping status!',
execute(message, args) {
if (message.channel.id === '1059798572855476245') {
message.delete(1000);
const name = ("dropping-🔴")
message.channel.setName(name)
message.channel.send(`Successfully set the dropping status to **${name}**\n<#${message.author.id}> is not Dropping anymore!\nDont Ping Him in your Ticket.`)
}
}
}

Cannot read properties of undefined when it shouldn't be undefined in discord.js command handler

I created a small command handler with a simple "ping" command. However, trying to access message.channel shows up undefined. Why is this?
./index.js
//Discord, fs, prefix, client etc. declarations
client.commands = new Collection()
const files = fs.readdirSync("./commands").filter(file => file.endsWith(".js")
for (const file of files) {
const command = require(`./commands/${file}`)
client.commands.set(command.name, command)
}
client.on("messageCreate", message => {
const [command, ...args] = message.content.slice(prefix.length).split(/ +/g)
if (client.commands.has(command) {
client.commands.get(command).execute(message, client, args)
}
})
client.login(/*token*/)
./commands/ping.js
module.exports = {
name: "ping",
description: "Make me say pong",
async execute(client, message, args) {
message.channel.send("Pong!")
}
}
While Message.channel may be valid, message is not an instance of Message. It is actually an instance of Client. The order of the arguments always matter, and putting them in the wrong order can throw TypeErrors. There is 1 simple solution here
Make the arguments in the right order! You can either change the execution, or the declaration
client.commands.get(command).execute(client, message, args)
This is really common and happens for more than sending messages to channels

Discord.js embeds: TypeError: Discord.MessageEmbed is not a constructor

I was recently trying to add a MessageEmbed for my discord bot, but I get this error:
TypeError: Discord.MessageEmbed is not a constructor
I was wondering if anyone knows how to fix this, I have tried some of the rips I could find online, some include trying to re-install node.js and discord.js, other mention a different method like using NewMessageEmbed() instead, but none of them have been working for me, it would be great someone with a bit more experience than me could provide a solution, I have provided all the code involved and screenshot of the error, thanks in advance.
Command file:
module.exports = {
name: 'command',
description: "Embeds!",
execute(message, args, Discord){
const newEmbed = new Discord.MessageEmbed()
.setColor('#FFA62B')
.setTitle('Rules')
.setURL('https://discord.gg/fPAsvEey2k')
.setDescription('**This is an embed for the server rules.**')
.addFields(
{name: '1.', value: 'Treat everyone with respect. Absolutely no harassment, witch hunting, sexism, racism or hate speech will be tolerated.'},
{name: '2.', value: 'No spam or self-promotion (server invites, advertisements, etc.) without permission from a staff member. This includes DMing fellow members.'},
{name: '3.', value: 'No NSFW or obscene content. This includes text, images or links featuring nudity, sex, hard violence or other graphically disturbing content.'},
{name: '4.', value: 'if you see something against the rules or something that makes you feel unsafe, let staff know. We want this server to be a welcoming space!'},
{name: '5.', value: 'Keep public conversations in English.'},
{name: '6.', value: 'This list is not exhaustive and will be updated as we see fit.'}
)
.setImage('./images/rules.png')
.setFooter('Make sure to follow the rules');
message.channel.send(newEmbed);
}
}
Main file:
// grabs the discord.js bot file for import //
const {Client, Intents, Collection} = require('discord.js');
// create the client for the bot //
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });
// prefix to use for bot commands //
const prefix = '!';
const fs = require('fs');
const Discord = require('./commands/discord');
client.commands = new Collection();
const commandFiles = fs.readdirSync('./commands/').filter(file => file.endsWith('.js'));
for(const file of commandFiles){
const command = require(`./commands/${file}`);
client.commands.set(command.name, command);
}
// log to console that the bot has successfully logged in //
client.once('ready', () => {
console.log("Reformed Esports is online");
});
/* Command handler, checking if message starts with prefix and is not the bot,
allowing commands to have multiple words */
client.on('message', message =>{
if(!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).split(/ +/);
const command = args.shift().toLowerCase();
if(command === 'ping'){
client.commands.get('ping').execute(message, args);
} else if(command === 'discord'){
client.commands.get('discord').execute(message, args);
} else if(command === 'pugs') {
client.commands.get('pugs').execute(message, args);
} else if(command === 'command'){
client.commands.get('command').execute(message, args, Discord)
}
});
// bot login using discord bot token //
client.login('blank');
Image of full error:
You should be passing the discord.js module but instead you pass a file. This may have different functions, properties, etc than the discord.js module.
This code will fix the error:
client.commands.get('command').execute(message, args, require('discord.js'))
Additionally, embeds must now be sent using the embeds property
message.channel.send({ embeds: [newEmbed] })
It looks like you are sending ./commands/discord as an argument instead of the real discord.js package. I would add Embed to the const {Client, Intents, Collection} = require('discord.js');, and send Embed instead of Discord inside of client.commands.get('command').execute(message, args, Discord).

Unable to get a channel in discord.js

So, I have been making a suggestions command in discord.js and I keep getting the following error:
Cannot read property 'channels' of undefined
Here is my code, I have tried changing it in many ways but it doesn't work.
module.exports = {
name: "suggestion",
aliases: ['suggest', 'suggestion'],
permissions: [],
description: "suggetion command",
execute(message, args, cmd, client, discord) {
let channel = message.guild.channels.cache.get("865868649839460353");
if (!channel)
return message.reply('A suggestions channel does not exist! Please create one or contact a server administrator.')
.then(message => {
message.delete(6000)
})
.catch(error => {
console.error;
});
}
}
1st idea: The channel you're executing in is not a guild.
By fixing this, you can do:
if(!message.guild){
return
}
2nd idea: You may have specify the wrong channel in the wrong server. Do the following:
let guild = client.guilds.cache.get(`YourGuildId`)
let channel = guild.channels.cache.get(`ChannelId`)
or
let guild = client.guilds.cache.get(`${message.guild.id}`)
let channel = guild.channels.cache.get(`ChannelId`)
I hope these idea helped you! <3

Snipe command returning something I haven't seen before?

so I have a snipe command that works, but after a few uses it returns this? Not sure exactly what's going on, haven't seen it before in any of my bots to my knowledge
(node:2994) UnhandledPromiseRejectionWarning: AbortError: The user aborted a request.
The scripts look like this, however to my eyes they're just a standard database snipe command -
messageDelete event in index.js -
client.on('messageDelete', async (message) => {
db.set(`snipe.content`, message.content);
db.set(`snipe.authorName`, message.author.tag);
db.set(`snipe.authorIcon`, message.author.displayAvatarURL({ format: 'png', dynamic: true }));
});
snipe.js -
const Discord = require('discord.js');
const db = require('quick.db');
module.exports = {
name: 'snipe',
description: 'snipe the last deleted message',
execute (client, message, args) {
let content = db.get(`snipe.content`);
let authorIcon = db.get(`snipe.authorIcon`);
let authorName = db.get(`snipe.authorName`);
const snipeEmbed = new Discord.MessageEmbed()
.setColor('RANDOM')
.setAuthor(authorName, authorIcon)
.setDescription(content)
.setTimestamp()
message.channel.send(snipeEmbed)
}
}
(node:2994) UnhandledPromiseRejectionWarning: AbortError: The user aborted a request.
This error is usually triggered when the bot can not connet to discord or database api.
P.S: I suggest add a snipe.channel property to avoid full client sniping.

Categories