Skip to content

Commit

Permalink
Bug/ allows OP of embed from transfer-feature to dismiss GPT's respon…
Browse files Browse the repository at this point in the history
…se (#951)

* refactor dismiss gpt ans button to allow embed author use it

* refactor if conditions into seperate method for better readability

* refactor to replace complete with queue

* refactor dismiss button flow

* improve message UX
  • Loading branch information
ankitsmt211 authored Nov 16, 2023
1 parent 79a659f commit 837e913
Showing 1 changed file with 42 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -157,19 +157,15 @@ public void acceptComponentIdGenerator(ComponentIdGenerator generator) {
@Override
public void onButtonClick(ButtonInteractionEvent event, List<String> args) {
// This method handles chatgpt's automatic response "dismiss" button
event.deferEdit().queue();

ThreadChannel channel = event.getChannel().asThreadChannel();
Member interactionUser = Objects.requireNonNull(event.getMember());
if (channel.getOwnerIdLong() != interactionUser.getIdLong()
&& !helper.hasTagManageRole(interactionUser)) {
event.reply("You do not have permission for this action.").setEphemeral(true).queue();
return;
}

RestAction<Void> deleteMessages = event.getMessage().delete();
for (String id : args) {
deleteMessages = deleteMessages.and(channel.deleteMessageById(id));
}
deleteMessages.queue();
channel.retrieveMessageById(channel.getId())
.queue(forumPostMessage -> handleDismiss(interactionUser, channel, forumPostMessage,
event, args));

}

@Override
Expand All @@ -182,4 +178,40 @@ public void onModalSubmitted(ModalInteractionEvent event, List<String> args) {
throw new UnsupportedOperationException("Not used");
}

private boolean isPostAuthor(Member interactionUser, Message message) {
if (message.getEmbeds().isEmpty())
return false;

String embedAuthor = Objects
.requireNonNull(message.getEmbeds().get(0).getAuthor(),
"embed author for forum post is null")
.getName();

return embedAuthor.equals(interactionUser.getUser().getName());
}

private boolean isAuthorized(Member interactionUser, ThreadChannel channel,
Message forumPostMessage) {
return (channel.getOwnerIdLong() == interactionUser.getIdLong())
|| helper.hasTagManageRole(interactionUser)
|| isPostAuthor(interactionUser, forumPostMessage);
}

private void handleDismiss(Member interactionUser, ThreadChannel channel,
Message forumPostMessage, ButtonInteractionEvent event, List<String> args) {
boolean isAuthorized = isAuthorized(interactionUser, channel, forumPostMessage);
if (!isAuthorized) {
event.getHook()
.sendMessage("You do not have permission for this action.")
.setEphemeral(true)
.queue();
return;
}

RestAction<Void> deleteMessages = event.getMessage().delete();
for (String id : args) {
deleteMessages = deleteMessages.and(channel.deleteMessageById(id));
}
deleteMessages.queue();
}
}

0 comments on commit 837e913

Please sign in to comment.