Make invite support both slash and message commands
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Bea 2022-11-22 21:42:35 +01:00
parent c186c9c576
commit b2a62d754e
4 changed files with 108 additions and 22 deletions

View File

@ -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());

View File

@ -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"));
}
}

View File

@ -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<String> getCommandLabels() {
return new LinkedList<>(Collections.singletonList("invite"));
}
@Nullable
@Override
public List<Permission> 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();
}
}
}

View File

@ -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<Message> reply =
event.getHook()
.editOriginalEmbeds(embedBuilder.build())
.setActionRow(inviteButton);
.editOriginalEmbeds(inviteEmbed)
.setActionRow(inviteButton);
reply.queue();
}