类型错误:无法读取未定义的属性(读取“GuildText”)

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

我不断收到此错误,

[防崩溃] | [UncaughtExceptionMonitor_Logs] | [开始] : =============== 未捕获的异常监视器:TypeError:无法读取未定义的属性(读取“GuildText”) 异常来源:uncaughtException [防崩溃] | [UncaughtExceptionMonitor_Logs] | [完] : =============== [防崩溃] | [UncaughtException_Logs] | [开始] : =============== 未捕获的异常:TypeError:无法读取未定义的属性(读取“GuildText”) 异常来源:uncaughtException [防崩溃] | [UncaughtException_Logs] | [完] : ===============

const { SlashCommandBuilder } = require('@discordjs/builders');
const { ChannelType } = require('discord.js'); // Ensure you are importing this correctly
const fs = require('fs');
const jailedUsersPath = './src/jailedUsers.json';

// Load jailed users from the file
function loadJailedUsers() {
    return fs.existsSync(jailedUsersPath) ? JSON.parse(fs.readFileSync(jailedUsersPath)) : {};
}

function saveJailedUser(userId, guildId, roles) {
    const jailedUsers = loadJailedUsers();
    jailedUsers[userId] = { guildId, roles };
    fs.writeFileSync(jailedUsersPath, JSON.stringify(jailedUsers, null, 4));
}

module.exports = {
    data: new SlashCommandBuilder()
        .setName('jail')
        .setDescription('Jail a member')
        .addUserOption(option =>
            option.setName('user')
                .setDescription('The user to jail')
                .setRequired(true)),

    async execute(interaction) {
        const member = interaction.options.getMember('user');

        // Ensure the jail system has been set up
        const { jailRole, jailCategory, accessRole } = interaction.client.jailSetup || {};
        if (!jailRole || !jailCategory || !accessRole) {
            return interaction.reply({
                content: 'Jail system is not set up yet. Please run `/jail-setup` first.',
                ephemeral: true,
            });
        }

        try {
            // Save current roles
            const memberRoles = member.roles.cache.map(role => role.id);
            saveJailedUser(member.id, interaction.guild.id, memberRoles);

            // Assign jail role to the member
            await member.roles.set([jailRole]);

            // Create the jail channel
            const jailChannel = await interaction.guild.channels.create({
                name: `${member.user.username}-jail`,
                type: ChannelType.message.GuildText, // Ensure you are using the correct type reference
                parent: jailCategory, // Category ID
                permissionOverwrites: [
                    {
                        id: member.id,
                        allow: ['VIEW_CHANNEL', 'SEND_MESSAGES', 'READ_MESSAGE_HISTORY'],
                    },
                    {
                        id: interaction.guild.id,
                        deny: ['VIEW_CHANNEL'], // Deny everyone else
                    },
                    {
                        id: accessRole,
                        allow: ['VIEW_CHANNEL', 'READ_MESSAGE_HISTORY'],
                        deny: ['SEND_MESSAGES'], // Allow access to see but not send messages
                    },
                ],
            });

            await jailChannel.send(`${member} has been jailed. Welcome to jail!`);

            await interaction.reply({ content: `${member.user.username} has been jailed.`, ephemeral: true });
        } catch (error) {
            console.error('Error jailing user:', error);
            await interaction.reply({ content: 'An error occurred while trying to jail the user.', ephemeral: true });
        }
    },
};

javascript discord.js bots
1个回答
0
投票

发生这种情况是因为 ChannelType 没有

message
属性。不同版本的
GuildText
discordjs
的引用方式有所不同。检查您从 package.json 安装的
discordjs
版本,并使用适当的属性访问 GuildText。

版本 财产 修改线路
v14
ChannelType.GuildText
type: ChannelType.GuildText,
v13
ChannelType.GUILD_TEXT
type: ChannelType.GUILD_TEXT,
v12
ChannelType.text
type: ChannelType.text,

来源:

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