TypeError:无法读取未定义的属性(读取' haspermission')

发布于 2025-02-01 08:25:58 字数 594 浏览 5 评论 0 原文

我又回来了我的票务功能。这次,我正在处理一个近距离命令。无论我尝试什么,我总是会遇到相同的错误:

TypeError: message.member.hasPermission is not a function

任何可以查看代码的人?

    const { MessageEmbed, Collection, Permissions } = require ('discord.js');

module.exports = {
    name: 'close',
    description: "closes the ticket",
    execute(message, args, client){
        if(!message.member.hasPermission("MANAGE_CHANNELS")) return message.channel.send("Only a moderator can end a ticket!")

        if(message.member.hasPermission("MANAGE_CHANNELS")) message.channel.delete()
    }
}

I am back again with again my ticket function. This time I am working on a close command. Whatever I try I always come back at the same errors:

TypeError: message.member.hasPermission is not a function

Anyone who can look at the code?

    const { MessageEmbed, Collection, Permissions } = require ('discord.js');

module.exports = {
    name: 'close',
    description: "closes the ticket",
    execute(message, args, client){
        if(!message.member.hasPermission("MANAGE_CHANNELS")) return message.channel.send("Only a moderator can end a ticket!")

        if(message.member.hasPermission("MANAGE_CHANNELS")) message.channel.delete()
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

最冷一天 2025-02-08 08:25:58

首先,您应该尝试阅读 docs 属性guildmember实际上具有什么,只需快速查看它,您应该能够找到 .permissions ,然后从中,您将获得此 .has 函数。因此,您的最终代码应该是

message.member.permissions.has(Permissions.FLAGS.MANAGE_MESSAGES)

Firstly, you should try reading the docs and see what properties GuildMember actually has, just a quick look through it, you should be able to find a .permissions, following from that, you'll get to this doc on the Permissions class. From there, you can see you can use the .has function. So your final code should be

message.member.permissions.has(Permissions.FLAGS.MANAGE_MESSAGES)
简单爱 2025-02-08 08:25:58

此代码将检查成员是否已许可 Manage_Channels ,并且由于 .permissions.has()返回布尔值,如果用户do no no no no no no no n dre boolean拥有这样的权限,它将返回一条消息,即他们无法使用命令,否则它将删除频道...

if(!message.member.permissions.has(Permissions.FLAGS.MANAGE_CHANNELS)) { 
  return message.channel.send("Only a moderator can end a ticket!")
} else {
  return message.channel.delete()
}

this code will check if the member has the permission to MANAGE_CHANNELS and since .permissions.has() returns a boolean, it will either return true or false, if the user doesn't have such permission it will return a message that they cannot use the command, else if it will delete the channel...

if(!message.member.permissions.has(Permissions.FLAGS.MANAGE_CHANNELS)) { 
  return message.channel.send("Only a moderator can end a ticket!")
} else {
  return message.channel.delete()
}
单挑你×的.吻 2025-02-08 08:25:58

按照您对我的评论的回答,看来您只是将论点放在错误的顺序中。

您的功能被定义为,

execute(client, message, args)

因此您应该按照相同的顺序将其称为参数:

try {
  command.execute(client, message, args); // `client` is first, not last
} catch (error) {
  console.error(error);
  message.channel.send('There was an error trying to execute that command!');
} 

按照您对此答案的评论,它解决了初始问题。现在 message.member 具有一个值!

但是现在 member.haspermission 似乎不是一个函数。确实不是:当您查看 a>,公会上没有 haspermission 方法。

因此,您必须弄清楚确切需要什么并查看文档以了解如何做。

Following your answer to my comment, it seems that you just put the arguments in the wrong order.

Your function is defined as

execute(client, message, args)

So you should call it with the arguments in the same order:

try {
  command.execute(client, message, args); // `client` is first, not last
} catch (error) {
  console.error(error);
  message.channel.send('There was an error trying to execute that command!');
} 

Following your comment on this answer, it fixed the initial issue. Now message.member has a value!

But now member.hasPermission does not seem to be a function. And indeed it is not: when you look at the documentation, there is no hasPermission method on a GuildMember.

So you have to figure out what exactly you need and look at the documentation to understand how to do it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文