From 18db0282d546ea5e0e5ab2d3b72ddc30773693fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beatrice=20Dellac=C3=A0?= Date: Sun, 20 Nov 2022 16:07:04 +0100 Subject: [PATCH] Implement invite link command --- .../wtf/beatrice/hidekobot/Configuration.java | 16 ++++++++++++++++ .../java/wtf/beatrice/hidekobot/HidekoBot.java | 9 +++------ .../hidekobot/listeners/MessageListener.java | 2 +- .../listeners/SlashCommandListener.java | 6 ++---- .../hidekobot/slashcommands/DieCommand.java | 2 +- .../hidekobot/slashcommands/InviteCommand.java | 13 +++++++++++++ .../hidekobot/utils/SlashCommandsUtil.java | 1 + 7 files changed, 37 insertions(+), 12 deletions(-) create mode 100644 src/main/java/wtf/beatrice/hidekobot/slashcommands/InviteCommand.java diff --git a/src/main/java/wtf/beatrice/hidekobot/Configuration.java b/src/main/java/wtf/beatrice/hidekobot/Configuration.java index 47fb1f7..8fa5c94 100644 --- a/src/main/java/wtf/beatrice/hidekobot/Configuration.java +++ b/src/main/java/wtf/beatrice/hidekobot/Configuration.java @@ -12,6 +12,11 @@ public class Configuration // todo: allow people to set their own user id private static final long botOwnerId = 979809420714332260L; + private final static String defaultInviteLink = + "https://discord.com/api/oauth2/authorize?client_id=%userid%&scope=bot+applications.commands&permissions=8"; + + private static String botUserId = ""; + /** * Checks if the bot has been started with the verbose argument. @@ -54,4 +59,15 @@ public class Configuration */ public static long getBotOwnerId() { return botOwnerId; } + public static void seBotUserId(String id) + { + botUserId = id; + } + + public static String getBotUserId() { return botUserId; } + + public static String getInviteUrl() { + return defaultInviteLink.replace("%userid%", botUserId); + } + } diff --git a/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java b/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java index f69b68e..b7e7d51 100644 --- a/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java +++ b/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java @@ -20,9 +20,6 @@ import java.util.concurrent.TimeUnit; public class HidekoBot { private static String botToken; - private static String standardInviteLink = - "https://discord.com/oauth2/authorize?client_id=%userid%&scope=bot+applications.commands&permissions=8"; - private static String botUserId; private static final String version = "0.0.2-slash"; // we should probably find a way to make this consistent with Maven private static JDA jda; @@ -64,8 +61,8 @@ public class HidekoBot } // find the bot's user id and generate an invite-link. - botUserId = jda.getSelfUser().getId(); - standardInviteLink = standardInviteLink.replace("%userid%", botUserId); + String botUserId = jda.getSelfUser().getId(); + Configuration.seBotUserId(botUserId); // register listeners jda.addEventListener(new MessageListener()); @@ -94,7 +91,7 @@ public class HidekoBot // log the invite-link to console so noob users can just click on it. logger.log("Bot User ID: " + botUserId, 3); - logger.log("Invite Link: " + standardInviteLink, 4); + logger.log("Invite Link: " + Configuration.getInviteUrl(), 4); } public static JDA getAPI() diff --git a/src/main/java/wtf/beatrice/hidekobot/listeners/MessageListener.java b/src/main/java/wtf/beatrice/hidekobot/listeners/MessageListener.java index 35bd062..e40e63e 100644 --- a/src/main/java/wtf/beatrice/hidekobot/listeners/MessageListener.java +++ b/src/main/java/wtf/beatrice/hidekobot/listeners/MessageListener.java @@ -20,7 +20,7 @@ public class MessageListener extends ListenerAdapter if(eventMessage.equalsIgnoreCase("hideko")) { MessageChannel channel = event.getChannel(); - channel.sendMessage("Hello there! :sparkles:").queue(); + channel.sendMessage("Hello there! ✨").queue(); return; } diff --git a/src/main/java/wtf/beatrice/hidekobot/listeners/SlashCommandListener.java b/src/main/java/wtf/beatrice/hidekobot/listeners/SlashCommandListener.java index 4d7d08f..8da3a8f 100644 --- a/src/main/java/wtf/beatrice/hidekobot/listeners/SlashCommandListener.java +++ b/src/main/java/wtf/beatrice/hidekobot/listeners/SlashCommandListener.java @@ -3,10 +3,7 @@ package wtf.beatrice.hidekobot.listeners; import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; import net.dv8tion.jda.api.hooks.ListenerAdapter; import org.jetbrains.annotations.NotNull; -import wtf.beatrice.hidekobot.slashcommands.ClearChatCommand; -import wtf.beatrice.hidekobot.slashcommands.CoinFlipCommand; -import wtf.beatrice.hidekobot.slashcommands.DieCommand; -import wtf.beatrice.hidekobot.slashcommands.PingCommand; +import wtf.beatrice.hidekobot.slashcommands.*; public class SlashCommandListener extends ListenerAdapter { @@ -18,6 +15,7 @@ public class SlashCommandListener extends ListenerAdapter case "clear" -> new ClearChatCommand(event); case "coinflip" -> new CoinFlipCommand(event); case "die" -> new DieCommand(event); + case "invite" -> new InviteCommand(event); case "ping" -> new PingCommand(event); } } diff --git a/src/main/java/wtf/beatrice/hidekobot/slashcommands/DieCommand.java b/src/main/java/wtf/beatrice/hidekobot/slashcommands/DieCommand.java index 5ef8ba6..eccbad7 100644 --- a/src/main/java/wtf/beatrice/hidekobot/slashcommands/DieCommand.java +++ b/src/main/java/wtf/beatrice/hidekobot/slashcommands/DieCommand.java @@ -17,7 +17,7 @@ public class DieCommand { event.reply("Sorry, only the bot owner can run this command!").setEphemeral(true).queue(); } else { - event.reply("Going to sleep! Cya :sparkles:").queue(); + event.reply("Going to sleep! Cya ✨").queue(); Executors.newSingleThreadScheduledExecutor().schedule(HidekoBot::shutdown, 3, TimeUnit.SECONDS); } } diff --git a/src/main/java/wtf/beatrice/hidekobot/slashcommands/InviteCommand.java b/src/main/java/wtf/beatrice/hidekobot/slashcommands/InviteCommand.java new file mode 100644 index 0000000..16664f3 --- /dev/null +++ b/src/main/java/wtf/beatrice/hidekobot/slashcommands/InviteCommand.java @@ -0,0 +1,13 @@ +package wtf.beatrice.hidekobot.slashcommands; + +import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; +import org.jetbrains.annotations.NotNull; +import wtf.beatrice.hidekobot.Configuration; + +public class InviteCommand +{ + public InviteCommand(@NotNull SlashCommandInteractionEvent event) + { + event.reply("Here's your link ✨ " + Configuration.getInviteUrl()).setEphemeral(true).queue(); + } +} diff --git a/src/main/java/wtf/beatrice/hidekobot/utils/SlashCommandsUtil.java b/src/main/java/wtf/beatrice/hidekobot/utils/SlashCommandsUtil.java index e6988d8..897398a 100644 --- a/src/main/java/wtf/beatrice/hidekobot/utils/SlashCommandsUtil.java +++ b/src/main/java/wtf/beatrice/hidekobot/utils/SlashCommandsUtil.java @@ -26,6 +26,7 @@ public class SlashCommandsUtil .addOption(OptionType.INTEGER, "amount", "The amount of messages to delete.") .setDefaultPermissions(DefaultMemberPermissions.enabledFor(Permission.MESSAGE_MANAGE))); add(Commands.slash("coinflip", "Flip a coin and get head or tails.")); + add(Commands.slash("invite", "Get an invite link for the bot.")); add(Commands.slash("ping", "Test if the bot is responsive.")); }};