node.jsdiscord.jsmessagesharding

Edit message with channel id and message id using shards


im trying to edit a specific message in a specific channel. I have getMessage function than return me the channel using broadcastEval because i use shards:

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;

And when I try to edit my message I cannot do it:

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)

Solution

  • I found a tricky solution, you should edit your message inside the 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 } });
    

    And if you want to call another function inside it declare it before: client.myFunction = myFunction;

    And after call it inside the bEval: c.myFunction();