如何使用fetchMessages()获取一定数量的消息,然后以某种方式发送它们?

时间:2018-04-26 20:24:46

标签: node.js discord discord.js

我想要做的是从fetchMessages()解析承诺中获取信息,然后能够通过它提供的集合和 然后 能够发送某些作品。

例如,机器人将列出10条看起来像GalaxyDJx#2462: hi的消息,如果可能的话,找到信息(例如用户名,鉴别器,消息内容),然后将每条信息设置为自己的变量,例如var unamevar discrmvar msgcnt

我从一条消息中获得的信息如下,我真正需要的是会员的用户名,鉴别符,并获取他们发送的消息内容。

'439129579567972353' => Message {
  channel: TextChannel {
    type: 'text',
    id: '411925734970490880',
    name: 'galaxys-bot-testing',
    position: 3,
    parentID: '394174705655087104',
    permissionOverwrites: [Object],
    topic: '',
    nsfw: false,
    lastMessageID: '439131920736059402',
    guild: [Object],
    messages: [Object],
    _typing: Map {},
    lastMessage: [Object]
  },
  id: '439129579567972353',
  type: 'DEFAULT',
  content: '<@436985225546039337> fetch',
  author: User {
    id: '311283024312532993',
    username: 'GalaxyDJx',
    discriminator: '2462',
    avatar: 'IgnoreThis',
    bot: false,
    lastMessageID: '439131920736059402',
    lastMessage: [Object]
  },
  member: GuildMember {
    guild: [Object],
    user: [Object],
    _roles: [Array],
    serverDeaf: false,
    serverMute: false,
    selfMute: undefined,
    selfDeaf: undefined,
    voiceSessionID: undefined,
    voiceChannelID: undefined,
    speaking: false,
    nickname: null,
    joinedTimestamp: 1518280794719,
    lastMessageID: '439131920736059402',
    lastMessage: [Object]
  },
  pinned: false,
  tts: false,
  nonce: undefined,
  system: false,
  embeds: [],
  attachments: Collection {},
  createdTimestamp: 1524767050402,
  editedTimestamp: null,
  reactions: Collection {},
  mentions: MessageMentions {
    everyone: false,
    users: [Object],
    roles: Collection {},
    _content: '<@436985225546039337> fetch',
    _client: [Object],
    _guild: [Object],
    _members: null,
    _channels: null
  },
  webhookID: null,
  hit: null,
  _edits: []
}

我从使用此代码获得此信息。

var promise1 = Promise.resolve(message.channel.fetchMessages({limit: 1}));

promise1.then(function(value) {
  console.log(value);
});

1 个答案:

答案 0 :(得分:0)

由于TextChannel.fetchMessages()返回了Promise,因此您无需使用Promise.resolve(),存储返回的值和所有这些东西:您可以这样做:

message.channel.fetchMessages({limit: 1}).then(console.log);
//       this returns a Promise          |     Here you're using Promise.then()