From 01cf6fdb586b2b962bf791aebc4e539903e5e235 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Wed, 28 Jul 2021 15:06:40 +0200 Subject: [PATCH 01/37] Add DiscordBot --- pom.xml | 39 ++++++++++ src/de/steamwar/bungeecore/BungeeCore.java | 4 ++ .../bungeecore/bot/SteamwarDiscordBot.java | 70 ++++++++++++++++++ .../bungeecore/bot/config/DiscordRole.java | 39 ++++++++++ .../bot/config/DiscordRulesLink.java | 36 ++++++++++ .../bot/config/SteamwarDiscordBotConfig.java | 71 +++++++++++++++++++ .../bot/listeners/BasicDiscordListener.java | 30 ++++++++ .../RolesInteractionButtonListener.java | 41 +++++++++++ .../bot/util/DiscordRolesMessage.java | 51 +++++++++++++ .../bot/util/DiscordRulesMessage.java | 57 +++++++++++++++ 10 files changed, 438 insertions(+) create mode 100644 src/de/steamwar/bungeecore/bot/SteamwarDiscordBot.java create mode 100644 src/de/steamwar/bungeecore/bot/config/DiscordRole.java create mode 100644 src/de/steamwar/bungeecore/bot/config/DiscordRulesLink.java create mode 100644 src/de/steamwar/bungeecore/bot/config/SteamwarDiscordBotConfig.java create mode 100644 src/de/steamwar/bungeecore/bot/listeners/BasicDiscordListener.java create mode 100644 src/de/steamwar/bungeecore/bot/listeners/RolesInteractionButtonListener.java create mode 100644 src/de/steamwar/bungeecore/bot/util/DiscordRolesMessage.java create mode 100644 src/de/steamwar/bungeecore/bot/util/DiscordRulesMessage.java diff --git a/pom.xml b/pom.xml index dbf27603..8bb101a0 100644 --- a/pom.xml +++ b/pom.xml @@ -35,10 +35,31 @@ 8 + + org.apache.maven.plugins + maven-shade-plugin + 3.2.4 + + + package + + shade + + + + bungeecore + + + dv8tion + m2-dv8tion + https://m2.dv8tion.net/releases + + + steamwar @@ -61,5 +82,23 @@ system ${main.basedir}/lib/BungeeTabListPlus.jar + + net.dv8tion + JDA + 4.3.0_299 + compile + + + club.minnced + opus-java + + + + + org.projectlombok + lombok + 1.18.20 + provided + \ No newline at end of file diff --git a/src/de/steamwar/bungeecore/BungeeCore.java b/src/de/steamwar/bungeecore/BungeeCore.java index f0a8bb79..72e58395 100644 --- a/src/de/steamwar/bungeecore/BungeeCore.java +++ b/src/de/steamwar/bungeecore/BungeeCore.java @@ -19,6 +19,8 @@ package de.steamwar.bungeecore; +import de.steamwar.bungeecore.bot.SteamwarDiscordBot; +import de.steamwar.bungeecore.bot.config.SteamwarDiscordBotConfig; import de.steamwar.bungeecore.commands.*; import de.steamwar.bungeecore.comms.SpigotReceiver; import de.steamwar.bungeecore.listeners.*; @@ -141,6 +143,7 @@ public class BungeeCore extends Plugin { new EventStarter(); new SessionManager(); new SpigotReceiver(); + new SteamwarDiscordBot(); new TablistManager(); getProxy().getScheduler().schedule(this, () -> { @@ -254,6 +257,7 @@ public class BungeeCore extends Plugin { ); ArenaMode.init(config.getSection("games")); + SteamwarDiscordBotConfig.loadConfig(config.getSection("discord")); final Configuration servers = config.getSection("servers"); for(final String serverName : servers.getKeys()){ diff --git a/src/de/steamwar/bungeecore/bot/SteamwarDiscordBot.java b/src/de/steamwar/bungeecore/bot/SteamwarDiscordBot.java new file mode 100644 index 00000000..91ab97f7 --- /dev/null +++ b/src/de/steamwar/bungeecore/bot/SteamwarDiscordBot.java @@ -0,0 +1,70 @@ +/* + This file is a part of the SteamWar software. + + Copyright (C) 2020 SteamWar.de-Serverteam + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . + */ + +package de.steamwar.bungeecore.bot; + +import de.steamwar.bungeecore.bot.config.SteamwarDiscordBotConfig; +import de.steamwar.bungeecore.bot.listeners.RolesInteractionButtonListener; +import de.steamwar.bungeecore.bot.util.DiscordRolesMessage; +import de.steamwar.bungeecore.bot.util.DiscordRulesMessage; +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.hooks.ListenerAdapter; + +import javax.security.auth.login.LoginException; + +public class SteamwarDiscordBot { + + private static SteamwarDiscordBot INSTANCE; + + public static SteamwarDiscordBot instance() { + return INSTANCE; + } + + @Getter + private final JDA jda; + + public SteamwarDiscordBot() { + INSTANCE = this; + JDABuilder builder = JDABuilder.createDefault(SteamwarDiscordBotConfig.TOKEN); + builder.setActivity(Activity.playing("auf Steamwar.de")); + builder.setStatus(OnlineStatus.ONLINE); + try { + jda = builder.build(); + } catch (LoginException e) { + throw new SecurityException("Could not Login: " + SteamwarDiscordBotConfig.TOKEN, e); + } + try { + jda.awaitReady(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + DiscordRolesMessage.sendMessage(); + DiscordRulesMessage.sendMessage(); + + new RolesInteractionButtonListener(); + } + + public void addListener(ListenerAdapter listenerAdapter) { + jda.addEventListener(listenerAdapter); + } +} diff --git a/src/de/steamwar/bungeecore/bot/config/DiscordRole.java b/src/de/steamwar/bungeecore/bot/config/DiscordRole.java new file mode 100644 index 00000000..68b57bee --- /dev/null +++ b/src/de/steamwar/bungeecore/bot/config/DiscordRole.java @@ -0,0 +1,39 @@ +/* + This file is a part of the SteamWar software. + + Copyright (C) 2020 SteamWar.de-Serverteam + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . + */ + +package de.steamwar.bungeecore.bot.config; + +import lombok.AllArgsConstructor; +import lombok.Data; +import net.dv8tion.jda.api.entities.Emoji; +import net.dv8tion.jda.api.interactions.components.Button; +import net.dv8tion.jda.api.interactions.components.ButtonStyle; + +@Data +@AllArgsConstructor +public class DiscordRole { + + private String emoji; + private String label; + private String roleId; + + public Button toButton() { + return Button.of(ButtonStyle.SECONDARY, roleId, label, Emoji.fromUnicode(emoji)); + } +} diff --git a/src/de/steamwar/bungeecore/bot/config/DiscordRulesLink.java b/src/de/steamwar/bungeecore/bot/config/DiscordRulesLink.java new file mode 100644 index 00000000..4f2ebe01 --- /dev/null +++ b/src/de/steamwar/bungeecore/bot/config/DiscordRulesLink.java @@ -0,0 +1,36 @@ +/* + This file is a part of the SteamWar software. + + Copyright (C) 2020 SteamWar.de-Serverteam + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . + */ + +package de.steamwar.bungeecore.bot.config; + +import lombok.AllArgsConstructor; +import lombok.Data; +import net.dv8tion.jda.api.interactions.components.Button; + +@Data +@AllArgsConstructor +public class DiscordRulesLink { + + private String label; + private String link; + + public Button toButton() { + return Button.link(link, label); + } +} diff --git a/src/de/steamwar/bungeecore/bot/config/SteamwarDiscordBotConfig.java b/src/de/steamwar/bungeecore/bot/config/SteamwarDiscordBotConfig.java new file mode 100644 index 00000000..b1baaee7 --- /dev/null +++ b/src/de/steamwar/bungeecore/bot/config/SteamwarDiscordBotConfig.java @@ -0,0 +1,71 @@ +/* + This file is a part of the SteamWar software. + + Copyright (C) 2020 SteamWar.de-Serverteam + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . + */ + +package de.steamwar.bungeecore.bot.config; + +import net.md_5.bungee.config.Configuration; + +import java.util.ArrayList; +import java.util.List; + +public class SteamwarDiscordBotConfig { + + public static String TOKEN; + public static String GUILD; + public static String ROLES_CHANNEL; + public static String ROLES_BASE_MESSAGE; + public static String ROLES_ADDED; + public static String ROLES_REMOVED; + public static List ROLES; + public static String RULES_CHANNEL; + public static String RULES_TITLE; + public static List RULES_RULES; + public static List RULES_LINKS; + + public static void loadConfig(Configuration config) { + TOKEN = config.getString("token"); + GUILD = config.getString("guild"); + Configuration rolesSection = config.getSection("roles-claim"); + ROLES_CHANNEL = rolesSection.getString("channel"); + ROLES_BASE_MESSAGE = rolesSection.getString("base"); + ROLES_ADDED = rolesSection.getString("added"); + ROLES_REMOVED = rolesSection.getString("removed"); + ROLES = new ArrayList<>(); + + for (String roles : rolesSection.getSection("roles").getKeys()) { + Configuration role = rolesSection.getSection("roles").getSection(roles); + ROLES.add(new DiscordRole(role.getString("emoji"), + role.getString("label"), + role.getString("roleId"))); + } + + Configuration rulesSection = config.getSection("rules"); + RULES_CHANNEL = rulesSection.getString("channel"); + RULES_TITLE = rulesSection.getString("title"); + RULES_RULES = rulesSection.getStringList("rules"); + + RULES_LINKS = new ArrayList<>(); + + for (String links : rulesSection.getSection("links").getKeys()) { + Configuration link = rulesSection.getSection("links").getSection(links); + RULES_LINKS.add(new DiscordRulesLink(link.getString("label"), + link.getString("url"))); + } + } +} diff --git a/src/de/steamwar/bungeecore/bot/listeners/BasicDiscordListener.java b/src/de/steamwar/bungeecore/bot/listeners/BasicDiscordListener.java new file mode 100644 index 00000000..1c8a5ffd --- /dev/null +++ b/src/de/steamwar/bungeecore/bot/listeners/BasicDiscordListener.java @@ -0,0 +1,30 @@ +/* + This file is a part of the SteamWar software. + + Copyright (C) 2020 SteamWar.de-Serverteam + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . + */ + +package de.steamwar.bungeecore.bot.listeners; + +import de.steamwar.bungeecore.bot.SteamwarDiscordBot; +import net.dv8tion.jda.api.hooks.ListenerAdapter; + +public abstract class BasicDiscordListener extends ListenerAdapter { + + BasicDiscordListener() { + SteamwarDiscordBot.instance().addListener(this); + } +} diff --git a/src/de/steamwar/bungeecore/bot/listeners/RolesInteractionButtonListener.java b/src/de/steamwar/bungeecore/bot/listeners/RolesInteractionButtonListener.java new file mode 100644 index 00000000..0790a07d --- /dev/null +++ b/src/de/steamwar/bungeecore/bot/listeners/RolesInteractionButtonListener.java @@ -0,0 +1,41 @@ +/* + This file is a part of the SteamWar software. + + Copyright (C) 2020 SteamWar.de-Serverteam + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . + */ + +package de.steamwar.bungeecore.bot.listeners; + +import de.steamwar.bungeecore.bot.config.SteamwarDiscordBotConfig; +import net.dv8tion.jda.api.events.interaction.GenericComponentInteractionCreateEvent; +import net.dv8tion.jda.api.interactions.InteractionType; +import org.jetbrains.annotations.NotNull; + +public class RolesInteractionButtonListener extends BasicDiscordListener { + + @Override + public void onGenericComponentInteractionCreate(@NotNull GenericComponentInteractionCreateEvent event) { + if(event.getType() == InteractionType.COMPONENT && event.getTextChannel().getId().equals(SteamwarDiscordBotConfig.ROLES_CHANNEL) && SteamwarDiscordBotConfig.ROLES.stream().anyMatch(discordRole -> discordRole.getRoleId().equals(event.getComponentId()))) { + if (event.getMember().getRoles().stream().anyMatch(role -> role.getId().equals(event.getComponentId()))) { + event.getGuild().removeRoleFromMember(event.getMember(), event.getGuild().getRoleById(event.getComponentId())).complete(); + event.reply(SteamwarDiscordBotConfig.ROLES_REMOVED.replace("%role%", event.getGuild().getRoleById(event.getComponentId()).getAsMention())).setEphemeral(true).complete(); + } else { + event.getGuild().addRoleToMember(event.getMember(), event.getGuild().getRoleById(event.getComponentId())).complete(); + event.reply(SteamwarDiscordBotConfig.ROLES_ADDED.replace("%role%", event.getGuild().getRoleById(event.getComponentId()).getAsMention())).setEphemeral(true).complete(); + } + } + } +} diff --git a/src/de/steamwar/bungeecore/bot/util/DiscordRolesMessage.java b/src/de/steamwar/bungeecore/bot/util/DiscordRolesMessage.java new file mode 100644 index 00000000..deb65ed2 --- /dev/null +++ b/src/de/steamwar/bungeecore/bot/util/DiscordRolesMessage.java @@ -0,0 +1,51 @@ +/* + This file is a part of the SteamWar software. + + Copyright (C) 2020 SteamWar.de-Serverteam + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . + */ + +package de.steamwar.bungeecore.bot.util; + +import de.steamwar.bungeecore.bot.SteamwarDiscordBot; +import de.steamwar.bungeecore.bot.config.SteamwarDiscordBotConfig; +import net.dv8tion.jda.api.MessageBuilder; +import net.dv8tion.jda.api.entities.TextChannel; +import net.dv8tion.jda.api.interactions.components.ActionRow; +import net.dv8tion.jda.api.interactions.components.Button; +import net.dv8tion.jda.api.requests.restaction.MessageAction; + +import java.util.ArrayList; +import java.util.List; + +public class DiscordRolesMessage { + + public static void sendMessage() { + TextChannel channel = SteamwarDiscordBot.instance().getJda().getGuildById(SteamwarDiscordBotConfig.GUILD).getTextChannelById(SteamwarDiscordBotConfig.ROLES_CHANNEL); + assert channel != null; + if(channel.hasLatestMessage()) { + channel.getIterableHistory().complete().forEach(message -> message.delete().complete()); + } + + MessageBuilder builder = new MessageBuilder(); + builder.setContent(SteamwarDiscordBotConfig.ROLES_BASE_MESSAGE); + List