Discord Bot Node JS simple error .send - javascript

I'm having a weird issue while following this tutorial. Building a JS Discord bot, literally only 33 lines in and its throwing errors about .send being undefined. I've googled around and I can't find anything that has helped get any closer to working this out.
const fs = require("fs");
const Discord = require("discord.js");
const client = new Discord.Client();
const config = require("./config.json");
client.login(config.token);
client.on("ready", () => {
client.user.setGame(`on ${client.guilds.size} servers`);
console.log(`Ready to serve on ${client.guilds.size} servers, for ${client.users.size} users.`);
});
client.on("guildMemberAdd", (member) => {
console.log(`New User ${member.user.username} has joined ${member.guild.name}` );
member.guild.defaultChannel.send(`${member.user} has joined this server`);
});
client.on("message", (message) => {
if (!message.content.startsWith(config.prefix) || message.author.bot) return;
if (message.content.startsWith(config.prefix + "ping")) {
message.channel.send("pong!");
} else
if (message.content.startsWith(config.prefix + "foo")) {
message.channel.send("bar!");
}
});
client.on("error", (e) => console.error(e));
client.on("warn", (e) => console.warn(e));
client.on("debug", (e) => console.info(e));
When ran, the console.log works without fuss, but the message to default channel throws the following error in PowerShell
C:\Users\super\Desktop\autoslap\mybot.js:18
member.guild.defaultChannel.send(`${member.user} has joined this server`);
^
TypeError: Cannot read property 'send' of undefined
Any help would be appreciated, getting frustrated over what is probably something so simple.

I know this is a late reply and you might've already figured out how to do this, but I will still explain to those who may need help.
As of 03/08/2017, there is no more Default Channel in guilds on
Discord. The #general default channel can be deleted, and the
guild.defaultChannel property no longer works - From
https://anidiots.guide/frequently-asked-questions.html
If you want an alternative, the code from https://anidiots.guide/frequently-asked-questions.html may do the trick. Just enter the site and scroll down until you see Default Channel!
If your bot has admin perms, its "first writable channel" is the one at the top. It could be any channel, so if their default channel was deleted you can potentially annoy a lot of people.
Hope this helps!
javascript node.js discord discord.js

You would get that error if the default channel of the server has been deleted. Previously, you weren't able to delete the default channel but now you can. To be sure, make a new server and try it there.

Related

Why doesn't my discord.js bot reply to messages? [duplicate]

This question already has an answer here:
message.content doesn't have any value in Discord.js
(1 answer)
Closed 4 months ago.
So my code is this:
const { GatewayIntentBits } = require('discord.js');
const Discord = require('discord.js');
const token = 'My Token';
const client = new Discord.Client({
intents:
[
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages
]
});
client.on('ready', () =>
{
console.log("Bot is on");
});
client.on('message', (message) =>
{
const channel = client.channels.get("1028177045773094915")
if (message.content === 'ping')
{
channel.send("pong")
}
});
// This is always the end
client.login(token);
I don't know why it is not working, it's in online but when I type anything in channel it doesnt do anything.
There are a few errors with this.
Before I get into the more complicated stuff, please know that the message event doesn't work -- use messageCreate.
The second smaller thing that I need to point out is that you don't have the MessageContent Intent enabled; that needs to be enabled for you to detect the text/attachments of a message that isn't a Direct Message to the bot, and doesn't ping/mention the bot.
Now, for the bigger stuff.
client.channels.get() isn't a function, and that's because of the Channel Cache.
I'll try my best to break down what a Cache is, but I'm not too good with this, so someone can correct me below.
A cache is similar to a pocket; when someone (being discord.js) looks inside of your pocket (the cache), that someone can only see the items inside of the pocket. It can't detect what isn't there. That's what the cache is. It's the inside of the pocket, and holds the things that you want discord.js to see.
If your item isn't in the cache, discord.js won't use it. But, you can get discord.js to look for that item elsewhere, and put it into that pocket for later.
This is the .fetch() method. It puts something that isn't in the cache, well, in the cache.
An example of using it:
const channel = await client.channels.fetch("CHANNEL_ID");
Or, what I'd call the more "traditional" way of doing it:
client.channels.fetch("CHANNEL_ID").then(channel => {
});
Now, after fetching your channel, you can call it like this:
client.channels.cache.get("CHANNEL_ID");
Another fun thing about the .fetch() method is that you can use it to shove everything in the cache, though it isn't necessary in most cases.
client.channels.fetch();
That will fetch every channel that the bot can see.
client.guilds.fetch();
That will fetch every guild the bot is in.
guild.members.fetch();
That will fetch every guild member of a guild.
And so on.
After Everything
After applying everything that I've said previously, you should wind up with code that looks like this:
const { Client, GatewayIntentBits } = require("discord.js");
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent // Make sure this is enabled in discord.com/developers/applications!
]
});
client.on("messageCreate", async(message) => {
if(message.content.toLowerCase() === "ping") {
// I added the .toLowerCase() method so that you can do PING and it works.
client.channels.fetch("1028177045773094915").then(channel => {
channel.send({ content: "pong!" });
});
}
});
client.login("Your Token");

Delete existing categories and channels and make new ones(discord.js)?

I need some help with a code in discord.js where I delete the existing categories and channels and then make new ones. My code edits the icon and guild name so now I'm moving onto channels and whatnot. No, this is not a nuker, I'm trying to make a server rebuilder or remodeler and things similar to that. Anyways, here is my code and by the bottom is where I'd like to implement the channel deleting and replacing.
const Discord = require('discord.js');
const client = new Discord.Client();
const guild = new Discord.Guild();
const { prefix, token } = require('./config.json');
client.once('ready', () => {
console.log('Ready!');
});
client.login(token);
client.on("message", async (message) => {
if (message.content == "server") {
try {
await message.guild.setIcon("./icon.png");
await message.guild.setName("server");
message.channel.send("Successfully made server.");
} catch {
message.channel.send("Unknown error occurred while making server.");
}
}
});
If anyone could help with this, please let me know.
Your question is too broad, you should try to narrow it to something specific that you are having issues with, like #Skulaurun Mrusal told you to. However, I can give you some tips on where to start:
You can access a <Guild>'s <GuildChannelManager> - a class that gives you some ways of managing the guild channels, such as deleting or creating them - through <Guild>.channels.
If you'd like to delete a channel from this <Guild> you would do <Guild>.cache.channels.find(channel => channel.name === 'channel-name').delete()
While if you wanted to create one, you would do: <Guild>.channels.create(...).
Don't forget you need the MANAGE_CHANNELS permission to do such operations. I hope this can help you out.

Message reaction

I am trying to get my Discord bot to react to people's messages. For example, if somebody said "welcome", the bot will react to their message, because it has the word welcome, which I would set the keyword too. I understand how to get the bot to reply to messages with a response, but not reactions.
In discord.js you can use something like this:
const Discord = require("discord.js");
const client = new Discord.Client();
client.on("message", message => {
if(message.content === "welcome") {
message.react("😄");
}
}
client.login("YOUR_TOKEN");
This in the Discord.js Docs might be helpful as well.

TypeError: channel.join is not a function

Hello I am a bit stuck here, I am trying to make my bot join VC but it returns:
(node:847) UnhandledPromiseRejectionWarning: TypeError: channel.join is not a function
Help would be appreciated :)
client.on("ready", () => {
const channel = client.channels.cache.get("757004209701912587");
if (!channel) return console.error("The channel does not exist!");
channel.join().then(connection => {
// Yay, it worked!
console.log("Successfully connected.");
}).catch(e => {
// Oh no, it errored! Let's log it to console :)
console.error(e);
});
});
Apparently it's really not a function.
After encountering the same problem and searching to no avail for solution for some time, I've stumbled upon this example code: https://github.com/discordjs/voice/tree/main/examples/radio-bot
It uses completely different principles, but by integrating part of it into my code I've managed to create voice connection successfully.
In order to get a valid channel you will first need get a guild from your client.
const guild = client.guilds.cache.get("YOUR_GUILD_ID");
Then you can change your code to:
const channel = guild.channels.cache.get("757004209701912587");
It should work then.

!FIXED! How do you send a message in a channel on startup in discord.js"

Fixed elsewhere.
I'm currently coding a discord.js bot and when the bot starts it will send a message in a certain channel. How do you do that?
I know this might sound stupid but I'm quite new to discord.js. Thanks in advance.
client.on("ready", () => {
const Channel = client.channels.cache.get("ChannelID");
if (!Channel) return console.log("Invalid channel.");
Channel.send("Bot startup.");
});

Categories

Resources