代码之家  ›  专栏  ›  技术社区  ›  wixter-dev-acc

错误:未捕获的类型错误类型错误:无法读取未定义的属性(读取“FLAGS”)Discord bot Discord.js V14

  •  0
  • wixter-dev-acc  · 技术社区  · 2 周前

    我只是想让机器人如果我与按钮互动,它会在“ticket”类别上创建一个频道。如果没有ticket类别,我会验证是否有我需要的id的类别,如果没有,机器人会创建一个 顺便说一句,这是我的代码:

    const { Client, IntentsBitField, MessageActionRow, MessageButton, Permissions } = require("discord.js");
    
    const ChannelIDs = [
        "1194626191504703609", "1220403612157608017", "1214602084507459696",
        "1205122006975258624", "1194626176887566400", "1233790139671515196",
        "1233786987014651965", "1204022782149664788", "1259563299712929914",
        "1194626159607021578", "1194626093165064213", "1250802176444596265"
    ];
    
    let ticketsCategoryID = null; // Variable to hold the category ID
    let i = 0;
    const prefix = "&";
    
    const client = new Client({
        intents: [
            IntentsBitField.Flags.Guilds,
            IntentsBitField.Flags.GuildMembers,
            IntentsBitField.Flags.GuildMessages,
            IntentsBitField.Flags.MessageContent,
            IntentsBitField.Flags.DirectMessages,
    
        ]
    });
    
    client.once("ready", async() => {
        console.log(`${client.user.tag} is working!`);
    
        const guildId = "guild"; 
        const guild = client.guilds.cache.get(guildId);
    
        if (!guild) {
            console.error(`Guild with ID ${guildId} not found.`);
            return;
        }
    
        let category = guild.channels.cache.find(c => c.type === "GUILD_CATEGORY" && c.name === "tickets");
    
        if (!category) {
            category = await guild.channels.create("tickets", {
                type: "GUILD_CATEGORY",
                permissionOverwrites: [{
                    id: guild.roles.everyone.id,
                    deny: [Permissions.FLAGS.VIEW_CHANNEL]
                }]
            });
    
            ticketsCategoryID = category.id;
            console.log(`Tickets category ID: ${ticketsCategoryID}`);
        } else {
            ticketsCategoryID = category.id;
            console.log(`Found existing tickets category with ID: ${ticketsCategoryID}`);
        }
    });
    
    client.on("messageCreate", async(message) => {
        if (message.author.bot || !message.guild) return;
    
        if (message.content === prefix + 'setup' && ChannelIDs.includes(message.channel.id)) {
            await message.delete();
    
            const button = new MessageActionRow()
                .addComponents(
                    new MessageButton()
                    .setCustomId('Open_Ticket')
                    .setLabel('Open Ticket 🎟️')
                    .setStyle('SUCCESS')
                );
    
            const embed = {
                color: 'GREEN',
                description: 'Click on the button to open a new ticket'
            };
    
            await message.channel.send({ embeds: [embed], components: [button] });
        }
    });
    
    client.on("interactionCreate", async(interaction) => {
        if (!interaction.isButton() || interaction.customId !== 'Open_Ticket') return;
    
        if (!ticketsCategoryID) {
            return interaction.reply({
                content: "Tickets category is not set up correctly. Please contact an admin.",
                ephemeral: true
            });
        }
    
        const guild = interaction.guild;
        const ticketCategory = guild.channels.cache.get(ticketsCategoryID);
    
        if (!ticketCategory) {
            return interaction.reply({
                content: "Tickets category not found. Please contact an admin.",
                ephemeral: true
            });
        }
    
        i += 1;
        const channelName = `channel-${i}`;
    
        await guild.channels.create(channelName, {
            type: "GUILD_TEXT",
            parent: ticketCategory.id,
            permissionOverwrites: [{
                    id: guild.roles.everyone,
                    deny: [Permissions.FLAGS.VIEW_CHANNEL]
                },
                {
                    id: interaction.user.id,
                    allow: [Permissions.FLAGS.VIEW_CHANNEL]
                }
            ]
        });
    
        await interaction.reply({ content: `Created channel: ${channelName}`, ephemeral: true });
    });
    
    const token = "token";
    client.login(token);
    

    这是整个错误:

    未捕获的TypeError TypeError:无法读取未定义的属性(读取“FLAGS”) at(c:\Users\Alpha\Desktop\devloment=)\bot jell\src\index.js:43:36) onceWrapper(事件:634:26) 发射时(事件:519:28) 在triggerClientReady(c:\Users\Alpha\Desktop\devloment=)\bot jell\node_modules\discord.js\src\client\websocket\WebocketManager.js:38:17) 在checkShardsReady(c:\Users\Alpha\Desktop\devloment=)\bot jell\node_modules\discord.js\src\client\websocket\WebSocketManager.js:371:10) at(c:\Users\Alpha\Desktop\devloment=)\bot jell\node_modules\discord.js\src\client\websocket\WebocketManager.js:201:16) 发射时(事件:519:28) 在checkReady(c:\Users\Alpha\Desktop\devloment=)\bot jell\node_modules\discord.js\src\client\websocket\WebocketHard.js:18:12) 在gotGuild(c:\Users\Alpha\Desktop\devloment=)\bot jell\node_modules\discord.js\src\client\websocket\WebocketHard.js:155:10) at(c:\Users\Alpha\Desktop\devloment=)\bot jell\node_modules\discord.js\src\client\websocket\WebocketManager.js:241:15) at emit(c:\Users\Alpha\Desktop\devloment=)\bot jelly\node_modules@vladfrangu\async_event_emitter\dist\index.cjs:285:31) 位于(c:\Users\Alpha\Desktop\devloment=)\bot jelly\node_modules@discordjs\ws\dist\index.js:1190:51) at emit(c:\Users\Alpha\Desktop\devloment=)\bot jelly\node_modules@vladfrangu\async_event_emitter\dist\index.cjs:285:31) 在onMessage(c:\Users\Alpha\Desktop\devloment=)\bot jelly\node_modules@discordjs\ws\dist\index.js:1005:14) 在processTicksAndRejections(内部/进程/任务队列:95:5) ---TickObject--- 在初始(internal/inspector_async_hook:25:19) at emitInitNative(内部/async_hooks:202:43) at emitInitScript(内部/async_hooks:505:3) 在nextTick(内部/进程/任务队列:132:5) 在(事件:380:17) 在processTicksAndRejections(内部/进程/任务队列:95:5) ---承诺。那么--- 在addCatch(事件:377:12) 发射时(事件:525:7) 在triggerClientReady(c:\Users\Alpha\Desktop\devloment=)\bot jell\node_modules\discord.js\src\client\websocket\WebocketManager.js:38:17) 在checkShardsReady(c:\Users\Alpha\Desktop\devloment=)\bot jell\node_modules\discord.js\src\client\websocket\WebSocketManager.js:371:10) at(c:\Users\Alpha\Desktop\devloment=)\bot jell\node_modules\discord.js\src\client\websocket\WebocketManager.js:201:16) 发射时(事件:519:28) 在checkReady(c:\Users\Alpha\Desktop\devloment=)\bot jell\node_modules\discord.js\src\client\websocket\WebocketHard.js:18:12) 在gotGuild(c:\Users\Alpha\Desktop\devloment=)\bot jell\node_modules\discord.js\src\client\websocket\WebocketHard.js:155:10) at(c:\Users\Alpha\Desktop\devloment=)\bot jell\node_modules\discord.js\src\client\websocket\WebocketManager.js:241:15) at emit(c:\Users\Alpha\Desktop\devloment=)\bot jelly\node_modules@vladfrangu\async_event_emitter\dist\index.cjs:285:31) 位于(c:\Users\Alpha\Desktop\devloment=)\bot jelly\node_modules@discordjs\ws\dist\index.js:1190:51) at emit(c:\Users\Alpha\Desktop\devloment=)\bot jelly\node_modules@vladfrangu\async_event_emitter\dist\index.cjs:285:31) 在onMessage(c:\Users\Alpha\Desktop\devloment=)\bot jelly\node_modules@discordjs\ws\dist\index.js:1005:14) 在processTicksAndRejections(内部/进程/任务队列:95:5) 等候 在processTicksAndRejections(内部/进程/任务队列:95:5) 等候 在processTicksAndRejections(内部/进程/任务队列:95:5) 等候 在connection.onmessage(c:\Users\Alpha\Desktop\devloment=)\bot jelly\node_modules@discordjs\ws\dist\index.js:680:17) at callListener(c:\Users\Alpha\Desktop\devloment=)\bot jell\node_modules\ws\lib\event target.js:290:14) 在onMessage(c:\Users\Alpha\Desktop\devloment=)\bot jell\node_modules\ws\lib\event target.js:209:9) 发射时(事件:519:28) at receiverOnMessage(c:\Users\Alpha\Desktop\devloment=)\bot jell\node_modules\ws\lib\websocket.js:1220:20) 发射时(事件:519:28) at dataMessage(c:\Users\Alpha\Desktop\devloment=)\bot jell\node_modules\ws\lib\receiver.js:596:14) 在getData(c:\Users\Alpha\Desktop\devloment=)\bot jell\node_modules\ws\lib\receiver.js:496:10) at startLoop(c:\Users\Alpha\Desktop\devloment=)\bot jell\node_modules\ws\lib\receiver.js:167:16) at _write(c:\Users\Alpha\Desktop\devloment=)\bot jell\node_modules\ws\lib\receiver.js:94:10) 在writeOrBuffer(内部/流/可写:564:12) at _write(内部/流/可写:493:10) 在Writable.write(internal/streams/Writable:502:10) at socketOnData(c:\Users\Alpha\Desktop\devloment=)\bot jell\node_modules\ws\lib\websocket.js:1355:35) 发射时(事件:519:28) 在addChunk(内部/流/可读:559:12) 在readableAddChunkPushByteMode(内部/流/可读:510:3) 在Readable.push(内部/流/可读:390:5) 在onStreamRead(内部/stream_base_commons:191:23) 在回调Trampoline(内部/async_hooks:130:17) ---TLSWRAP--- 在初始(internal/inspector_async_hook:25:19) at emitInitNative(内部/async_hooks:202:43) 在TLSSocket_wrapHandle(_tls_wrap:706:24) 在TLSSocket(_tls_wrap:577:18) 连接时(_tls_wrap:1765:19) 在tlsConnect(c:\Users\Alpha\Desktop\devloment=)\bot jell\node_modules\ws\lib\websocket.js:1071:14) 在客户端请求(_http_client:358:32) 应要求(https:379:10) 在initAsClient(c:\Users\Alpha\Desktop\devloment=)\bot jell\node_modules\ws\lib\websocket.js:868:28) 在WebSocket(c:\Users\Alpha\Desktop\devloment=)\bot jell\node_modules\ws\lib\WebSocket.js:88:7) 在internalConnect(c:\Users\Alpha\Desktop\devloment=)\bot jelly\node_modules@discordjs\ws\dist\index.js:675:24) 在processTicksAndRejections(内部/进程/任务队列:95:5) 等候 在connect(c:\Users\Alpha\Desktop\devloment=)\bot jelly\node_modules@discordjs\ws\dist\index.js:641:15) 在connect(c:\Users\Alpha\Desktop\devloment=)\bot jelly\node_modules@discordjs\ws\dist\index.js:1212:27) 在connect(c:\Users\Alpha\Desktop\devloment=)\bot jelly\node_modules@discordjs\ws\dist\index.js:1462:25) 在processTicksAndRejections(内部/进程/任务队列:95:5) 等候 在processTicksAndRejections(内部/进程/任务队列:95:5) 等候 在processTicksAndRejections(内部/进程/任务队列:95:5) 等候 在processTicksAndRejections(内部/进程/任务队列:95:5) 等候 at connect(c:\Users\Alpha\Desktop\devloment=)\bot jell\node_modules\discord.js\src\client\websocket\WebocketManager.js:207:20) 在processTicksAndRejections(内部/进程/任务队列:95:5) 等候 在processTicksAndRejections(内部/进程/任务队列:95:5) 等候 在processTicksAndRejections(内部/进程/任务队列:95:5) 等候 登录时(c:\Users\Alpha\Desktop\devloment=)\bot jell\node_modules\discord.js\src\client\client.js:228:21) at(c:\Users\Alpha\Desktop\devloment=)\bot jell\src\index.js:119:8) 在模块中_compile(internal/modules/cjs/loader:1358:14) 在模块中_扩展。。js(internal/modules/cjs/loader:1416:10) 在Module.load(internal/modules/cjs/loader:1208:32) 在模块中_加载(internal/modules/cjs/loader:1024:12) 在executeUserEntryPoint(内部/modules/run_main:174:12) 在(internal/main/run_main_module:28:49)

    尝试将FLAGS命名为FLAGS不起作用

    2 回复  |  直到 2 周前
        1
  •  0
  •   Jeremy Fiel    2 周前

    当您尝试从这两行读取Permissions对象时,它没有FLAGS密钥

    deny: [Permissions.FLAGS.VIEW_CHANNEL]

    allow: [Permissions.FLAGS.VIEW_CHANNEL]

    试试这个

    deny: [PermissionsBitField.Flags.ViewChannel]

    allow: [PermissionsBitField.Flags.ViewChannel]

        2
  •  0
  •   Fuxlor    2 周前

    您可以使用以下方式管理权限标志:

    const { Client, IntentsBitField, MessageActionRow, MessageButton, PermissionFlagsBits} = require("discord.js");
    
    category = await guild.channels.create("tickets", {
       type: "GUILD_CATEGORY",
       permissionOverwrites: [{
          id: guild.roles.everyone.id,
          deny: [PermissionFlagsBits.ViewChannel]
       }]
    });
    

    我希望这对你有帮助!