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

[FEATURE] Webhooks #147

Closed
wants to merge 0 commits into from
Closed

Conversation

blueysh
Copy link
Member

@blueysh blueysh commented Mar 20, 2023

Pull Request

  • I have checked the PRs for upcoming features/bug fixes.

Changes

  • Internal code
  • Library
  • Documentation

Closes #28

Description

Adds Webhooks. This pull request isn't final yet (I think) so I will probably mark it as draft.

@seailz seailz changed the title feat: start webhooks!!!! [FEATURE] Webhooks Mar 21, 2023
Copy link
Member

@seailz seailz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't try-catch the `DiscordRequest.UnhandledDiscordAPIErrorException" error. It should be added to the method signature.

@blueysh
Copy link
Member Author

blueysh commented Mar 22, 2023

Don't try-catch the `DiscordRequest.UnhandledDiscordAPIErrorException" error. It should be added to the method signature.

@seailz Fixed. Any other comments on the current code?

@blueysh
Copy link
Member Author

blueysh commented Mar 22, 2023

Did you want me to keep the Webhook class or just IncomingWebhook now that there will only be one kind of Webhook?

@seailz
Copy link
Member

seailz commented Mar 22, 2023

Did you want me to keep the Webhook class or just IncomingWebhook now that there will only be one kind of Webhook?

leave it how it is atm

@blueysh blueysh requested a review from seailz March 23, 2023 18:30
@blueysh blueysh requested a review from seailz March 23, 2023 19:59
* @param url The URL of the Webhook.
* @param discordJar A discord.jar instance used internally.
*/
public record Webhook(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please implement modify methods.

* @param url The URL of the Webhook.
* @param discordJar A discord.jar instance used internally.
*/
public record Webhook(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please implement the ability to retrieve, edit, and delete webhook messages.

@blueysh
Copy link
Member Author

blueysh commented May 19, 2023

Will eventually be re-opened. Fixing files

@blueysh blueysh reopened this Jun 16, 2023
@blueysh blueysh requested a review from seailz June 16, 2023 22:21
@blueysh blueysh requested a review from seailz July 2, 2023 18:58
Copy link
Member

@seailz seailz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think these are the last things that need doing before we get this merged.

* @param user The user object tied to the Webhook.
* @param discordJar A discord.jar instance used internally.
*/
public record IncomingWebhook(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please implement the ability to retrieve, edit, and delete webhook messages.

@seailz seailz added status: in progress PR or issue is currently in progress type: feature Issue or PR is a feature that should be implemented priority: medium Medium priority labels Jul 2, 2023
@seailz seailz added this to the discord.jar 1.0 milestone Jul 2, 2023
@blueysh blueysh requested a review from seailz July 6, 2023 19:06
@blueysh blueysh requested a review from seailz July 7, 2023 05:17
Comment on lines 100 to 102
public EditWebhookMessageAction editMessage(String messageId, String content, List<Embed> embeds, List<Attachment> attachments, String usernameOverride, String avatarUrlOverride, String threadName){
return new EditWebhookMessageAction(messageId, content, embeds, attachments, usernameOverride, avatarUrlOverride, threadName, id, token, discordJar);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public EditWebhookMessageAction editMessage(String messageId, String content, List<Embed> embeds, List<Attachment> attachments, String usernameOverride, String avatarUrlOverride, String threadName){
return new EditWebhookMessageAction(messageId, content, embeds, attachments, usernameOverride, avatarUrlOverride, threadName, id, token, discordJar);
}
public EditWebhookMessageAction editMessage(String messageId){
return new EditWebhookMessageAction(messageId, id, token, discordJar);
}

Please also add other messages for sending embeds, attachements, etc.

Comment on lines 28 to 30
private final Snowflake channelId;
private final Snowflake webhookId;
private final Snowflake guildId;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These should all be strings - not snowflakes. This applies to the rest of the project, by the way.

Comment on lines 33 to 45
public WebhookExecuteAction(String content, List<Embed> embeds, List<Attachment> attachments, String usernameOverride, String avatarUrlOverride, String threadName, DiscordJar discordJar, Snowflake channelId, Snowflake webhookId, Snowflake guildId, String webhookToken) {
this.content = content;
this.embeds = embeds;
this.attachments = attachments;
this.usernameOverride = usernameOverride;
this.avatarUrlOverride = avatarUrlOverride;
this.threadName = threadName;
this.discordJar = discordJar;
this.channelId = channelId;
this.webhookId = webhookId;
this.guildId = guildId;
this.webhookToken = webhookToken;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public WebhookExecuteAction(String content, List<Embed> embeds, List<Attachment> attachments, String usernameOverride, String avatarUrlOverride, String threadName, DiscordJar discordJar, Snowflake channelId, Snowflake webhookId, Snowflake guildId, String webhookToken) {
this.content = content;
this.embeds = embeds;
this.attachments = attachments;
this.usernameOverride = usernameOverride;
this.avatarUrlOverride = avatarUrlOverride;
this.threadName = threadName;
this.discordJar = discordJar;
this.channelId = channelId;
this.webhookId = webhookId;
this.guildId = guildId;
this.webhookToken = webhookToken;
}
public WebhookExecuteAction(String content, DiscordJar discordJar, String channelId, String webhookId, String guildId, String webhookToken) {
this.content = content;
this.discordJar = discordJar;
this.channelId = channelId;
this.webhookId = webhookId;
this.guildId = guildId;
this.webhookToken = webhookToken;
}

Comment on lines 87 to 89
public WebhookExecuteAction execute(String content, List<Embed> embeds, List<Attachment> attachments, String usernameOverride, String avatarUrlOverride, String threadName){
return new WebhookExecuteAction(content, embeds, attachments, usernameOverride, avatarUrlOverride, threadName, discordJar, channelId, id, guildId, token);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public WebhookExecuteAction execute(String content, List<Embed> embeds, List<Attachment> attachments, String usernameOverride, String avatarUrlOverride, String threadName){
return new WebhookExecuteAction(content, embeds, attachments, usernameOverride, avatarUrlOverride, threadName, discordJar, channelId, id, guildId, token);
}
public WebhookExecuteAction execute(String content) {
return new WebhookExecuteAction(content, discordJar, channelId, id, guildId, token);
}

Comment on lines 115 to 145
public Response<Message> run() {
Response<Message> future = new Response<>();
new Thread(() -> {

JSONObject body = new JSONObject();
if (content != null) body.put("content", content);
if (embeds != null) body.put("embeds", new JSONArray(embeds));
if (attachments != null) body.put("attachments", new JSONArray(attachments));
if (usernameOverride != null) body.put("username", usernameOverride);
if (avatarUrlOverride != null) body.put("avatar_url", avatarUrlOverride);
if (threadName != null) {
body.put("thread_name", threadName);
}
DiscordRequest request = new DiscordRequest(
body,
new HashMap<>(),
URLS.POST.GUILDS.CHANNELS.EXECUTE_WEBHOOK_WITH_WAIT.replace("{guild.id}", guildId.id()).replace("{channel.id}", channelId.id()).replace("{webhook.id}", webhookId.id()).replace("{webhook.token}", webhookToken),
discordJar,
URLS.POST.GUILDS.CHANNELS.EXECUTE_WEBHOOK_WITH_WAIT,
RequestMethod.POST
);
try {
future.complete(Message.decompile(request.invoke().body(), discordJar));
} catch (DiscordRequest.UnhandledDiscordAPIErrorException e) {
future.completeError(new Response.Error(e));
return;
}

}).start();

return future;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add support for files - see the MessageCreateAction class for more details on how that works.

* @param webhook.token The token of the webhook
* @param wait Whether to wait for server confirmation
*/
public static final String EXECUTE_WEBHOOK_WITH_WAIT = "/webhooks/{webhook.id}/{webhook.token}?wait=true";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please actually add support for this endpoint - also, query params should be implemented at use instead of as separate URLs.

Comment on lines 30 to 41
public EditWebhookMessageAction(String messageId, String content, List<Embed> embeds, List<Attachment> attachments, String usernameOverride, String avatarUrlOverride, String threadName, Snowflake webhookId, String webhookToken, DiscordJar discordJar) {
this.messageId = messageId;
this.content = content;
this.embeds = embeds;
this.attachments = attachments;
this.usernameOverride = usernameOverride;
this.avatarUrlOverride = avatarUrlOverride;
this.threadName = threadName;
this.webhookId = webhookId;
this.webhookToken = webhookToken;
this.discordJar = discordJar;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public EditWebhookMessageAction(String messageId, String content, List<Embed> embeds, List<Attachment> attachments, String usernameOverride, String avatarUrlOverride, String threadName, Snowflake webhookId, String webhookToken, DiscordJar discordJar) {
this.messageId = messageId;
this.content = content;
this.embeds = embeds;
this.attachments = attachments;
this.usernameOverride = usernameOverride;
this.avatarUrlOverride = avatarUrlOverride;
this.threadName = threadName;
this.webhookId = webhookId;
this.webhookToken = webhookToken;
this.discordJar = discordJar;
}
public EditWebhookMessageAction(String messageId, String webhookId, String webhookToken, DiscordJar discordJar) {
this.messageId = messageId;
this.webhookId = webhookId;
this.webhookToken = webhookToken;
this.discordJar = discordJar;
}

@seailz
Copy link
Member

seailz commented Jul 7, 2023

For future reference, a class that has an id field should implement the Snowflake interface - it's not meant to represent individual snowflakes.

URLS.POST.GUILDS.CHANNELS.EXECUTE_WEBHOOK,
RequestMethod.POST
);
public Message getMessage(String messageId) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rename this to getMessageById please.

@blueysh blueysh closed this Oct 2, 2023
@blueysh blueysh deleted the feature/webhooks branch October 2, 2023 17:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority: medium Medium priority status: in progress PR or issue is currently in progress type: feature Issue or PR is a feature that should be implemented
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Feature Request] Webhooks
2 participants