How to use fetchMessages() to fetch a certain amou

2019-08-23 09:59发布

问题:

What I would like to do is to take information from resolving a promise from fetchMessages() and then being able to go through the collection it gives and then being able to send certain pieces.

For example, the bot will list 10 messages looking like GalaxyDJx#2462: hi by, if possible, finding the information (e.g. username, discriminator, message content) and then having each piece of information set to its own variable such as var uname, var discrm, and var msgcnt.

The information I got from one message is below and all I really need is the member's username, discriminator, and get the message content that they sent.

'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: []
}

I got this information from using this code.

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

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

回答1:

Since TextChannel.fetchMessages() returns a Promise, you don't need to use Promise.resolve(), store the returned value and all that stuff: you can just do this:

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