Reputation: 3
I went through other questions trying to find the answer to this and came to the following code:
let Prayer = message.member.user.tag;
bot.users.cache.get("my_id").send("My goddess, " + Prayer + " has sent you a prayer!");
However, I get this error:
TypeError: Cannot read property 'send' of undefined
I don't know why it says this since it works for others as well. Can someone help me?
Edit:
New code is now
const Discord = require("discord.js");
const bot = new Discord.Client();
let Prayer = message.member.user.tag;
async function SendingPray() {
message.reply("Test3");
const Yuuki = await bot.users.fetch('715912580127785060');
message.reply("Test4");
console.log(Yuuki);
message.reply("Test5");
Yuuki.send("Testing");
message.reply("Test6");
};
SendingPray();
message.reply("your pray has been sent. The goddess will read your pray and decide your fate!");
It gets as far as Test3 but after that Test 4 won't appear and Yuuki won't get logged.
Results: Special thanks to @Karizma for solving this with me!
On the end of index.js must say
module.exports = bot;
While the pray.js (using command handler) has the following code:
module.exports = {
name: 'pray',
description: 'Pray to the goddess!',
usage: '[command name]',
execute(message, args){
const Discord = require("discord.js");
const { Client } = require('discord.js');
const bot = require("path_to_index.js")
const token = require("path_to_your_token");
bot.login(token.login_token);
//token_login is how I stored my token to login the bot
let Prayer = message.member.user.tag;
async function SendingPray() {
const Yuuki = await bot.users.fetch('ID_wanted').catch(err => {
console.error(err);
//means the user id is invalid
});
Yuuki.send("My goddess, " + Prayer + " has sent you a pray!").catch(console.error);
};
SendingPray();
message.reply("your pray has been sent. The goddess will read your pray and decide your fate!");
}
}
The problem was that the ID couldn't be fetched while the bot hasn't been redefined through the new file by adding code in 7-9, directing back to index.js
Upvotes: 0
Views: 65
Reputation:
Since you are fetching a user you have to call .catch
on the method incase it fails
async function SendingPray() {
message.reply("Test3");
const Yuuki = await bot.users.fetch('715912580127785060').catch(err => {
console.error(err);
//means the user id is invalid
});
message.reply("Test4");
console.log(Yuuki);
message.reply("Test5");
Yuuki.send("Testing");
message.reply("Test6");
};
I have tested out the id on discord and it seems that it is invalid.
Upvotes: 1