Reputation:
I have an error with my discord bot, OS: Windows 10 language: node.js, javascript where is the error? I am new baby in programming in javascript main.js
const Discord = require('discord.js');
const client = new Discord.Client();
client.once('ready', () => {
client.channels.get('code-channel').send('Hello here!');
});
client.login('code-for-login');
});
cmd
C:\Users\myname\Desktop\Projekty\Dis. bot>node .
C:\Users\myname\Desktop\Projekty\Dis. bot\main.js:8
client.channels.get('792759306831265825').send('Hello here!');
^
TypeError: client.channels.get is not a function
at Client.<anonymous> (C:\Users\myname\Desktop\Projekty\Dis. bot\main.js:8:27)
at Object.onceWrapper (node:events:482:28)
at Client.emit (node:events:388:22)
at WebSocketManager.triggerClientReady (C:\Users\myname\Desktop\Projekty\Dis. bot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:431:17)
at WebSocketManager.checkShardsReady (C:\Users\myname\Desktop\Projekty\Dis. bot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:415:10)
at WebSocketShard.<anonymous> (C:\Users\myname\Desktop\Projekty\Dis. bot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:197:14)
at WebSocketShard.emit (node:events:376:20)
at WebSocketShard.checkReady (C:\Users\myname\Desktop\Projekty\Dis. bot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:475:12)
at WebSocketShard.onPacket (C:\Users\myname\Desktop\Projekty\Dis. bot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:447:16)
at WebSocketShard.onMessage (C:\Users\myname\Desktop\Projekty\Dis. bot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:301:10)
Upvotes: 0
Views: 96
Reputation: 564
If you are using Discord.js v12, you need to .fetch
a Guild
, Channel
, GuildMember
and Role
to access it or you can use .cache.get
.
v12 introduces the concept of managers, you will no longer be able to directly use collection methods such as Collection#get on data structures like Client#users. You will now have to directly ask for cache on a manager before trying to use collection methods. Any method that is called directly on a manager will call the API, such as GuildMemberManager#fetch and MessageManager#delete.
Accessing from cache: client.guilds.cache.get("ID")
, Guild.members.cache.get("ID")
, Guild.roles.cache.get("ID")
Fetching: await client.guilds.fetch("ID")
, await Guild.members.fetch("ID")
, await Guild.roles.fetch("ID")
What you are looking for: client.channels.cache.get("ID")
. Of course, you can use .fetch
: await client.channels.fetch("ID")
const Discord = require('discord.js');
const client = new Discord.Client();
// You need to make the function 'async' if you are using 'await'
client.once('ready', async () => {
client.channels.cache.get('code-channel').send('Hello here!');
// You can use the both
(await client.channels.fetch('code-channel')).send('Hello here!');
// '.fetch' returns a Promise, so you need to use 'await'
});
client.login('code-for-login');
A documentation that can help you: https://discordjs.guide/additional-info/changes-in-v12.html
Upvotes: 1