首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >discord.js日志最后10条消息

discord.js日志最后10条消息
EN

Stack Overflow用户
提问于 2022-10-18 04:38:10
回答 1查看 93关注 0票数 -3

我有以下代码:

代码语言:javascript
复制
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")

但是,当我运行代码时,我总是会得到以下错误:

代码语言:javascript
复制
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:

代码语言:javascript
复制
{
"token": "Token hidden",
"general": "1028589311282647041",
"guild": "1028241554277679236"
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-10-19 15:50:44

首先,你不需要这个街区:

代码语言:javascript
复制
const { token } = require('./config.json');
const { general } = require('./config.json');
const { guild } = require('./config.json');

你可以把它写成一行:

代码语言:javascript
复制
const { token, general, guild } = require('./config.json');

您只需定义channel +就可以修复错误,我认为您还需要将GuildMessages意图添加到bot中。

代码语言:javascript
复制
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中已经有一个了,完全没用。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74105766

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档