eilvelia / tdl

Node.js bindings to TDLib 🥒
MIT License
394 stars 52 forks source link

[Bug] Can't delete messages #126

Closed AlexXanderGrib closed 1 year ago

AlexXanderGrib commented 1 year ago

Conditions

  1. Client logged in as bot
  2. Bot has full permissions in the chat
  3. Message exists

Code

td.invoke({
  _: 'deleteMessages',
  chat_id: -1001111111111,
  message_ids: [2219055],
  revoke: true
});

Error

({
  _: 'error',
  message: 'Invalid message identifier',
  code: 400
});
AlexXanderGrib commented 1 year ago

Actually, message ids in Bot API and TDLib are different. They are skewed by 20 bytes.

So here is the code to solve this

function tdMessageId(botApiMessageId) {
  return Number(BigInt(botApiMessageId) << 20n);
}

function botApiMessageId(tdMessageId) {
  return Number(BigInt(tdMessageId) >> 20n);
}

Conversion to BigInt is required cause bitwise operations truncate any integer stored in Number to just 32 bytes