From b2a62d754eb92b09136e9c09ec309c09dca31e30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beatrice=20Dellac=C3=A0?= Date: Tue, 22 Nov 2022 21:42:35 +0100 Subject: [PATCH] Make invite support both slash and message commands --- .../wtf/beatrice/hidekobot/HidekoBot.java | 1 + .../hidekobot/commands/base/Invite.java | 37 +++++++++++ .../commands/message/InviteCommand.java | 63 +++++++++++++++++++ .../commands/slash/InviteCommand.java | 29 +++------ 4 files changed, 108 insertions(+), 22 deletions(-) create mode 100644 src/main/java/wtf/beatrice/hidekobot/commands/base/Invite.java create mode 100644 src/main/java/wtf/beatrice/hidekobot/commands/message/InviteCommand.java diff --git a/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java b/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java index 85499c0..f5123c5 100644 --- a/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java +++ b/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java @@ -114,6 +114,7 @@ public class HidekoBot // register message commands MessageCommandListener messageCommandListener = new MessageCommandListener(); messageCommandListener.registerCommand(new HelloCommand()); + messageCommandListener.registerCommand(new wtf.beatrice.hidekobot.commands.message.InviteCommand()); messageCommandListener.registerCommand(new wtf.beatrice.hidekobot.commands.message.BotInfoCommand()); messageCommandListener.registerCommand(new wtf.beatrice.hidekobot.commands.message.CoinFlipCommand()); messageCommandListener.registerCommand(new wtf.beatrice.hidekobot.commands.message.ClearCommand()); diff --git a/src/main/java/wtf/beatrice/hidekobot/commands/base/Invite.java b/src/main/java/wtf/beatrice/hidekobot/commands/base/Invite.java new file mode 100644 index 0000000..67a0cee --- /dev/null +++ b/src/main/java/wtf/beatrice/hidekobot/commands/base/Invite.java @@ -0,0 +1,37 @@ +package wtf.beatrice.hidekobot.commands.base; + +import net.dv8tion.jda.api.EmbedBuilder; +import net.dv8tion.jda.api.entities.MessageEmbed; +import net.dv8tion.jda.api.entities.emoji.Emoji; +import net.dv8tion.jda.api.interactions.components.buttons.Button; +import wtf.beatrice.hidekobot.Cache; +import wtf.beatrice.hidekobot.HidekoBot; + +public class Invite +{ + + public static MessageEmbed generateEmbed() + { + EmbedBuilder embedBuilder = new EmbedBuilder(); + + //embed processing + { + embedBuilder.setColor(Cache.getBotColor()); + String avatarUrl = HidekoBot.getAPI().getSelfUser().getAvatarUrl(); + if(avatarUrl != null) embedBuilder.setThumbnail(avatarUrl); + embedBuilder.setTitle("Invite"); + embedBuilder.appendDescription("Click on the button below to invite " + + Cache.getBotName() + + " to your server!"); + } + + return embedBuilder.build(); + } + + public static Button getInviteButton() + { + String inviteUrl = Cache.getInviteUrl(); + return Button.link(inviteUrl, "Invite " + Cache.getBotName()) + .withEmoji(Emoji.fromUnicode("\uD83C\uDF1F")); + } +} diff --git a/src/main/java/wtf/beatrice/hidekobot/commands/message/InviteCommand.java b/src/main/java/wtf/beatrice/hidekobot/commands/message/InviteCommand.java new file mode 100644 index 0000000..11d3497 --- /dev/null +++ b/src/main/java/wtf/beatrice/hidekobot/commands/message/InviteCommand.java @@ -0,0 +1,63 @@ +package wtf.beatrice.hidekobot.commands.message; + +import net.dv8tion.jda.api.Permission; +import net.dv8tion.jda.api.entities.MessageEmbed; +import net.dv8tion.jda.api.entities.emoji.Emoji; +import net.dv8tion.jda.api.events.message.MessageReceivedEvent; +import net.dv8tion.jda.api.interactions.components.buttons.Button; +import org.jetbrains.annotations.Nullable; +import wtf.beatrice.hidekobot.commands.base.Invite; +import wtf.beatrice.hidekobot.objects.commands.MessageCommand; + +import java.util.Collections; +import java.util.LinkedList; +import java.util.List; + +public class InviteCommand implements MessageCommand +{ + + @Override + public LinkedList getCommandLabels() { + return new LinkedList<>(Collections.singletonList("invite")); + } + + @Nullable + @Override + public List getPermissions() { + return null; + } + + @Override + public boolean passRawArgs() { + return false; + } + + @Override + public void runCommand(MessageReceivedEvent event, String label, String[] args) + { + + + MessageEmbed inviteEmbed = Invite.generateEmbed(); + Button inviteButton = Invite.getInviteButton(); + + // if this is a guild, don't spam the invite in public but DM it + if(event.getChannelType().isGuild()) + { + event.getAuthor().openPrivateChannel().queue(privateChannel -> + { + privateChannel.sendMessageEmbeds(inviteEmbed) + .addActionRow(inviteButton) + .queue(); + event.getMessage().addReaction(Emoji.fromUnicode("✅")).queue(); + }, (error) -> { + event.getMessage().addReaction(Emoji.fromUnicode("❌")).queue(); + }); + } else { + event.getMessage() + .replyEmbeds(inviteEmbed) + .addActionRow(inviteButton) + .queue(); + } + + } +} diff --git a/src/main/java/wtf/beatrice/hidekobot/commands/slash/InviteCommand.java b/src/main/java/wtf/beatrice/hidekobot/commands/slash/InviteCommand.java index 4c712a9..9cd2ebb 100644 --- a/src/main/java/wtf/beatrice/hidekobot/commands/slash/InviteCommand.java +++ b/src/main/java/wtf/beatrice/hidekobot/commands/slash/InviteCommand.java @@ -1,9 +1,8 @@ package wtf.beatrice.hidekobot.commands.slash; -import net.dv8tion.jda.api.EmbedBuilder; import net.dv8tion.jda.api.entities.Message; +import net.dv8tion.jda.api.entities.MessageEmbed; import net.dv8tion.jda.api.entities.channel.ChannelType; -import net.dv8tion.jda.api.entities.emoji.Emoji; import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; import net.dv8tion.jda.api.interactions.commands.build.CommandData; import net.dv8tion.jda.api.interactions.commands.build.Commands; @@ -11,8 +10,7 @@ import net.dv8tion.jda.api.interactions.components.buttons.Button; import net.dv8tion.jda.api.requests.restaction.WebhookMessageEditAction; import net.dv8tion.jda.api.requests.restaction.interactions.ReplyCallbackAction; import org.jetbrains.annotations.NotNull; -import wtf.beatrice.hidekobot.Cache; -import wtf.beatrice.hidekobot.HidekoBot; +import wtf.beatrice.hidekobot.commands.base.Invite; import wtf.beatrice.hidekobot.objects.commands.SlashCommandImpl; public class InviteCommand extends SlashCommandImpl @@ -30,6 +28,7 @@ public class InviteCommand extends SlashCommandImpl { // defer reply because this might take a moment ReplyCallbackAction replyCallbackAction = event.deferReply(); + // only make message permanent in DMs if(event.getChannelType() != ChannelType.PRIVATE) { @@ -37,27 +36,13 @@ public class InviteCommand extends SlashCommandImpl } replyCallbackAction.queue(); - EmbedBuilder embedBuilder = new EmbedBuilder(); - - //embed processing - { - embedBuilder.setColor(Cache.getBotColor()); - String avatarUrl = HidekoBot.getAPI().getSelfUser().getAvatarUrl(); - if(avatarUrl != null) embedBuilder.setThumbnail(avatarUrl); - embedBuilder.setTitle("Invite"); - embedBuilder.appendDescription("Click on the button below to invite " + - Cache.getBotName() + - " to your server!"); - } - - String inviteUrl = Cache.getInviteUrl(); - Button inviteButton = Button.link(inviteUrl, "Invite " + Cache.getBotName()) - .withEmoji(Emoji.fromUnicode("\uD83C\uDF1F")); + MessageEmbed inviteEmbed = Invite.generateEmbed(); + Button inviteButton = Invite.getInviteButton(); WebhookMessageEditAction reply = event.getHook() - .editOriginalEmbeds(embedBuilder.build()) - .setActionRow(inviteButton); + .editOriginalEmbeds(inviteEmbed) + .setActionRow(inviteButton); reply.queue(); }