Reputation: 4671
I'm using discord14 and typescript. I have a weird behaviour, sometimes the bot send a message, and sometime I get this error
TypeError: Cannot read properties of undefined (reading 'send')
at DiscordService.discordTest (/discord.service.ts:55:17)
at processTicksAndRejections (node:internal/process/task_queues:95:5)
at DiscordController.discordTest (/discord/discord.controller.ts:23:16)
My code is simple
async discordTest() {
// Creating a new client:
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent
]
});
const token = 'xxx'
const AuthenticationToken = token;
const logged = await client.login(AuthenticationToken);
console.log(logged);
const channelId = 'yyy'
const channel = await client.channels.cache.get(channelId) as TextChannel;
console.log(channel)
channel.send('test')
}
I enabled the "MESSAGE CONTENT INTENT" checkbox in my application panel, and for now, the bot is Admin as well. I'm going to redefine the roles later
the call client.channels.cache
return me as undefined
sometimes.
UPDATE
using const c = await client.channels.fetch(channelId) as TextChannel;
I don't have any problem. So what is the different between cache and fetch? I can read cache in Discord documentation.
Upvotes: 0
Views: 566
Reputation: 886
Cache uses data from previous requests, fetch always sends calls for latest data. Using cache is purely for performance reasons (and sometimes so that your requests do not get blocked because you are sending way too many of them). This means that if you want to make your life easier you can always use fetch.
Here is a guide for editing cache behavior: https://discordjs.guide/miscellaneous/cache-customization.html#limiting-caches
Upvotes: 1