NodeJS 使用分片用通道id和消息id编辑消息

wqsoz72f  于 2023-06-22  发布在  Node.js
关注(0)|答案(1)|浏览(145)

我试图编辑一个特定的消息在一个特定的渠道。我有一个getMessage函数,它使用broadcastEval返回频道,因为我使用了分片:

async function getMessage(channelId, messageId, client) {
  const results = await client.shard.broadcastEval(async (c, { channelId, messageId }) => {
    const channel = await c.channels.fetch(channelId);
    const message = await channel.messages.fetch(messageId);
    return message;
  }, { context: { channelId, messageId } });
  return results.find(result => result !== null);
}

module.exports = getMessage;

当我尝试编辑我的消息时,我无法做到:

const message = await getMessage('1114550429917925416', '1118615356701937746', client);
console.log('message', message);
message.edit({content: 'test'}); // LINE 136
message {
  channelId: '1114550429917925416',
  guildId: '1060112326361092247',
  id: '1118615356701937746',
  createdTimestamp: 1686769083906,
  type: 0,
  system: false,
  content: '',
  authorId: '829707635787825152',
  pinned: false,
  tts: false,
  nonce: null,
  embeds: [
    {
      type: 'rich',
      title: '📊 **1**',
      color: 5197823,
      timestamp: '2023-06-14T18:58:03.718000+00:00',
      fields: [Array],
      footer: [Object]
    }
  ],
  components: [
    { type: 1, components: [Array] },
    { type: 1, components: [Array] }
  ],
  attachments: [],
  stickers: [],
  position: null,
  roleSubscriptionData: null,
  editedTimestamp: 1686769084345,
  mentions: {
    everyone: false,
    users: [],
    roles: [],
    crosspostedChannels: [],
    repliedUser: null,
    members: [],
    channels: []
  },
  webhookId: null,
  groupActivityApplicationId: null,
  applicationId: null,
  activity: null,
  flags: 0,
  reference: null,
  interaction: null,
  cleanContent: ''
}
node:events:505
      throw er; // Unhandled 'error' event
      ^

TypeError: message.edit is not a function
    at Object.execute (/src/events/client/ready.js:136:11)
oprakyz7

oprakyz71#

我发现了一个棘手的解决方案,你应该在bEval中编辑你的消息:

const results = await client.shard.broadcastEval(async (c, { channelId, messageId }) => {
  const channel = await c.channels.cache.get(channelId);
  const message = await channel.messages.fetch(messageId);
  message.edit();    // HERE
}, { context: { channelId, messageId } });

如果你想在它内部调用另一个函数,请在下面声明:client.myFunction = myFunction;
然后在bEval中调用它:c.myFunction();

相关问题