cloudxleaf
cloudxleaf

Reputation: 57

Discord Bot UnhandledPromiseRejectionWarning: TypeError: generalChannel.send is not a function

I'm running into this error, but I'm not really sure what's causing it. I tried Googling it and fixed my previous error, where get was an old function which is now called fetch -- but I couldn't find anything for set. I've also tried npm install and npm update as well to see if there was something corrupted. Is there something I'm missing here?

const {
    prefix,
    token,
} = require('./config.json');
const ytdl = require('ytdl-core');

const client = new Discord.Client();
client.login(token);

client.once('ready', () => {
    console.log('Ready!');
   });
   client.once('reconnecting', () => {
    console.log('Reconnecting!');
   });
   client.once('disconnect', () => {
    console.log('Disconnect!');
   });

client.on('ready', () => {
    var generalChannel = client.channels.fetch("2131436123767") // Replace with known channel ID
    generalChannel.send("Hello, world!")  
})```

Stack
  [1]: https://i.sstatic.net/Y2zOD.png

Upvotes: 1

Views: 134

Answers (1)

user13429955
user13429955

Reputation:

Fetch is an async function which means the code goes on without it resolving by default

to fix this either use await or then

Await:

client.on("ready", async () => {
  const generalChannel = await client.channels.fetch("2131436123767");
  generalChannel.send("Hello, world!");
});

Then:

client.on("ready,", () => {
  client.channels.fetch("2131436123767")
     .then(generalChannel => generalChannel.send("Hello, world!"));
});

Also you have to instances of on "ready", merge them into one, and you shouldn't put the other on events inside of the ready event.

Upvotes: 2

Related Questions