From 971f71b78d2caebdeae5e6b7d516571a2f27e339 Mon Sep 17 00:00:00 2001 From: Joel Date: Sat, 22 May 2021 22:00:59 +0200 Subject: [PATCH] Created a bot for BG-Software auto responder. Still needs cleanup. And i will move it to it's own project soon. --- bgsoftware-discordbot/build.gradle.kts | 26 ++++++ .../themcgamer/discordbot/BGSoftwareBot.java | 68 +++++++++++++++ .../themcgamer/discordbot/BotConstants.java | 36 ++++++++ .../discordbot/command/BaseCommand.java | 35 ++++++++ .../impl/AddReactionToMessageCommand.java | 48 ++++++++++ .../command/impl/EditMessageCommand.java | 53 +++++++++++ .../command/impl/InviteCommand.java | 42 +++++++++ .../command/impl/JsonParseCommand.java | 26 ++++++ .../command/impl/MemberCountCommand.java | 37 ++++++++ .../command/impl/MessageCommand.java | 38 ++++++++ .../discordbot/command/impl/PingCommand.java | 50 +++++++++++ .../command/impl/SetActivityCommand.java | 44 ++++++++++ .../discordbot/events/GuildListener.java | 55 ++++++++++++ .../discordbot/events/HasteBinListener.java | 39 +++++++++ .../discordbot/events/MainGuildListener.java | 28 ++++++ .../discordbot/events/TestGuildListener.java | 4 + .../themcgamer/discordbot/guild/Guild.java | 15 ++++ .../discordbot/utilities/EmbedUtils.java | 30 +++++++ .../discordbot/utilities/GuildUtils.java | 25 ++++++ .../discordbot/utilities/HasteBinHandler.java | 46 ++++++++++ .../discordbot/utilities/MessageUtils.java | 39 +++++++++ .../discordbot/utilities/Responses.java | 87 +++++++++++++++++++ .../src/main/resources/META-INF/MANIFEST.MF | 3 + .../zone/themcgamer/discordbot/MGZBot.java | 1 + .../command/impl/CommandsCommand.java | 35 ++++++++ settings.gradle.kts | 3 +- 26 files changed, 912 insertions(+), 1 deletion(-) create mode 100644 bgsoftware-discordbot/build.gradle.kts create mode 100644 bgsoftware-discordbot/src/main/java/zone/themcgamer/discordbot/BGSoftwareBot.java create mode 100644 bgsoftware-discordbot/src/main/java/zone/themcgamer/discordbot/BotConstants.java create mode 100644 bgsoftware-discordbot/src/main/java/zone/themcgamer/discordbot/command/BaseCommand.java create mode 100644 bgsoftware-discordbot/src/main/java/zone/themcgamer/discordbot/command/impl/AddReactionToMessageCommand.java create mode 100644 bgsoftware-discordbot/src/main/java/zone/themcgamer/discordbot/command/impl/EditMessageCommand.java create mode 100644 bgsoftware-discordbot/src/main/java/zone/themcgamer/discordbot/command/impl/InviteCommand.java create mode 100644 bgsoftware-discordbot/src/main/java/zone/themcgamer/discordbot/command/impl/JsonParseCommand.java create mode 100644 bgsoftware-discordbot/src/main/java/zone/themcgamer/discordbot/command/impl/MemberCountCommand.java create mode 100644 bgsoftware-discordbot/src/main/java/zone/themcgamer/discordbot/command/impl/MessageCommand.java create mode 100644 bgsoftware-discordbot/src/main/java/zone/themcgamer/discordbot/command/impl/PingCommand.java create mode 100644 bgsoftware-discordbot/src/main/java/zone/themcgamer/discordbot/command/impl/SetActivityCommand.java create mode 100644 bgsoftware-discordbot/src/main/java/zone/themcgamer/discordbot/events/GuildListener.java create mode 100644 bgsoftware-discordbot/src/main/java/zone/themcgamer/discordbot/events/HasteBinListener.java create mode 100644 bgsoftware-discordbot/src/main/java/zone/themcgamer/discordbot/events/MainGuildListener.java create mode 100644 bgsoftware-discordbot/src/main/java/zone/themcgamer/discordbot/events/TestGuildListener.java create mode 100644 bgsoftware-discordbot/src/main/java/zone/themcgamer/discordbot/guild/Guild.java create mode 100644 bgsoftware-discordbot/src/main/java/zone/themcgamer/discordbot/utilities/EmbedUtils.java create mode 100644 bgsoftware-discordbot/src/main/java/zone/themcgamer/discordbot/utilities/GuildUtils.java create mode 100644 bgsoftware-discordbot/src/main/java/zone/themcgamer/discordbot/utilities/HasteBinHandler.java create mode 100644 bgsoftware-discordbot/src/main/java/zone/themcgamer/discordbot/utilities/MessageUtils.java create mode 100644 bgsoftware-discordbot/src/main/java/zone/themcgamer/discordbot/utilities/Responses.java create mode 100644 bgsoftware-discordbot/src/main/resources/META-INF/MANIFEST.MF create mode 100644 discordbot/src/main/java/zone/themcgamer/discordbot/command/impl/CommandsCommand.java diff --git a/bgsoftware-discordbot/build.gradle.kts b/bgsoftware-discordbot/build.gradle.kts new file mode 100644 index 0000000..9ad01c7 --- /dev/null +++ b/bgsoftware-discordbot/build.gradle.kts @@ -0,0 +1,26 @@ +dependencies { + implementation(project(":core")) + implementation("net.dv8tion:JDA:4.2.0_228") + implementation("com.jagrosh:jda-utilities:3.0.5") + implementation("javax.json:javax.json-api:1.0") +} + +val jar by tasks.getting(Jar::class) { + manifest { + attributes["Main-Class"] = "zone.themcgamer.discordbot.MGZBot" + } +} + +tasks { + processResources { + val tokens = mapOf("version" to project.version) + from(sourceSets["main"].resources.srcDirs) { + filter("tokens" to tokens) + } + } + + shadowJar { + archiveFileName.set("${project.rootProject.name}-${project.name}-v${project.version}.jar") + destinationDir = file("$rootDir/output") + } +} \ No newline at end of file diff --git a/bgsoftware-discordbot/src/main/java/zone/themcgamer/discordbot/BGSoftwareBot.java b/bgsoftware-discordbot/src/main/java/zone/themcgamer/discordbot/BGSoftwareBot.java new file mode 100644 index 0000000..c046cc9 --- /dev/null +++ b/bgsoftware-discordbot/src/main/java/zone/themcgamer/discordbot/BGSoftwareBot.java @@ -0,0 +1,68 @@ +package zone.themcgamer.discordbot; + +import com.jagrosh.jdautilities.command.CommandClientBuilder; +import lombok.Getter; +import net.dv8tion.jda.api.JDA; +import net.dv8tion.jda.api.JDABuilder; +import net.dv8tion.jda.api.OnlineStatus; +import net.dv8tion.jda.api.entities.Activity; +import net.dv8tion.jda.api.requests.GatewayIntent; +import zone.themcgamer.discordbot.command.impl.*; +import zone.themcgamer.discordbot.events.GuildListener; +import zone.themcgamer.discordbot.events.HasteBinListener; +import zone.themcgamer.discordbot.events.MainGuildListener; + +import javax.security.auth.login.LoginException; +import java.util.concurrent.Executors; + +@Getter +public class BGSoftwareBot { + @Getter private static BGSoftwareBot instance; + + private JDA jda; + + public BGSoftwareBot() { + instance = this; + long time = System.currentTimeMillis(); + + CommandClientBuilder commandClientBuilder = new CommandClientBuilder(); + commandClientBuilder.setPrefix(BotConstants.PREFIX); + commandClientBuilder.setActivity(Activity.playing("BG-Software Harold")); + commandClientBuilder.setStatus(OnlineStatus.ONLINE); + commandClientBuilder.setOwnerId(BotConstants.OWNER_ID); + for (String botAdmin : BotConstants.BOT_ADMINS) + commandClientBuilder.setCoOwnerIds(botAdmin); + commandClientBuilder.useHelpBuilder(false); + + commandClientBuilder.addCommand(new SetActivityCommand()); + commandClientBuilder.addCommand(new InviteCommand()); + commandClientBuilder.addCommand(new MessageCommand()); + commandClientBuilder.addCommand(new EditMessageCommand()); + commandClientBuilder.addCommand(new AddReactionToMessageCommand()); + commandClientBuilder.addCommand(new MemberCountCommand()); + commandClientBuilder.addCommand(new PingCommand()); + + try { + jda = JDABuilder.createDefault(BotConstants.TOKEN) + .setCallbackPool(Executors.newScheduledThreadPool(10)) + .setActivity(Activity.playing("Booting up...")) + .setStatus(OnlineStatus.IDLE) + .enableIntents(GatewayIntent.GUILD_MEMBERS, GatewayIntent.GUILD_EMOJIS) + .addEventListeners( + commandClientBuilder.build(), + new MainGuildListener(this), + new GuildListener(this), + new HasteBinListener()) + .build(); + jda.awaitReady(); + } catch (LoginException | InterruptedException ex) { + ex.printStackTrace(); + } + + System.out.println("Done (" + (System.currentTimeMillis() - time) + ")! For help, type \"help\" or \"?\"\n"); + } + + public static void main(String[] args) { + new BGSoftwareBot(); + } +} \ No newline at end of file diff --git a/bgsoftware-discordbot/src/main/java/zone/themcgamer/discordbot/BotConstants.java b/bgsoftware-discordbot/src/main/java/zone/themcgamer/discordbot/BotConstants.java new file mode 100644 index 0000000..c02a14c --- /dev/null +++ b/bgsoftware-discordbot/src/main/java/zone/themcgamer/discordbot/BotConstants.java @@ -0,0 +1,36 @@ +package zone.themcgamer.discordbot; + +import java.util.Calendar; + +/** + * @author Nicholas + */ +public class BotConstants { + public static final String TOKEN = "ODQ1Njk2NDk0ODA3OTQxMTcx.YKkuPA.g9Xhoc_r-tnlf8sYes9LAIL2vaE"; + public static final String PREFIX = "."; + + public static final String OWNER_ID = "504069946528104471"; // Joel + public static final String[] BOT_ADMINS = new String[] { + "544533281992081408", // Omer_R + }; + + // Default Lines + public static final String COPYRIGHT = "© BG-Software - " + Calendar.getInstance().get(Calendar.YEAR); + + // Channels + public static final String HAROLD_LOG = "845706257779654656"; + public static final String WILDINSPECT = "554282614702211087"; + public static final String WILDBUSTER = "554282630779109377"; + public static final String WILDSTACKER = "554282642980470785"; + public static final String WILDTOOLS = "554282655441747998"; + public static final String WILDCHEST = "554282670272544769"; + public static final String WILDLOADERS = "751895102422777886"; + public static final String SUPERIORSKYBLOCK = "554282577322704896"; + + //Categories + public static final String SUPPORT_CATEGORY = "554281400145281025"; + + //Role IDS + public static final String SUPPORT_TEAM = "818064395636703243"; + +} \ No newline at end of file diff --git a/bgsoftware-discordbot/src/main/java/zone/themcgamer/discordbot/command/BaseCommand.java b/bgsoftware-discordbot/src/main/java/zone/themcgamer/discordbot/command/BaseCommand.java new file mode 100644 index 0000000..4d573f1 --- /dev/null +++ b/bgsoftware-discordbot/src/main/java/zone/themcgamer/discordbot/command/BaseCommand.java @@ -0,0 +1,35 @@ +package zone.themcgamer.discordbot.command; + +import com.jagrosh.jdautilities.command.Command; +import com.jagrosh.jdautilities.command.CommandEvent; +import zone.themcgamer.discordbot.BotConstants; +import zone.themcgamer.discordbot.guild.Guild; +import zone.themcgamer.discordbot.utilities.GuildUtils; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.regex.Pattern; + +/** + * @author Nicholas + */ +public abstract class BaseCommand extends Command { + protected List guilds; // The guilds the command can be executed in + + @Override + protected void execute(CommandEvent event) { + if (!guilds.contains(GuildUtils.matchGuild(event.getGuild().getId()))) + return; + List args = new ArrayList<>(); + if (event.getArgs() != null && event.getArgs().length() > 0) { + String[] split = event.getMessage().getContentRaw() + .replaceFirst("(?i)" + Pattern.quote(BotConstants.PREFIX), "") + .split("\\s+"); + args = Arrays.asList(split); + } + execute(event, args); + } + + protected abstract void execute(CommandEvent event, List args); +} \ No newline at end of file diff --git a/bgsoftware-discordbot/src/main/java/zone/themcgamer/discordbot/command/impl/AddReactionToMessageCommand.java b/bgsoftware-discordbot/src/main/java/zone/themcgamer/discordbot/command/impl/AddReactionToMessageCommand.java new file mode 100644 index 0000000..a6e928a --- /dev/null +++ b/bgsoftware-discordbot/src/main/java/zone/themcgamer/discordbot/command/impl/AddReactionToMessageCommand.java @@ -0,0 +1,48 @@ +package zone.themcgamer.discordbot.command.impl; + +import com.jagrosh.jdautilities.command.CommandEvent; +import net.dv8tion.jda.api.EmbedBuilder; +import net.dv8tion.jda.api.Permission; +import net.dv8tion.jda.api.entities.TextChannel; +import zone.themcgamer.discordbot.BGSoftwareBot; +import zone.themcgamer.discordbot.command.BaseCommand; +import zone.themcgamer.discordbot.guild.Guild; +import zone.themcgamer.discordbot.utilities.EmbedUtils; +import zone.themcgamer.discordbot.utilities.MessageUtils; + +import java.util.Arrays; +import java.util.List; + +public class AddReactionToMessageCommand extends BaseCommand { + public AddReactionToMessageCommand() { + name = "addreaction"; + aliases = new String[] { "react" }; + help = "Edit a message from the bot."; + arguments = " "; + userPermissions = new Permission[] { Permission.ADMINISTRATOR }; + guildOnly = true; + guilds = Arrays.asList(Guild.MAIN, Guild.TEST); + } + + @Override + protected void execute(CommandEvent event, List args) { + if (args.size() < 3) { + MessageUtils.sendUsageMessage(event.getTextChannel(),this); + return; + } + + TextChannel textChannelById = BGSoftwareBot.getInstance().getJda().getTextChannelById(args.get(1)); + if (textChannelById == null) { + event.reply("Channel does not exist!"); + return; + } + + textChannelById.addReactionById(args.get(2), args.get(3)).queue(message -> { + + }, error -> { + EmbedBuilder embedBuilder = EmbedUtils.errorEmbed(); + embedBuilder.setDescription(error.getLocalizedMessage()); + event.reply(embedBuilder.build()); + }); + } +} \ No newline at end of file diff --git a/bgsoftware-discordbot/src/main/java/zone/themcgamer/discordbot/command/impl/EditMessageCommand.java b/bgsoftware-discordbot/src/main/java/zone/themcgamer/discordbot/command/impl/EditMessageCommand.java new file mode 100644 index 0000000..7da8fb9 --- /dev/null +++ b/bgsoftware-discordbot/src/main/java/zone/themcgamer/discordbot/command/impl/EditMessageCommand.java @@ -0,0 +1,53 @@ +package zone.themcgamer.discordbot.command.impl; + +import com.jagrosh.jdautilities.command.CommandEvent; +import net.dv8tion.jda.api.EmbedBuilder; +import net.dv8tion.jda.api.Permission; +import net.dv8tion.jda.api.entities.TextChannel; +import zone.themcgamer.discordbot.BGSoftwareBot; +import zone.themcgamer.discordbot.command.BaseCommand; +import zone.themcgamer.discordbot.guild.Guild; +import zone.themcgamer.discordbot.utilities.EmbedUtils; +import zone.themcgamer.discordbot.utilities.MessageUtils; + +import java.util.Arrays; +import java.util.List; + +public class EditMessageCommand extends BaseCommand { + public EditMessageCommand() { + name = "edit"; + aliases = new String[] { "editmessage" }; + help = "Edit a message from the bot."; + arguments = " <description>"; + userPermissions = new Permission[] { Permission.ADMINISTRATOR }; + guildOnly = true; + guilds = Arrays.asList(Guild.MAIN, Guild.TEST); + } + + @Override + protected void execute(CommandEvent event, List<String> args) { + if (args.size() < 3) { + MessageUtils.sendUsageMessage(event.getTextChannel(), this); + return; + } + + TextChannel textChannelById = BGSoftwareBot.getInstance().getJda().getTextChannelById(args.get(1)); + if (textChannelById == null) { + event.reply("Channel does not exist!"); + return; + } + + EmbedBuilder embedBuilder = EmbedUtils.defaultEmbed(); + embedBuilder.setTitle(args.get(3).replace("_", " ")); + embedBuilder.setDescription(event.getMessage().getContentRaw() + .replace("." + args.get(0), "") + .replace(args.get(1), "") + .replace(args.get(2), "") + .replace(args.get(3), "")); + textChannelById.editMessageById(args.get(2), embedBuilder.build()).queue(message -> { + event.replySuccess("Message has been edited!"); + }, error -> { + event.reply("Message with this ID does not exist, are you sure this is the right id?"); + }); + } +} \ No newline at end of file diff --git a/bgsoftware-discordbot/src/main/java/zone/themcgamer/discordbot/command/impl/InviteCommand.java b/bgsoftware-discordbot/src/main/java/zone/themcgamer/discordbot/command/impl/InviteCommand.java new file mode 100644 index 0000000..3df10aa --- /dev/null +++ b/bgsoftware-discordbot/src/main/java/zone/themcgamer/discordbot/command/impl/InviteCommand.java @@ -0,0 +1,42 @@ +package zone.themcgamer.discordbot.command.impl; + +import com.jagrosh.jdautilities.command.CommandEvent; +import net.dv8tion.jda.api.entities.Guild; +import net.dv8tion.jda.api.entities.TextChannel; +import zone.themcgamer.discordbot.command.BaseCommand; + +import java.util.Collections; +import java.util.List; +import java.util.concurrent.TimeUnit; + +public class InviteCommand extends BaseCommand { + + public InviteCommand() { + name = "invite"; + aliases = new String[]{"createinvite"}; + help = "Create invite link via the bot"; + guildOnly = true; + guilds = Collections.singletonList(zone.themcgamer.discordbot.guild.Guild.MAIN); + } + + @Override + protected void execute(CommandEvent event, List<String> args) { + Guild guild = event.getGuild(); + + TextChannel textChannelById = guild.getTextChannelById(791015530001596456L); + if (textChannelById == null) + return; + textChannelById.createInvite() + .timeout(1, TimeUnit.DAYS) + .setTemporary(true).queue(inviteLink -> { + event.getMember().getUser().openPrivateChannel().queue(privateChannel -> { + privateChannel.sendMessage("I have generated an invite link for you! This invite link will work for 24 hours! " + inviteLink.getUrl()).queue(); + event.reply("Check your dm's!"); + }, error -> { + event.replyError("Could not sent you a dm!"); + }); }, error -> { + event.replyError("Coulnd't create an invite link due an error!"); + }); + + } +} \ No newline at end of file diff --git a/bgsoftware-discordbot/src/main/java/zone/themcgamer/discordbot/command/impl/JsonParseCommand.java b/bgsoftware-discordbot/src/main/java/zone/themcgamer/discordbot/command/impl/JsonParseCommand.java new file mode 100644 index 0000000..0c7e617 --- /dev/null +++ b/bgsoftware-discordbot/src/main/java/zone/themcgamer/discordbot/command/impl/JsonParseCommand.java @@ -0,0 +1,26 @@ +package zone.themcgamer.discordbot.command.impl; + +import com.jagrosh.jdautilities.command.CommandEvent; +import zone.themcgamer.discordbot.command.BaseCommand; +import zone.themcgamer.discordbot.guild.Guild; +import zone.themcgamer.discordbot.utilities.MessageUtils; + +import java.util.Arrays; +import java.util.List; + +public class JsonParseCommand extends BaseCommand { + public JsonParseCommand() { + name = "jsonparse"; + help = "Parse your json!"; + arguments = "<json>"; + guildOnly = true; + guilds = Arrays.asList(Guild.MAIN, Guild.TEST); + } + + @Override + protected void execute(CommandEvent event, List<String> args) { + if (args.size() < 1) { + MessageUtils.sendUsageMessage(event.getTextChannel(), this); + } + } +} diff --git a/bgsoftware-discordbot/src/main/java/zone/themcgamer/discordbot/command/impl/MemberCountCommand.java b/bgsoftware-discordbot/src/main/java/zone/themcgamer/discordbot/command/impl/MemberCountCommand.java new file mode 100644 index 0000000..bcd0e1d --- /dev/null +++ b/bgsoftware-discordbot/src/main/java/zone/themcgamer/discordbot/command/impl/MemberCountCommand.java @@ -0,0 +1,37 @@ +package zone.themcgamer.discordbot.command.impl; + +import com.jagrosh.jdautilities.command.CommandEvent; +import net.dv8tion.jda.api.EmbedBuilder; +import zone.themcgamer.discordbot.command.BaseCommand; +import zone.themcgamer.discordbot.guild.Guild; +import zone.themcgamer.discordbot.utilities.EmbedUtils; + +import java.util.Arrays; +import java.util.List; + +public class MemberCountCommand extends BaseCommand { + + public MemberCountCommand() { + name = "membercount"; + aliases = new String[]{"members"}; + help = "Shows the amount of members in the guild"; + cooldown = 10; + arguments = ""; + guildOnly = true; + guilds = Arrays.asList(Guild.MAIN, Guild.TEST); + } + + @Override + protected void execute(CommandEvent event, List<String> args) { + net.dv8tion.jda.api.entities.Guild guild = event.getGuild(); + guild.loadMembers().onSuccess(members -> { + EmbedBuilder embedBuilder = EmbedUtils.defaultEmbed(); + embedBuilder.setTitle("Member Count"); + embedBuilder.addField("Humans", String.valueOf(members.stream().filter(member -> !member.getUser().isBot()).count()), true); + embedBuilder.addField("Bots", String.valueOf(members.stream().filter(member -> member.getUser().isBot()).count()), true); + + event.getChannel().sendMessage(embedBuilder.build()).queue(); + }); + } +} + diff --git a/bgsoftware-discordbot/src/main/java/zone/themcgamer/discordbot/command/impl/MessageCommand.java b/bgsoftware-discordbot/src/main/java/zone/themcgamer/discordbot/command/impl/MessageCommand.java new file mode 100644 index 0000000..36c976a --- /dev/null +++ b/bgsoftware-discordbot/src/main/java/zone/themcgamer/discordbot/command/impl/MessageCommand.java @@ -0,0 +1,38 @@ +package zone.themcgamer.discordbot.command.impl; + +import com.jagrosh.jdautilities.command.CommandEvent; +import net.dv8tion.jda.api.EmbedBuilder; +import net.dv8tion.jda.api.Permission; +import zone.themcgamer.discordbot.command.BaseCommand; +import zone.themcgamer.discordbot.guild.Guild; +import zone.themcgamer.discordbot.utilities.EmbedUtils; +import zone.themcgamer.discordbot.utilities.MessageUtils; + +import java.util.Arrays; +import java.util.List; + +public class MessageCommand extends BaseCommand { + public MessageCommand() { + name = "message"; + aliases = new String[] { "say" }; + help = "Announce something in an embed format."; + arguments = "<title> <description>"; + userPermissions = new Permission[] { Permission.ADMINISTRATOR }; + guildOnly = true; + guilds = Arrays.asList(Guild.MAIN, Guild.TEST); + } + + @Override + protected void execute(CommandEvent event, List<String> args) { + if (args.size() < 1) { + MessageUtils.sendUsageMessage(event.getTextChannel(),this); + return; + } + + //TODO a way to add images, and such to the embeds. + EmbedBuilder embedBuilder = EmbedUtils.defaultEmbed(); + embedBuilder.setTitle(args.get(1).replace("_", " ")); + embedBuilder.setDescription(event.getMessage().getContentRaw().replace(args.get(1), "").replace("." + args.get(0), "")); + event.getChannel().sendMessage(embedBuilder.build()).queue(); + } +} \ No newline at end of file diff --git a/bgsoftware-discordbot/src/main/java/zone/themcgamer/discordbot/command/impl/PingCommand.java b/bgsoftware-discordbot/src/main/java/zone/themcgamer/discordbot/command/impl/PingCommand.java new file mode 100644 index 0000000..b57970f --- /dev/null +++ b/bgsoftware-discordbot/src/main/java/zone/themcgamer/discordbot/command/impl/PingCommand.java @@ -0,0 +1,50 @@ +package zone.themcgamer.discordbot.command.impl; + +import com.jagrosh.jdautilities.command.CommandEvent; +import zone.themcgamer.discordbot.command.BaseCommand; +import zone.themcgamer.discordbot.guild.Guild; +import zone.themcgamer.discordbot.utilities.EmbedUtils; + +import java.awt.*; +import java.util.Arrays; +import java.util.Date; +import java.util.List; + +public class PingCommand extends BaseCommand { + + private static long inputTime; + + public static void setInputTime(long inputTimeLong) { + inputTime = inputTimeLong; + } + + private Color getColorByPing(long ping) { + if (ping < 100) + return Color.cyan; + if (ping < 400) + return Color.green; + if (ping < 700) + return Color.yellow; + if (ping < 1000) + return Color.orange; + return Color.red; + } + + public PingCommand() { + name = "ping"; + aliases = new String[]{"latency"}; + help = "Get the latency of the bot to the guild."; + guildOnly = true; + guilds = Arrays.asList(Guild.MAIN, Guild.TEST); + } + + @Override + protected void execute(CommandEvent event, List<String> args) { + long processing = new Date().getTime() - inputTime; + long ping = event.getJDA().getGatewayPing(); + event.getTextChannel().sendMessage(EmbedUtils.defaultEmbed().setColor(getColorByPing(ping)).setDescription( + String.format(":ping_pong: **Pong!**\n\nThe bot took `%s` milliseconds to response.\nIt took `%s` milliseconds to parse the command and the ping is `%s` milliseconds.", + processing + ping, processing, ping) + ).build()).queue(); + } +} diff --git a/bgsoftware-discordbot/src/main/java/zone/themcgamer/discordbot/command/impl/SetActivityCommand.java b/bgsoftware-discordbot/src/main/java/zone/themcgamer/discordbot/command/impl/SetActivityCommand.java new file mode 100644 index 0000000..ee15848 --- /dev/null +++ b/bgsoftware-discordbot/src/main/java/zone/themcgamer/discordbot/command/impl/SetActivityCommand.java @@ -0,0 +1,44 @@ +package zone.themcgamer.discordbot.command.impl; + +import com.jagrosh.jdautilities.command.CommandEvent; +import net.dv8tion.jda.api.Permission; +import net.dv8tion.jda.api.entities.Activity; +import zone.themcgamer.discordbot.BGSoftwareBot; +import zone.themcgamer.discordbot.command.BaseCommand; +import zone.themcgamer.discordbot.guild.Guild; +import zone.themcgamer.discordbot.utilities.EmbedUtils; +import zone.themcgamer.discordbot.utilities.MessageUtils; + +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +/** + * @author Nicholas + */ +public class SetActivityCommand extends BaseCommand { + public SetActivityCommand() { + name = "setactivity"; + help = "Set the bot activity."; + arguments = "<message>"; + userPermissions = new Permission[] { Permission.ADMINISTRATOR }; + guildOnly = true; + guilds = Arrays.asList(Guild.MAIN, Guild.TEST); + } + + @Override + protected void execute(CommandEvent event, List<String> args) { + if (args.size() < 1) { + MessageUtils.sendUsageMessage(event.getTextChannel(),this); + return; + } + String activity = args.stream().skip(1).collect(Collectors.joining(" ")); + BGSoftwareBot.getInstance().getJda().getPresence().setActivity(Activity.playing(activity)); + event.getChannel().sendMessage(EmbedUtils.successEmbed() + .setThumbnail(event.getAuthor().getAvatarUrl()) + .setTitle("Activity updated!") + .appendDescription(event.getAuthor().getAsTag() + " updated the bot activity to \"" + activity + "\".") + .build() + ).queue(); + } +} \ No newline at end of file diff --git a/bgsoftware-discordbot/src/main/java/zone/themcgamer/discordbot/events/GuildListener.java b/bgsoftware-discordbot/src/main/java/zone/themcgamer/discordbot/events/GuildListener.java new file mode 100644 index 0000000..e848ec7 --- /dev/null +++ b/bgsoftware-discordbot/src/main/java/zone/themcgamer/discordbot/events/GuildListener.java @@ -0,0 +1,55 @@ +package zone.themcgamer.discordbot.events; + +import lombok.RequiredArgsConstructor; +import net.dv8tion.jda.api.EmbedBuilder; +import net.dv8tion.jda.api.entities.Category; +import net.dv8tion.jda.api.entities.Member; +import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent; +import net.dv8tion.jda.api.hooks.ListenerAdapter; +import zone.themcgamer.discordbot.BGSoftwareBot; +import zone.themcgamer.discordbot.BotConstants; +import zone.themcgamer.discordbot.utilities.EmbedUtils; +import zone.themcgamer.discordbot.utilities.GuildUtils; +import zone.themcgamer.discordbot.utilities.Responses; + +import javax.annotation.Nonnull; +import java.util.Arrays; + +@RequiredArgsConstructor +public class GuildListener extends ListenerAdapter { + private final BGSoftwareBot main; + + @Override + public void onGuildMessageReceived(@Nonnull GuildMessageReceivedEvent event) { + Member member = event.getMember(); + String contentRaw = event.getMessage().getContentRaw().toLowerCase(); + String name = event.getChannel().getName(); + + if (member == null) + return; + if (member.getUser().isBot()) + return; + if (GuildUtils.hasRole(member, BotConstants.SUPPORT_TEAM) == null) + return; + + for (Responses value : Responses.values()) { + if (Arrays.asList(value.getTriggerWords()).contains(contentRaw)) { + if (!value.isRequireHelpCategory()) + return; + if (!value.getRequiredChannel().equals(event.getChannel().getId())) + return; + EmbedBuilder embedBuilder = EmbedUtils.defaultEmbed(); + embedBuilder.setTitle(value.getTitle().replace("{plugin}", (isSupportCategory(event.getMessage().getCategory()) ? name : value.getDefaultReplace()))); + embedBuilder.setDescription(value.getDescription().replace("{plugin}", (isSupportCategory(event.getMessage().getCategory()) ? name : value.getDefaultReplace()))); + event.getMessage().reply(embedBuilder.build()).queue(); + break; + } + } + } + + protected boolean isSupportCategory(Category category) { + if (category == null) + return false; + return (category.getId().equals(BotConstants.SUPPORT_CATEGORY)); + } +} diff --git a/bgsoftware-discordbot/src/main/java/zone/themcgamer/discordbot/events/HasteBinListener.java b/bgsoftware-discordbot/src/main/java/zone/themcgamer/discordbot/events/HasteBinListener.java new file mode 100644 index 0000000..ae87c4a --- /dev/null +++ b/bgsoftware-discordbot/src/main/java/zone/themcgamer/discordbot/events/HasteBinListener.java @@ -0,0 +1,39 @@ +package zone.themcgamer.discordbot.events; + +import lombok.RequiredArgsConstructor; +import net.dv8tion.jda.api.EmbedBuilder; +import net.dv8tion.jda.api.entities.Member; +import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent; +import net.dv8tion.jda.api.hooks.ListenerAdapter; +import zone.themcgamer.discordbot.utilities.EmbedUtils; +import zone.themcgamer.discordbot.utilities.HasteBinHandler; + +import javax.annotation.Nonnull; +import java.io.IOException; + +@RequiredArgsConstructor +public class HasteBinListener extends ListenerAdapter { + + @Override + public void onGuildMessageReceived(@Nonnull GuildMessageReceivedEvent event) { + Member member = event.getMember(); + if (member == null) + return; + if (member.getUser().isBot()) + return; + + if (event.getMessage().getContentRaw().contains("java.lang.")) { + HasteBinHandler hasteBinHandler = new HasteBinHandler(); + try { + String url = hasteBinHandler.post(event.getMessage().getContentDisplay(), false); + EmbedBuilder embedBuilder = EmbedUtils.warnEmbed(); + embedBuilder.setTitle("Hastebin"); + embedBuilder.setDescription("I've noticed you pasted an error, this error has been pasted in hastebin for you!\n" + + "Please use this link and report it at github!"); + embedBuilder.addField("Error", url, false); + } catch (IOException exception) { + exception.printStackTrace(); + } + } + } +} diff --git a/bgsoftware-discordbot/src/main/java/zone/themcgamer/discordbot/events/MainGuildListener.java b/bgsoftware-discordbot/src/main/java/zone/themcgamer/discordbot/events/MainGuildListener.java new file mode 100644 index 0000000..59f2305 --- /dev/null +++ b/bgsoftware-discordbot/src/main/java/zone/themcgamer/discordbot/events/MainGuildListener.java @@ -0,0 +1,28 @@ +package zone.themcgamer.discordbot.events; + +import lombok.RequiredArgsConstructor; +import net.dv8tion.jda.api.entities.Guild; +import net.dv8tion.jda.api.entities.Member; +import net.dv8tion.jda.api.entities.User; +import net.dv8tion.jda.api.events.guild.member.GuildMemberJoinEvent; +import net.dv8tion.jda.api.hooks.ListenerAdapter; +import zone.themcgamer.discordbot.BGSoftwareBot; + +import javax.annotation.Nonnull; + +@RequiredArgsConstructor +public class MainGuildListener extends ListenerAdapter { + private final BGSoftwareBot mgzBot; + + @Override + public void onGuildMemberJoin(@Nonnull GuildMemberJoinEvent event) { + Member member = event.getMember(); + User user = event.getUser(); + Guild guild = event.getGuild(); + if (user.isBot()) + return; + if (!guild.getId().equals(zone.themcgamer.discordbot.guild.Guild.MAIN.getGuildId())) + return; + + } +} \ No newline at end of file diff --git a/bgsoftware-discordbot/src/main/java/zone/themcgamer/discordbot/events/TestGuildListener.java b/bgsoftware-discordbot/src/main/java/zone/themcgamer/discordbot/events/TestGuildListener.java new file mode 100644 index 0000000..a3ddee0 --- /dev/null +++ b/bgsoftware-discordbot/src/main/java/zone/themcgamer/discordbot/events/TestGuildListener.java @@ -0,0 +1,4 @@ +package zone.themcgamer.discordbot.events; + +public class TestGuildListener { +} diff --git a/bgsoftware-discordbot/src/main/java/zone/themcgamer/discordbot/guild/Guild.java b/bgsoftware-discordbot/src/main/java/zone/themcgamer/discordbot/guild/Guild.java new file mode 100644 index 0000000..7cec50c --- /dev/null +++ b/bgsoftware-discordbot/src/main/java/zone/themcgamer/discordbot/guild/Guild.java @@ -0,0 +1,15 @@ +package zone.themcgamer.discordbot.guild; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +/** + * @author Nicholas + */ +@AllArgsConstructor @Getter +public enum Guild { + MAIN("554276823010246687"), + TEST("845696905496363078"); + + private final String guildId; +} \ No newline at end of file diff --git a/bgsoftware-discordbot/src/main/java/zone/themcgamer/discordbot/utilities/EmbedUtils.java b/bgsoftware-discordbot/src/main/java/zone/themcgamer/discordbot/utilities/EmbedUtils.java new file mode 100644 index 0000000..92cbb3e --- /dev/null +++ b/bgsoftware-discordbot/src/main/java/zone/themcgamer/discordbot/utilities/EmbedUtils.java @@ -0,0 +1,30 @@ +package zone.themcgamer.discordbot.utilities; + +import net.dv8tion.jda.api.EmbedBuilder; +import zone.themcgamer.discordbot.BGSoftwareBot; +import zone.themcgamer.discordbot.BotConstants; + +import java.awt.*; +import java.time.LocalDateTime; + +public class EmbedUtils { + public static EmbedBuilder successEmbed() { + return defaultEmbed().setColor(Color.decode("#41bc7f")); + } + + public static EmbedBuilder errorEmbed() { + return defaultEmbed().setTitle("Oops!") + .setColor(Color.decode("#d74742")); + } + + public static EmbedBuilder warnEmbed() { + return defaultEmbed().setColor(Color.decode("#f85b2e")); + } + + public static EmbedBuilder defaultEmbed() { + return new EmbedBuilder() + .setColor(Color.decode("#1DD0D5")) + .setTimestamp(LocalDateTime.now()) + .setFooter(BotConstants.COPYRIGHT, BGSoftwareBot.getInstance().getJda().getSelfUser().getEffectiveAvatarUrl()); + } +} \ No newline at end of file diff --git a/bgsoftware-discordbot/src/main/java/zone/themcgamer/discordbot/utilities/GuildUtils.java b/bgsoftware-discordbot/src/main/java/zone/themcgamer/discordbot/utilities/GuildUtils.java new file mode 100644 index 0000000..0a491be --- /dev/null +++ b/bgsoftware-discordbot/src/main/java/zone/themcgamer/discordbot/utilities/GuildUtils.java @@ -0,0 +1,25 @@ +package zone.themcgamer.discordbot.utilities; + +import net.dv8tion.jda.api.entities.Member; +import net.dv8tion.jda.api.entities.Role; +import zone.themcgamer.discordbot.guild.Guild; + +import java.util.Arrays; +import java.util.List; + +/** + * @author JohannesHQ + */ +public class GuildUtils { + public static Guild matchGuild(String guildId) { + return Arrays.stream(Guild.values()).filter(guild -> guild.getGuildId().equals(guildId)).findFirst().orElse(null); + } + + public static Role hasRole(Member member, String id) { + List<Role> roles = member.getRoles(); + return roles.stream() + .filter(role -> role.getId().equals(id)) // filter by role name + .findFirst() // take first result + .orElse(null); // else return null + } +} \ No newline at end of file diff --git a/bgsoftware-discordbot/src/main/java/zone/themcgamer/discordbot/utilities/HasteBinHandler.java b/bgsoftware-discordbot/src/main/java/zone/themcgamer/discordbot/utilities/HasteBinHandler.java new file mode 100644 index 0000000..d299193 --- /dev/null +++ b/bgsoftware-discordbot/src/main/java/zone/themcgamer/discordbot/utilities/HasteBinHandler.java @@ -0,0 +1,46 @@ +package zone.themcgamer.discordbot.utilities; + +import javax.net.ssl.HttpsURLConnection; +import java.io.BufferedReader; +import java.io.DataOutputStream; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.URL; +import java.nio.charset.StandardCharsets; + +public class HasteBinHandler { + public String post(String text, boolean raw) throws IOException { + byte[] postData = text.getBytes(StandardCharsets.UTF_8); + int postDataLength = postData.length; + + String requestURL = "https://hastebin.com/documents/"; + URL url = new URL(requestURL); + HttpsURLConnection conn = (HttpsURLConnection) url.openConnection(); + conn.setDoOutput(true); + conn.setInstanceFollowRedirects(false); + conn.setRequestMethod("POST"); + conn.setRequestProperty("User-Agent", "Hastebin Java Api"); + conn.setRequestProperty("Content-Length", Integer.toString(postDataLength)); + conn.setUseCaches(false); + + String response = null; + DataOutputStream wr; + try { + wr = new DataOutputStream(conn.getOutputStream()); + wr.write(postData); + BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); + response = reader.readLine(); + } catch (IOException e) { + e.printStackTrace(); + } + + if (response.contains("\"key\"")) { + response = response.substring(response.indexOf(":") + 2, response.length() - 2); + + String postURL = raw ? "https://hastebin.com/raw/" : "https://hastebin.com/"; + response = postURL + response; + } + + return response; + } +} diff --git a/bgsoftware-discordbot/src/main/java/zone/themcgamer/discordbot/utilities/MessageUtils.java b/bgsoftware-discordbot/src/main/java/zone/themcgamer/discordbot/utilities/MessageUtils.java new file mode 100644 index 0000000..2661398 --- /dev/null +++ b/bgsoftware-discordbot/src/main/java/zone/themcgamer/discordbot/utilities/MessageUtils.java @@ -0,0 +1,39 @@ +package zone.themcgamer.discordbot.utilities; + +import net.dv8tion.jda.api.EmbedBuilder; +import net.dv8tion.jda.api.entities.Message; +import net.dv8tion.jda.api.entities.TextChannel; +import zone.themcgamer.discordbot.BGSoftwareBot; +import zone.themcgamer.discordbot.BotConstants; +import zone.themcgamer.discordbot.command.BaseCommand; + +import java.util.Objects; + +public class MessageUtils { + public static void sendUsageMessage(TextChannel textChannel, BaseCommand command) { + textChannel.sendMessage(EmbedUtils.errorEmbed() + .appendDescription("Usage: " + BotConstants.PREFIX + command.getName() + " " + command.getArguments()) + .build() + ).queue(); + } + + public static void sendLogMessage(Message message) { + Objects.requireNonNull(getLogChannel()).sendMessage(message).queue(); + } + + public static void sendLogMessage(EmbedBuilder embedBuilder) { + Objects.requireNonNull(getLogChannel()).sendMessage(embedBuilder.build()).queue(); + } + + private static TextChannel getLogChannel() { + TextChannel logChannel = BGSoftwareBot.getInstance().getJda().getTextChannelById(BotConstants.HAROLD_LOG); + if (logChannel == null) { + Objects.requireNonNull(BGSoftwareBot.getInstance().getJda().getUserById("504069946528104471")) + .openPrivateChannel().queue(privateChannel -> + privateChannel.sendMessage("There was an error while sending a log message, the channel id is " + + "invalid or does not exist.").queue()); + return null; + } + return logChannel; + } +} \ No newline at end of file diff --git a/bgsoftware-discordbot/src/main/java/zone/themcgamer/discordbot/utilities/Responses.java b/bgsoftware-discordbot/src/main/java/zone/themcgamer/discordbot/utilities/Responses.java new file mode 100644 index 0000000..84c9faa --- /dev/null +++ b/bgsoftware-discordbot/src/main/java/zone/themcgamer/discordbot/utilities/Responses.java @@ -0,0 +1,87 @@ +package zone.themcgamer.discordbot.utilities; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import zone.themcgamer.discordbot.BotConstants; + +@AllArgsConstructor +@Getter +public enum Responses { + PURCHASE("Purchase", + new String[] {"purchase", "buy"}, + "Premium plugins at no cost!", + "Since 1st January, 2021, " + + "{plugin} is free, with all the features included to make sure you get the best quality for no cost!\u200B\n"+ + "You can download the plugin [here](https://bg-software.com/{plugin})", + "Are all plugins", + false, + null), + ERROR("error", + new String[] {"java.lang", "org.bukkit.event", "error occurred while enabling", "error"}, + "No errors!", + "Please do not paste stack-traces and or errors here!\n" + + "please report them at our [github](https://github.com/BG-Software-LLC)!", + null, + true, + null), + COMMANDS("commands", + new String[] {"commands", "cmds"}, + "{plugin} - Commands", + "You can find the list of commands [here](https://wiki.bg-software.com/#/{plugin}/?id=commands)", + "not found", + true, + null), + DISABLE("Disable Commands", + new String[] {"disable command", "permissions"}, + "How to disable a command?", + "All commands are based on permissions. If you want to disable or enable a command just give them the permission or not. You can find all permissions at the wiki of the plugin!\n" + + "You can find the permissions [here](https://wiki.bg-software.com/#/{plugin}/?id=permissions)", + "not found", + true, + null), + WIKI("Wiki", + new String[] {"wiki", "about"}, + "{plugin} - Wiki", + "[Click here](https://bg-software.com/{plugin}/) for the wiki of the plugin {plugin}", + "not found", + true, + null), + PLACEHOLDERS("Placeholders", + new String[] {"placeholders"}, + "{plugin} - Placeholders", + "You can find all placeholders of this plugin [here](https://wiki.bg-software.com/#/{plugin}/?id=placeholders)", + "not found", + true, + null), + WILDSTACKER_LOOT_TABLES("Loot Tables", + new String[] {"loot tables", "loot"}, + "Loot Tables", + "Loot tables are used to store all the loot data of entities.\n" + + "They cannot be disabled, and they are used to calculate loot faster.\n" + + "They can be changed however you want, as long as you follow the formatting rules.\n" + + "\n" + + "Every file is represented as a \"loot table\". Loot tables contain global settings and pairs.\n" + + "Pairs contain the items, and can be manipulated differently to get different results.\n" + + "\n" + + "[Click Here](https://wiki.bg-software.com/#/wildstacker/loot-tables/) for more information!", + "not found", + true, + BotConstants.WILDSTACKER), + NOT_SAVING("No data saving", + new String[] {"not saving"}, + "Is your settings not saving?", + "If you're using `/{plugin} settings` and you changed settings, make sure to go back to the main menu" + + "and click the `Save Settings` button.", + "pluginName", + false, + null); + + private final String name; + private final String[] triggerWords; + private final String title; + private final String description; + private final String defaultReplace; + private final boolean requireHelpCategory; + private final String requiredChannel; + +} diff --git a/bgsoftware-discordbot/src/main/resources/META-INF/MANIFEST.MF b/bgsoftware-discordbot/src/main/resources/META-INF/MANIFEST.MF new file mode 100644 index 0000000..f3a4463 --- /dev/null +++ b/bgsoftware-discordbot/src/main/resources/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Main-Class: zone.themcgamer.discordbot.MGZBot + diff --git a/discordbot/src/main/java/zone/themcgamer/discordbot/MGZBot.java b/discordbot/src/main/java/zone/themcgamer/discordbot/MGZBot.java index 78b5416..f0c6392 100644 --- a/discordbot/src/main/java/zone/themcgamer/discordbot/MGZBot.java +++ b/discordbot/src/main/java/zone/themcgamer/discordbot/MGZBot.java @@ -10,6 +10,7 @@ import net.dv8tion.jda.api.requests.GatewayIntent; import zone.themcgamer.discordbot.command.impl.*; import zone.themcgamer.discordbot.events.GuildsListener; import zone.themcgamer.discordbot.events.MainGuildListener; +import zone.themcgamer.discordbot.utilities.EmbedUtils; import javax.security.auth.login.LoginException; import java.util.concurrent.Executors; diff --git a/discordbot/src/main/java/zone/themcgamer/discordbot/command/impl/CommandsCommand.java b/discordbot/src/main/java/zone/themcgamer/discordbot/command/impl/CommandsCommand.java new file mode 100644 index 0000000..446e6ad --- /dev/null +++ b/discordbot/src/main/java/zone/themcgamer/discordbot/command/impl/CommandsCommand.java @@ -0,0 +1,35 @@ +package zone.themcgamer.discordbot.command.impl; + +import com.jagrosh.jdautilities.command.Command; +import com.jagrosh.jdautilities.command.CommandClientBuilder; +import com.jagrosh.jdautilities.command.CommandEvent; +import net.dv8tion.jda.api.EmbedBuilder; +import net.dv8tion.jda.api.entities.TextChannel; +import zone.themcgamer.core.command.CommandManager; +import zone.themcgamer.discordbot.BotConstants; +import zone.themcgamer.discordbot.MGZBot; +import zone.themcgamer.discordbot.command.BaseCommand; +import zone.themcgamer.discordbot.guild.Guild; +import zone.themcgamer.discordbot.utilities.EmbedUtils; +import zone.themcgamer.discordbot.utilities.MessageUtils; + +import java.util.Collections; +import java.util.List; +import java.util.stream.Collectors; + +/** + * @author Nicholas + */ +public class CommandsCommand extends BaseCommand { + public CommandsCommand() { + name = "commands"; + aliases = new String[] { "help" }; + help = "List of commands!"; + guildOnly = true; + } + + @Override + protected void execute(CommandEvent event, List<String> args) { + EmbedBuilder embedBuilder = EmbedUtils.defaultEmbed(); + } +} \ No newline at end of file diff --git a/settings.gradle.kts b/settings.gradle.kts index 2041e36..2a7f450 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -13,4 +13,5 @@ include("hub") include("arcade") include("skyblock") include("discordbot") -include("testing") \ No newline at end of file +include("testing") +include("bgsoftware-discordbot")