如果成员在自定义状态中有邀请,则添加角色 [英] add role if member has invite in custom status

查看:10
本文介绍了如果成员在自定义状态中有邀请,则添加角色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的机器人在其状态中有服务器邀请的情况下为成员提供角色.有谁知道我该怎么做?也许您可以获取成员信息或其他内容?我试着在谷歌上搜索,但我什么也没找到.非常感谢:)

I want my bot to give a member a role if they have the servers invite in their status. Does anyone know how I could do that? maybe you could fetch the members info or something? I tried searching on google but I found nothing. Thank you so much:)

推荐答案

获取自定义状态

正如 Squidleton 所说,您可以使用 <Presence 的 code>.activities 属性来获取用户的自定义状态.此属性是 Activity 的数组,您可以通过查找 .typeCUSTOM_STATUS.

Getting the custom status

As Squiddleton said, you can use the .activities property of Presence to get their a user's custom status. This property is an array of Activitys and you can get the custom status by finding the activity whose .type is CUSTOM_STATUS.

自定义状态的活动如下所示:

The activity of a custom status looks something like this:

Activity {
  name: 'Custom Status',
  type: 'CUSTOM_STATUS',
  url: null,
  details: null,
  state: 'this is the status message',
  applicationID: null,
  timestamps: null,
  party: null,
  assets: null,
  syncID: undefined,
  flags: ActivityFlags { bitfield: 0 },
  emoji: null,
  createdTimestamp: 1625478958735
}

如您所见,状态消息存储在 state 属性中.

As you can see, the status message is stored in the state property.

/**
 * The custom status message of `member`,
 * or `undefined` if the member does not have a custom status.
 * @type {string | undefined}
 */
const customStatus = member.presence.activites
  .find(activity => activity.type === 'CUSTOM_STATUS')
  ?.state

检查状态是否包含邀请

您可以使用 String.prototype.includes 方法来测试一个字符串是否包含另一个字符串:

Checking if the status includes the invite

You can use the String.prototype.includes method to test if a string contains another string:

const inviteLink = 'https://discord.gg/blahblah'

if (customStatus) {
  /**
   * Whether `customStatus` has the invite link `inviteLink`.
   * @type {boolean}
   */
  const hasInviteLink = customStatus.includes(inviteLink)
}

如果您愿意,您可以更进一步,使用 Guild#fetchInvitesinviteCreate 客户端事件.

If you wanted, you could take this a step further and test if the custom status contains any invite from the server using a combination of Guild#fetchInvites and the inviteCreate client event.

现在您需要做的就是添加该成员的角色:

if (hasInviteLink) {
  member.roles.add(theRoleYouWantToAdd)
    // Don't forget to handle errors!
    .catch(console.error)
}

theRoleYouWantToAdd 可以是 Role 或角色的 ID.

theRoleYouWantToAdd can be a Role or the ID of the role.

Discord.js 有一个 presenceUpdate 事件,当成员的状态(例如他们的自定义状态)更新时触发.请注意,您需要启用 GUILD_PRESENCES 意图来接收此事件(请参阅 this answer 了解更多信息信息).

Discord.js has a presenceUpdate event, which fires when a member's presence (such as their custom status) updates. Note that you need to enable the GUILD_PRESENCES intent to receive this event (see this answer for more information).

最终的代码可能如下所示:

The final code might look something like this:

const roleID = // ...
const inviteLink = // ...

client.on('presenceUpdate', (_oldPresence, newPresence) => {
  const member = newPresence.member
  if (member) {
    // Ignore members who already have the role
    if (!member.roles.cache.has(roleID)) {
      const customStatus = newPresence.activites
        .find(activity => activity.type === 'CUSTOM_STATUS')
        ?.state
      if (customStatus) {
        if (customStatus.includes(inviteLink)) {
          member.roles.add(roleID)
            .catch(console.error)
        }
      }
    }
  }
})

这篇关于如果成员在自定义状态中有邀请,则添加角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆