从discord.js导入客户端对象不起作用

问题描述 投票:0回答:1

所以我为不和谐机器人制作了一个仪表板,并试图获取公会,但它给出了错误:

Uncaught TypeError TypeError: Cannot read properties of undefined (reading 'cache')
    at getManageableGuilds (e:\The House Party 2\Panzer Bot\dashboard\middleware.js:34:35)
    at module.exports.updateGuilds (e:\The House Party 2\Panzer Bot\dashboard\middleware.js:9:33)
    at processTicksAndRejections (internal/process/task_queues:95:5)

我正在尝试使用

client.guilds.cache.get(id)
来获取公会,但这似乎不起作用,我也尝试使用
fetch
但这也不起作用。我已经尝试使用 ChatGPT 修复它,但没有任何结果。我很确定我正确定义了客户端对象:

const client = new Discord.Client({
    intents: [
        GatewayIntentBits.Guilds,
        GatewayIntentBits.GuildVoiceStates
    ]
});
client.login(TOKEN);
module.exports = client;

这就是我的 middleware.js 的样子,

updateGuilds
部分工作正常,但
getManageableGuilds
函数是我遇到错误的地方,
const guild = await client.guilds.cache.get(id)

const authClient = require('./auth-client')
const client = require('../main')

module.exports.updateGuilds = async (req, res, next) => {
    try {
        const key = res.cookies.get('key')
        if(key) {
            const authGuilds = await authClient.getGuilds(key)
            res.locals.guilds = getManageableGuilds(authGuilds)
        } 
    } finally {
        next()
    }
}


module.exports.updateUser = async (req, res, next) => {
    try {
        const key = res.cookies.get('key')
        if(key) res.locals.user = await authClient.getUser(key)
    } finally {
        next()
    }
}

module.exports.validateUser = async (req, res, next) => {
    res.locals.user ? next() : res.render('errors/401')
}

async function getManageableGuilds(authGuilds) {
    const guilds = [];
    for(const id of authGuilds.keys()){
        const isManager = authGuilds
            .get(id).permissions
            .includes('MANAGE_GUILD');
        const guild = await client.guilds.cache.get(id)
        if (!guild || !isManager) continue;

        guilds.push(guild);
    }
    return guilds;
}

我最终通过而不是导入客户端来修复它,我只是决定创建一个新的客户端对象,并执行

client.login(TOKEN)
。但是必须在每个需要使用来自discordjs 的客户端对象的文件中执行此操作感觉非常未优化/有缺陷。或者如果不行的话这样做可以吗?我还能如何解决这个问题?

javascript node.js oauth-2.0 discord discord.js
1个回答
0
投票

据我所知,您正在使用

Client
中的
discord.js
类来获取公会数据?您不需要这样做,特别是因为每个
client.login(TOKEN)
调用都会启动另一个 Discord 客户端,该客户端与 Discord 保持持久的 WebSocket 连接并持续接收来自 Discord 的公会事件,这可能会减慢您的 Express 应用程序的速度(取决于您的机器人有多大)是,当然)。

不要使用

discord.js
,请考虑使用它们的子包
@discordjs/core
@discordjs/rest
。您已经安装了这些,因为
discord.js
取决于它们。以下是获取公会数据的方法:

// Assuming `TOKEN` and `id` are defined elsewhere

import { REST } from '@discordjs/rest';
import { GuildsAPI } from '@discordjs/core';

const rest = new REST().setToken(TOKEN);
const guilds = new GuildsAPI(rest);

const guild = await guilds.get(id);

如果您在项目中使用 CommonJS 而不是 ESM 模块,请将上面的

import
语句替换为
require
语法:

const { REST } = require('@discordjs/rest');
const { GuildsAPI } = require('@discordjs/core');

请注意,您从

@discordjs/core
获得的对象类型与
discord.js
不同。请阅读有关 @discordjs/core
文档以了解更多信息。 https://discord-api-types.dev 也可能有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.