简体   繁体   English

Discord Bot - 如何检查 DM 中的角色

[英]Discord Bot - How to check roles in a DM

I'm using Discord v13 and I'm following the docs here in order to verify if the user that sent a DM message to the bot has the Admin role or not.我正在使用 Discord v13 并且我正在关注此处的文档,以验证向机器人发送 DM 消息的用户是否具有Admin角色。

client.on('messageCreate', (message) => {
  if (message.member.roles.cache.has('role-id') {
    console.log('User is an admin')
  }
})

It works fine if I send a message to my bot from a channel, which is not what I want.如果我从频道向我的机器人发送消息,它工作正常,这不是我想要的。 I need the bot to check for the role when the user sends a DM to the bot.当用户向机器人发送 DM 时,我需要机器人检查角色。 However, the member property returns null when I send a DM to the bot.但是,当我向机器人发送 DM 时, member属性返回null

Is there something that I'm missing?有什么我想念的吗? Some intent or partial?一些意图或部分? These are my intents/partials so far:到目前为止,这些是我的意图/部分:

const client = new Client({
  intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.DIRECT_MESSAGES],
  partials: ['CHANNEL']
});

I also tried to access the property channel to see if I could fetch the roles but no success.我还尝试访问属性channel以查看是否可以获取角色但没有成功。 Any hints guys?有什么提示吗? I would appreciate a lot if someone could help me here.如果有人可以在这里帮助我,我将不胜感激。 This is the message object that I get when the bot receives a DM:这是我在机器人收到 DM 时收到的消息 object:

<ref *1> Message {
channelId: 'channel-id',
guildId: null,
deleted: false,
id: '919665813525495879',
createdTimestamp: 1639335816509,
type: 'DEFAULT',
system: false,
content: '!get-comic',
author: User {
  id: '611561522673745931',
  bot: false,
  system: false,
  flags: UserFlags { bitfield: 0 },
  username: 'deric',
  discriminator: '6024',
  avatar: '0277c05b2fe81cd286dc403b5e88c7ba',
  banner: undefined,
  accentColor: undefined
},
pinned: false,
tts: false,
nonce: '919665812787167232',
embeds: [],
components: [],
attachments: Collection(0) [Map] {},
stickers: Collection(0) [Map] {},
editedTimestamp: null,
reactions: ReactionManager { message: [Circular *1] },
mentions: MessageMentions {
  everyone: false,
  users: Collection(0) [Map] {},
  roles: Collection(0) [Map] {},
  _members: null,
  _channels: null,
  crosspostedChannels: Collection(0) [Map] {},
  repliedUser: null
},
webhookId: null,
groupActivityApplication: null,
applicationId: null,
activity: null,
flags: MessageFlags { bitfield: 0 },
reference: null,
interaction: null
}

Your intents and partials look okay to me.你的意图和部分对我来说看起来不错。 However, in DMs the properties guild , guildId , and member are null .但是,在 DM 中,属性guildguildIdmembernull You can see that by reading the docs.您可以通过阅读文档看到这一点。

If you know the guild ID, you can fetch the member by its ID.如果您知道公会 ID,则可以通过其 ID 获取成员 You can get this user ID from message.author .您可以从message.author获取此用户 ID。

client.on('messageCreate', async (message) => {
  // the ID of the guild the message.author is a member of
  let guildId = '630136153909678321';
  let roleId = '869192198029209314';

  try {
    let guild = await client.guilds.fetch(guildId);
    let member = await guild.members.fetch(message.author.id);

    if (member.roles.cache.has(roleId)) {
      console.log('User is an admin');
    }
  } catch (err) {
    console.log(err);
  }
});

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

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