0

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.

2 Answers 2

1

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!

Sign up to request clarification or add additional context in comments.

Comments

0

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.

3 Comments

I'm working on a brand new server, just me and the bot
You sure it doesn't have something to do with the perms on the server?
I think its actually got something to do with my installs being broken after some additional testing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.