Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Webhook): add '(edit|delete)Message' methods #5223

Merged
merged 14 commits into from Apr 3, 2021

Conversation

izexi
Copy link
Contributor

@izexi izexi commented Jan 18, 2021

Adds support for the Edit Webhook Message and Delete Webhook Message endpoints.

After #4876 is merged, the object that represents the raw message data in the typings can be replaced to be more accurate with either WebhookRawMessageResponse (or eventually APIMessage over on discord-api-types). (See df89d04)

Here's a snippet of how I tested these changes
import { once } from 'events';
import { TOKEN } from './constants';
import { APIMessage, Client, MessageEmbed, WebhookClient } from 'discord.js';

const client = new Client();
let guild, webhook, message;

beforeAll(async (done) => {
  client.login(TOKEN);
  await once(client, 'ready');

  guild = await client.guilds.create('guild');
  webhook = await guild.channels.cache.find(({ type }) => type === 'text').createWebhook('webhook');
  message = await webhook.send('message');

  done();
});

test('calling Webhook#editMessage() on a cached message should return the updated Message', async () => {
  const content = 'foo';
  const embeds = [new MessageEmbed().setDescription('foo'), new MessageEmbed().setDescription('bar')];

  await expect(webhook.editMessage(message, content, { embeds })).resolves.toMatchObject({ content, embeds });
});

test('calling Webhook#editMessage() on an uncached message should return the updated Message', async () => {
  const content = 'bar';
  message.channel.messages.cache.delete(message.id);

  await expect(webhook.editMessage(message, content)).resolves.toMatchObject({ content });
});

test('calling Webhook#editMessage() with an APIMessage|MessageEmbed|MessageEmbed[] as the 2nd param', async () => {
  const content = 'foo';
  const embed = new MessageEmbed().setDescription('foo');
  const embeds = [embed, embed.setDescription('bar')];

  await expect(webhook.editMessage(message, APIMessage.create(webhook, content))).resolves.toMatchObject({ content });
  await expect(webhook.editMessage(message, embed)).resolves.toMatchObject({ embeds: [embed] });
  await expect(webhook.editMessage(message, embeds)).resolves.toMatchObject({ embeds });
});

test('calling Webhook#editMessage() with files', async () => {
  const url = 'https://cdn.discordapp.com/avatars/191615925336670208/a_594885c678ec070375a459e56c623dbd.gif?size=4096';
  const { attachments } = await webhook.editMessage(message.id, { files: [url] });

  await expect(attachments.size === 1);
  await expect(attachments.first().url === url);
});

test('calling Webhook#editMessage() on a message that belongs to an uncached channel or WebhookCliet#editMessage() should return the raw message data', async () => {
  client.channels.remove(message.channel.id);

  await expect(webhook.editMessage(message.id, 'baz')).resolves.toMatchObject({ type: 0 });
  await expect(new WebhookClient(webhook.id, webhook.token).editMessage(message.id, 'qux')).resolves.toMatchObject({
    type: 0,
  });
});

test('calling Webhook#deleteMessage() and then fetching the deleted message should reject with an error', async () => {
  await webhook.deleteMessage(message);

  await expect(message.fetch(true)).rejects.toThrow('Unknown Message');
});

afterAll(() => guild.delete());

Status

  • Code changes have been tested against the Discord API, or there are no code changes
  • I know how to update typings and have done so, or typings don't need updating

Semantic versioning classification:

  • This PR changes the library's interface (methods or parameters added)
    • This PR includes breaking changes (methods removed or renamed, parameters moved or removed)
  • This PR only includes non-code changes, like changes to documentation, README, etc.

typings/index.d.ts Outdated Show resolved Hide resolved
typings/index.d.ts Outdated Show resolved Hide resolved
@SpaceEEC
Copy link
Member

This PR probably needs a rebase, I suspect that the duplicated type is from this PR and #4876.
Th merge commit as of now: 45adb3b#diff-4f45caa500ef03d94d3c2bfa556caa1642df95d4e2b980d76b876a8fd2e8c522R3349

typings(WebhookFields): add '(edit|delete)Message' methods

chore(typings): merge upstream changes

refactor(typings): use WebhookRawMessageResponse

feat(typings): add stricter overloads for 'WebhookClient'

fix(typings): include 'content' in 'WebhookMessageOptions'

docs(WebhookMessageOptions): add 'content' prop

docs(Webhook): create WebhookEditMessageOptions typedef
@izexi
Copy link
Contributor Author

izexi commented Feb 1, 2021

Added the option to allow APIMessage as the 2nd param to be consistent with send() and other message editing methods and also MessageEmbed/MessageEmbed[] can be accepted as the 2nd param since APIMessage#resolveData() can handle that.

Code used to test this change (which has also been added within the test code in the PR description)
test('calling Webhook#editMessage() with an APIMessage|MessageEmbed|MessageEmbed[] as the 2nd param', async () => {
  const content = 'foo';
  const embed = new MessageEmbed().setDescription('foo');
  const embeds = [embed, embed.setDescription('bar')];

  await expect(webhook.editMessage(message, APIMessage.create(webhook, content))).resolves.toMatchObject({ content });
  await expect(webhook.editMessage(message, embed)).resolves.toMatchObject({ embeds: [embed] });
  await expect(webhook.editMessage(message, embeds)).resolves.toMatchObject({ embeds });
});

@advaith1
Copy link
Contributor

advaith1 commented Mar 4, 2021

Attachments can be uploaded when editing a webhook message (discord/discord-api-docs#2615 (comment), discord/discord-api-docs#2615 (comment))

@SpaceEEC SpaceEEC linked an issue Mar 8, 2021 that may be closed by this pull request
@izexi
Copy link
Contributor Author

izexi commented Mar 20, 2021

Added the option to edit the message with files (ref: discord/discord-api-docs#2720)

Code used to test this change (which has also been added within the test code in the PR description)
test('calling Webhook#editMessage() with files', async () => {
  const url = 'https://cdn.discordapp.com/avatars/191615925336670208/a_594885c678ec070375a459e56c623dbd.gif?size=4096';
  const { attachments } = await webhook.editMessage(message.id, { files: [url] });

  await expect(attachments.size === 1);
  await expect(attachments.first().url === url);
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Can't edit message sent through Webhook
8 participants