I have the following code:
const { Client, GatewayIntentBits } = require('discord.js');
const { token } = require('./config.json');
const { general } = require('./config.json');
const { guild } = require('./config.json');
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
client.once('ready', () => {
console.log("Ready!");
channel.messages.fetch({ limit: 10 })
.then(messages => console.log(`Received ${messages.size} messages`))
.catch(console.error);
});
client.login(token);
console.log("Online")
But when I run the code I always get this error:
ReferenceError: channel is not defined
at Client.<anonymous> (/workspaces/proJM-bridge/index.js:14:3)
at Object.onceWrapper (node:events:628:26)
at Client.emit (node:events:513:28)
at WebSocketManager.triggerClientReady (/workspaces/proJM-bridge/node_modules/discord.js/src/client/websocket/WebSocketManager.js:385:17)
at WebSocketManager.checkShardsReady (/workspaces/proJM-bridge/node_modules/discord.js/src/client/websocket/WebSocketManager.js:368:10)
at WebSocketShard.<anonymous> (/workspaces/proJM-bridge/node_modules/discord.js/src/client/websocket/WebSocketManager.js:194:14)
at WebSocketShard.emit (node:events:513:28)
at WebSocketShard.checkReady (/workspaces/proJM-bridge/node_modules/discord.js/src/client/websocket/WebSocketShard.js:511:12)
at WebSocketShard.onPacket (/workspaces/proJM-bridge/node_modules/discord.js/src/client/websocket/WebSocketShard.js:483:16)
at WebSocketShard.onMessage (/workspaces/proJM-bridge/node_modules/discord.js/src/client/websocket/WebSocketShard.js:320:10)
I can't find anywhere what is wrong and I have tried many code and still get the same error. Does someone know what I'm doing wrong?
config.json:
{
"token": "Token hidden",
"general": "1028589311282647041",
"guild": "1028241554277679236"
}
First of all you don't need this block:
const { token } = require('./config.json');
const { general } = require('./config.json');
const { guild } = require('./config.json');
You can write it in one line:
const { token, general, guild } = require('./config.json');
And you can fix the error with just defining channel
+ I think you also need to add the GuildMessages
intent to your bot.
const { Client, GatewayIntentBits, TextChannel } = require('discord.js');
const { token } = require('./config.json');
const { general } = require('./config.json');
const { guild } = require('./config.json');
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent
]
});
client.once('ready', () => {
console.log("Ready!");
/**
* @type {TextChannel}
*/
const channel = client.guilds.cache.get(guild).channels.cache.get(general);
if(!channel) return console.log('Invalid guildId or channelId');
channel.messages.fetch({ limit: 10 })
.then(messages => {
messages.forEach((m) => {
const content = m.content ? m.content : `<No Message Content>`;
const embeds = m.embeds ? `<${m.embeds.length} Embed(s)>` : `<No embeds>`;
const attachments = m.attachments ? `<${m.attachments.size} Attachment(s)>` : `<No Attachments>`;
console.log(`---------------------------\nAuthor: ${m.author.tag}\nContent: ${content}\nEmbeds: ${embeds}\nAttachments: ${attachments}\n---------------------------`);
});
})
.catch(console.error);
});
client.login(token);
I also removed the console.log
in the last line bc it's completely useless as you already have one in the ready event.