【问题标题】:Adding a user to a role by name using arguments Discord.JS使用参数 Discord.JS 按名称将用户添加到角色
【发布时间】:2021-08-25 01:33:28
【问题描述】:

所以我目前正在尝试找出是否有一种方法可以通过键入变量名称来赋予用户角色。例如,'!role testrole',然后机器人会给他们 testrole 的定义。比如,'if(args[1] === "testrole")...'。 Testrole 可以这样定义,但实际的角色名称可以像 test 一样。您可以添加多个角色,它会添加它。 '!角色你好'等...? Discord.JS

【问题讨论】:

  • 也许你想要eval。这会将一串 JS 代码转换为实际的 JS acide。这样就可以访问字符串中的变量名了。

标签: discord.js


【解决方案1】:

您可以尝试一些简单的方法,使用函数来查找角色名称。您可以执行以下操作:

 const roleName = message.guild.roles.cache.find(r => (r.name === args[1].toString()) || (r.id === args[1].toString().replace(/[^\w\s]/gi, '')));

这将检查提到的角色或给定的角色名称

【讨论】:

    猜你喜欢
    • 2018-09-10
    • 2022-01-18
    • 1970-01-01
    • 1970-01-01
    • 2019-12-11
    • 2021-06-17
    • 2020-10-12
    • 1970-01-01
    相关资源
    最近更新 更多