简体   繁体   English

如何获取特定 Discord 角色的名称?

[英]How can I get the name of a specific Discord role?

I am trying to make a bot for my server with many international users.我正在尝试为我的服务器制作一个有许多国际用户的机器人。 One issue we always run into is coordinating times because were all around the world.我们经常遇到的一个问题是协调时间,因为它们遍布世界各地。 I wanted to make this bot be able to tell you what time it is for a specific user.我想让这个机器人能够告诉你特定用户的时间。 I already have all of my users have a role with the format "UTC[-10 through +14]" that says what time zone they are in. I already have the function that when you give it a UTC offset, it will give you the time there.我已经让我的所有用户都有一个格式为“UTC [-10 到 +14]”的角色,它说明了他们所在的时区。我已经有了 function,当你给它一个 UTC 偏移量时,它会给你那里的时间。 My question is, how can I get the UTC offset of someone given their name.我的问题是,我怎样才能得到给定他们名字的人的 UTC 偏移量。 For example, I have a user @User1#0000 with the role UTC-05 .例如,我有一个角色为UTC-05的用户@User1#0000 They also might have other roles (not timezone roles).他们也可能有其他角色(不是时区角色)。 How can I get that -05 out of someones roles just given their name?我怎样才能从某人的角色中获得-05的名字?

Firstly, to get a member's roles, you just need to use the roles property member , now to get just the timezone role, you can use the .find function that collection has and look for "UTC" at the start of the role name,首先,要获取成员的角色,您只需要使用roles属性member ,现在要获取时区角色,您可以使用集合具有的.find function 并在角色名称的开头查找“UTC”,

let utcRole = member.roles.find(role => role.name.startsWith('UTC'))

Now, the harder part is parsing the role name to get just the offset from it, we can do that using a regular expression and the match function, we want to isolate the +14, the regex for isolating it would be [\+\-][0-9]*[^\[\]] , so adding that to our sudo code,现在,更难的部分是解析角色名称以从中获取偏移量,我们可以使用正则表达式和匹配 function 来做到这一点,我们要隔离 +14,用于隔离它的正则表达式将是[\+\-][0-9]*[^\[\]] ,因此将其添加到我们的 sudo 代码中,

let utcRole = member.roles.find(role => role.name.startsWith('UTC'))

let offset = utcRole.name.match(/[\+\-][0-9]*[^\[\]]/)[0]

And there we go, you will have the offset that you wanted, now one important thing to remember is that you have the offset as a string, and by what you said you will use it as a number, to convert strings in to numbers in javascript we use parseInt() , so to convert it to a number, go,您将获得所需的偏移量,现在要记住的重要一件事是您将偏移量作为字符串,按照您所说的,您将使用它作为数字,将字符串转换为数字javascript 我们使用parseInt() ,因此要将其转换为数字,

let number = parseInt(offset)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM