我有以下代码:
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")但是,当我运行代码时,我总是会得到以下错误:
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)我在任何地方都找不到出问题的地方,我已经尝试了许多代码,但仍然得到了相同的错误。有人知道我做错了什么吗?
config.json:
{
"token": "Token hidden",
"general": "1028589311282647041",
"guild": "1028241554277679236"
}发布于 2022-10-19 15:50:44
首先,你不需要这个街区:
const { token } = require('./config.json');
const { general } = require('./config.json');
const { guild } = require('./config.json');你可以把它写成一行:
const { token, general, guild } = require('./config.json');您只需定义channel +就可以修复错误,我认为您还需要将GuildMessages意图添加到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);我还删除了bc最后一行中的console.log,因为在ready中已经有一个了,完全没用。
https://stackoverflow.com/questions/74105766
复制相似问题