Send is not a function discord.js - discord

I am trying to send a message to channel but I keep getting Error: send is not a function.I have been stuck on this problem for over an hour.
What I have tried:
using sendMessage
tried reading the discord.js documentation but it doesnt seem to be working at all.
Here is my code:
//Grab the Discord Library
const Discord = require("discord.js");
//What will connect to the server.
const bot = new Discord.Client();
//
bot.on('ready', () => {
console.log("Connected as " + bot.user.tag)
//Shows and set the activity of the user.
bot.user.setActivity("El Professor build me", {type: "Watching"})
//Inform you of the servers this bot is connected to.
bot.guilds.forEach((guild) => {
console.log(guild.name)
guild.channels.forEach((channel) => {
console.log(` - ${channel.name} ${channel.type} ${channel.id}`)
})
//Voice Channel ID =
})
var generalChannel = bot.channels.get("123456789").send("Hello World")
})
Error message:
C:\Users\F4_ALFA\documents\FirstDiscordBot\index.js:21
var generalChannel = bot.channels.get("123456789").send("Hello World")
^
TypeError: bot.channels.get(...).send is not a function
at Client.bot.on (C:\Users\F4_ALFA\documents\FirstDiscordBot\index.js:21:63)
at Client.emit (events.js:194:15)
at WebSocketConnection.triggerReady (C:\Users\F4_ALFA\documents\FirstDiscordBot\node_modules\discord.js\src\client\websocket\WebSocketConn
ection.js:125:17)
at WebSocketConnection.checkIfReady (C:\Users\F4_ALFA\documents\FirstDiscordBot\node_modules\discord.js\src\client\websocket\WebSocketConn
ection.js:141:61)
at GuildCreateHandler.handle (C:\Users\F4_ALFA\documents\FirstDiscordBot\node_modules\discord.js\src\client\websocket\packets\handlers\Gui
ldCreate.js:13:31)
at WebSocketPacketManager.handle (C:\Users\F4_ALFA\documents\FirstDiscordBot\node_modules\discord.js\src\client\websocket\packets\WebSocke
tPacketManager.js:103:65)
at WebSocketConnection.onPacket (C:\Users\F4_ALFA\documents\FirstDiscordBot\node_modules\discord.js\src\client\websocket\WebSocketConnecti
on.js:333:35)
at WebSocketConnection.onMessage (C:\Users\F4_ALFA\documents\FirstDiscordBot\node_modules\discord.js\src\client\websocket\WebSocketConnect
ion.js:296:17)
at WebSocket.onMessage (C:\Users\F4_ALFA\documents\FirstDiscordBot\node_modules\ws\lib\event-target.js:120:16)
at WebSocket.emit (events.js:189:13)

Discord.js uses cache and your code has no '.cache', you should try
bot.channels.cache.get('id').send('Hello world!')

The channels property returns a Collection with pair of ID and Channel object.
The Channel class could represent any channel in discord. You couldn't send any messages through it because it doesn't have send() method.

I’ve used this before client.guilds.find(x => x.name === "ChatLogs").channels.find(y => y.name === "server-testing1").send(botEmbed) try that

Related

Bot sends a embed when joins a server

Im trying to make my bot send a embed in the server that it joins (E.G a thank you for inviting and what the bot can do).This is the error that I am getting.
const channel = message.guild.channels.cache.find(channel => channel.type === 'text' && client.user.me.permissions.has("send_messages"))
^
TypeError: Cannot read properties of undefined (reading 'channels')
this is the code that i am using in the index.js file
client.on('guildCreate', message => {
const channel = message.guild.channels.cache.find(channel => channel.type === 'text' && client.user.me.permissions.has("send_messages"))
const embed = new MessageEmbed()
.setColor('GREEN')
.setTitle('Thank You')
.setDescription("Thank you for inviting Rynwin to your server. To get the command list please do /help.\n \n The default Bot prefix is ```<```")
.addFields([
{
name: "Our Support Server",
value: "If you require assistance or would like to get daily bot updates please join our support discord [here] ()"
},
{
name: "Commands",
value: "Try some of these commands; \n\n**/prefix <prefix>** - Sets the server prefix\n\n**/help** - Get the command list or information on a command \n\n**/botinfo** - Shows info on the bot"
},
channel.send({ embeds: [embed] })
])
There are a few things wrong on the first 2 lines. Firstly, guildCreate gives a Guild instance, not a message. Secondly, you aren't checking the correct channel type or permission. Here is the fixed code:
client.on('guildCreate', guild => {
const channel = guild.channels.cache.find(channel => channel.type === 'GUILD_TEXT' && guild.me.permissions.has("SEND_MESSAGES"))
// GUILD_TEXT channel type, guild.me for client guild member, and uppercase permission
// rest of code...
}

How to make a discord bot read embed

I have favorite discord bot game called "EPIC RPG" there's an event for players, so I wanted to make a bot that can announce the event with mention a specific role and adding some messages, how to fix this?
TypeError: Cannot read property 'includes' of undefined
Here's my code
client.on('message', message => {
let embed = message.embeds[0];
if (embed && embed.title.includes('Type ``join`` to join the arena!')) {
message.channel.send( "<#&757597420275368076>" +"\:moneybag:" + "**CATCH**" + "\:moneybag:" );
}
})
https://prnt.sc/uuci4z
You can do that with the following code ( for Discord.js V 13 ) :
client.on('messageCreate', message => {
let embed = message.embeds[0];
if (embed && embed.title?.includes('Type ``join`` to join the arena!')) {
message.channel.send( "<#&757597420275368076>" +"\:moneybag:" + "**CATCH**" + "\:moneybag:" );
}
})

Discord.js Sharding, How do you send an embed message with broadcastEval?

I'm trying to send an embed message to an specific channel with a sharded bot.
I've achieved sending a simple message successfully with this code:
client.shard.broadcastEval(`
(async () => {
let channel = await this.channels.get("683353482748756047");
channel.send("Hello")
})()
`)
The problem starts when I want to send an embed message. I've tried passing the variable like this:
//exampleEmbed is created
client.shard.broadcastEval(`
(async () => {
let channel = await this.channels.get("683353482748756047");
channel.send('${exampleEmbed}')
})()
`)
but the message is sent like "[object Object]".
I thought about returning the channel object back outside of broadcastEval and then sending my variable, but I've read this is not possible because you can't return full discord objects.
How should I send the embed message? Thank you for your time.
Okay, I solved it by creating the embed message inside the broadcastEval, and using the '${}' syntax to poblate it.
Example:
client.shard.broadcastEval(`
(async () => {
const Discord = require('discord.js');
let channel = await this.channels.get("683353482748756047");
if(channel){
//if shard has this server, then continue.
let message = new Discord.RichEmbed()
.setThumbnail(this.user.displayAvatarURL)
.setTitle('Title')
.addField("Something useful:", '${useful}')
.addField("Another useful thing:", '${useful2}')
.setTimestamp()
channel.send(message)
}
})()

How to make a bot greet newcomers?

I would like to make a bot that greets users that joined the server.
Any help is appreciated.
Basically, you need to listen for the guildMemberAdd event, which is when someone joins.
After that, you need to check if the server is YOUR server, get the channel, and send the welcome message.
client.on('guildMemberAdd', async member => {
if (member.guild.id !== "YOUR-GUILD-ID") return;
var channel = client.channels.cache.get('YOUR-CHANNEL-ID');
channel.send(`Welcome to the server, <#!${member.id}>!`);
});
const defaultChannel = guild.channels.find(channel => channel.permissionsFor(guild.me).has("SEND_MESSAGES"));
const userlist = newUsers.map(u => u.toString()).join(" ");
defaultChannel.send("Welcome our new users!\n" + userlist);
newUsers.clear();
this is a code sample
Welcome dm message guildMemberAdd (Discord.js Version 11.4.2)
client.on('guildMemberAdd', async member =>{
await member.send(Embed);
});
Goodbye dm message guildMemberRemove (Discord.js Version 11.4.2)
client.on('guildMemberRemove', async member =>{
await member.send(Embed);
});

Cannot send message to just created channel

I'm trying to send a message to a channel just after I created it, but it doesn't seem to work. I'm using discord.js#v12
This is the code:
message.guild.channels.create(cpl, 'text').then(ma => {
ma.setParent(cat);
ma.lockPermissions();
}).catch(err => console.log(err))
let nChannel = bot.channels.cache.find(ch => ch.name === cpl)
console.log(nChannel)
let embed = new Discord.MessageEmbed()
.setTitle("New Ticket")
nChannel.send(embed)
This is what is logged to the console:
Cannot read property 'send' of undefined
this is because you're not waiting for the channel to be created before trying to send the message. In the same way you wait for it to be ready before using .setParent() and .lockPermissions(), you should wait before using .send().
Here's how I would do it:
message.guild.channels.create(cpl, 'text').then(async (newChannel) => {
// You're inside .then, so all of this is happening AFTER the channel is created
// Wait for .setParent and .lockPermissions to fulfill
await newChannel.setParent(cat)
await newChannel.lockPermissions()
// You can now send your message
let embed = new Discord.MessageEmbed()
.setTitle("New Ticket")
newChannel.send(embed)
})

Resources