【问题标题】:How to update user permission discord js如何更新用户权限discord js
【发布时间】:2020-07-23 10:37:03
【问题描述】:

所以,我想更新一些用户的权限。 首先,我尝试创建一个文本通道并覆盖用户权限。然后我做了一个for循环来更新权限,但我不知道怎么做。 我尝试更新权限的事情:

channel.overwritePermissions({
    permissionOverwrites: [
        {
            id: guild.id,
            deny: ['VIEW_CHANNEL'],
        },
        {
            id: playerData[i],
            allow: ['VIEW_CHANNEL'],
        },
    ],
});
//it said channel not defined?
message.guild.channels.cache.find('????werewolves').overwritePermissions({
        permissionOverwrites: [
            {
            id: playerData[i],
            allow:['VIEW_CHANNEL'],
            }
        ]
    })
//it said : fn is not a function

我已经看过this 解决方案并阅读了documentation,但说明不清楚。

PS:权限更新一定是循环的,因为获得权限的用户数一直在变化。

【问题讨论】:

  • 您使用的是什么版本的 discord.js?你运行的是什么版本的节点?
  • 我正在使用 discord.js 版本 12.1.1 和 node.js 版本 12.16.1

标签: node.js discord.js


【解决方案1】:

关于第一个代码块

channel 未定义,因为它未定义。你不能在不定义变量的情况下使用变量,这就是 JavaScript 的工作原理。要解决它,您必须使用例如 const channel = ... 来定义它,或者从您拥有的其他变量中访问它,就像您尝试在第二个代码块中使用 find - 您从 message 访问它一样你最有可能在消息事件中。

关于第二个代码块

这不是使用find 的正确方法,无论是旧的 - 在您的版本中删除 - 方式,也不是新的方式。旧方法是执行find(property, value),您不会为其提供值('?werewolves' 将被视为您尝试搜索的属性)。您必须使用的新方法,通过要求将函数传递给它来提供更大的灵活性,就像在 example 中的方法一样。由于您传递的是字符串而不是函数,因此内部代码会引发错误fn is not a function。

对于上面的示例,使用该查找的正确方法是

message.guild.channels.cache.find(channel => channel.name === '?werewolves');

另外,请注意,理想情况下,您不应尝试直接调用任何方法,因为如果找不到具有该名称的通道,您的代码将引发错误。下面的片段应该避免这种可能性。

const channel = message.guild.channels.cache.find(channel => channel.name === '?werewolves');
if (channel) channel.overwritePermissions(...)

【讨论】:

    猜你喜欢
    • 2021-02-04
    • 2016-06-26
    • 2021-12-18
    • 2010-10-27
    • 2012-04-12
    • 2013-05-07
    • 2013-12-04
    • 1970-01-01
    • 2022-01-01
    相关资源
    最近更新 更多