锁定命令Discord.js

时间:2020-10-16 08:40:19

标签: javascript node.js discord discord.js

我最近对discord.js发出了锁定命令。但是,每当我运行命令时,都会出现错误。这是代码:

module.exports = {
    name: "lock",
    description: "Lock",

    async run(client, message, args) {
        if (!message.member.hasPermission("KICK_MEMBERS")) return message.channel.send('You can\'t use that!')
        function lock(message) {
            let channel = message.channel;
            const Guild = client.guilds.cache.get("751424392420130907");

            if (!Guild) return console.error("Couldn't find the guild.");

            const Role = Guild.roles.cache.find(role => role.name == "Verified");
            channel.overwritePermissions(
                Role, {
                'SEND_MESSAGES': false
            },
                'Competitive has Ended'
            )
        }
        lock(message)
        message.channel.send('Channel Locked')
    }
}

正如我之前提到的,每当我运行此命令时,都会出现以下错误:

(node:1354) UnhandledPromiseRejectionWarning: TypeError [INVALID_TYPE]: Supplied overwrites is not an Array or Collection of Permission Overwrites.
    at TextChannel.overwritePermissions (/home/runner/SweatyBeautifulHelpfulWorker/node_modules/discord.js/src/structures/GuildChannel.js:208:9)
    at lock (/home/runner/SweatyBeautifulHelpfulWorker/commands/lock.js:14:11)
    at Object.run (/home/runner/SweatyBeautifulHelpfulWorker/commands/lock.js:21:1)
    at Client.<anonymous> (/home/runner/SweatyBeautifulHelpfulWorker/index.js:77:42)
    at Client.emit (events.js:327:22)
    at Client.EventEmitter.emit (domain.js:483:12)
    at MessageCreateAction.handle (/home/runner/SweatyBeautifulHelpfulWorker/node_modules/discord.js/src/client/actions/MessageCreate.js:31:14)
    at Object.module.exports [as MESSAGE_CREATE] (/home/runner/SweatyBeautifulHelpfulWorker/node_modules/discord.js/src/client/websocket/handlers/MESSAGE_CREATE.js:4:32)
    at WebSocketManager.handlePacket (/home/runner/SweatyBeautifulHelpfulWorker/node_modules/discord.js/src/client/websocket/WebSocketManager.js:384:31)
    at WebSocketShard.onPacket (/home/runner/SweatyBeautifulHelpfulWorker/node_modules/discord.js/src/client/websocket/WebSocketShard.js:444:22)
(node:1354) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:1354) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
[WS => Shard 0] [HeartbeatTimer] Sending a heartbeat.
[WS => Shard 0] Heartbeat acknowledged, latency of 44ms.
(node:1354) UnhandledPromiseRejectionWarning: TypeError [INVALID_TYPE]: Supplied overwrites is not an Array or Collection of Permission Overwrites.
    at TextChannel.overwritePermissions (/home/runner/SweatyBeautifulHelpfulWorker/node_modules/discord.js/src/structures/GuildChannel.js:208:9)
    at lock (/home/runner/SweatyBeautifulHelpfulWorker/commands/lock.js:14:11)
    at Object.run (/home/runner/SweatyBeautifulHelpfulWorker/commands/lock.js:21:1)
    at Client.<anonymous> (/home/runner/SweatyBeautifulHelpfulWorker/index.js:77:42)
    at Client.emit (events.js:327:22)
    at Client.EventEmitter.emit (domain.js:483:12)
    at MessageCreateAction.handle (/home/runner/SweatyBeautifulHelpfulWorker/node_modules/discord.js/src/client/actions/MessageCreate.js:31:14)
    at Object.module.exports [as MESSAGE_CREATE] (/home/runner/SweatyBeautifulHelpfulWorker/node_modules/discord.js/src/client/websocket/handlers/MESSAGE_CREATE.js:4:32)
    at WebSocketManager.handlePacket (/home/runner/SweatyBeautifulHelpfulWorker/node_modules/discord.js/src/client/websocket/WebSocketManager.js:384:31)
    at WebSocketShard.onPacket (/home/runner/SweatyBeautifulHelpfulWorker/node_modules/discord.js/src/client/websocket/WebSocketShard.js:444:22)
(node:1354) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)

您能帮我解决这个问题吗?预先感谢!

5 个答案:

答案 0 :(得分:2)

你应该这样做,你的代码看起来很长:

 if (!message.member.roles.cache.some(role => role.name === 'Moderator')) return;
 message.channel.updateOverwrite(message.channel.guild.roles.everyone, { SEND_MESSAGES: false })
 message.channel.send(`Successfully locked **${message.channel.name}**`)

从您的角色中替换 message.channel.guild.roles.everyone

答案 1 :(得分:0)

您只需要调用以下行即可删除当前频道的发送权限:

const Role = guild.roles.find("name", "Verified ");

message.channel.overwritePermissions(role,{ 'SEND_MESSAGES': false })

如果您想发出一个解锁频道命令,只需在命令下面添加它:

const Role = guild.roles.find("name", "Verified ");

message.channel.overwritePermissions(role,{ 'SEND_MESSAGES': true})

答案 2 :(得分:0)

您还应该使用 updateOverwrite 而不是 overwritePermissions

示例:

module.exports = {
    name: "lock",
    description: "Lock",

    run(client, message, args) {
       const targetChannel = message.mentions.channels.first() || message.channel;

        // Guild ID is the same as the everyone role ID
        const everyoneID = message.guild.id;

        targetChannel.updateOverwrite(everyoneID, {
            SEND_MESSAGES: false,
        });

        targetChannel.send(`**${targetChannel.name}** has been locked :lock:`);
    }
}

也不需要它是异步函数,因为您没有在代码中使用 await。

答案 3 :(得分:0)

这不是你更新权限的方式,而是这个:

channel.overwritePermissions(
            Role, {
            'SEND_MESSAGES': false
        },
            'Competitive has Ended'
        )

使用这个:

channel.overwritePermissions([
        {
        id: roleId,
        deny: ['SEND_MESSAGES']
        }]
        ,'Competitive has Ended'
    )

来源discord.js documentation#overwritePermissions

答案 4 :(得分:0)

以下代码可能对您有所帮助

channel.overwritePermissions(
    [
        {
            id: roleId,
            deny: [
                'SEND_MESSAGES'
            ]
        }
    ]
        , 'Mark my question'
)```