From 268fa2d50804d1c54855ad0cfce8a1636e78354a Mon Sep 17 00:00:00 2001 From: yoyosource Date: Thu, 29 Jul 2021 16:40:39 +0200 Subject: [PATCH] Add IngameChatListener Add ServerTeamchatCommand --- .../bungeecore/bot/SteamwarDiscordBot.java | 16 +++-- .../bot/config/SteamwarDiscordBotConfig.java | 4 ++ .../bot/listeners/IngameChatListener.java | 58 ++++++++++++++++++ .../bot/listeners/ServerTeamChatListener.java | 59 +++++++++++++++++++ .../commands/ServerTeamchatCommand.java | 12 ++++ .../bungeecore/listeners/ChatListener.java | 24 ++++++++ 6 files changed, 167 insertions(+), 6 deletions(-) create mode 100644 src/de/steamwar/bungeecore/bot/listeners/IngameChatListener.java create mode 100644 src/de/steamwar/bungeecore/bot/listeners/ServerTeamChatListener.java diff --git a/src/de/steamwar/bungeecore/bot/SteamwarDiscordBot.java b/src/de/steamwar/bungeecore/bot/SteamwarDiscordBot.java index 03dceb9..295db91 100644 --- a/src/de/steamwar/bungeecore/bot/SteamwarDiscordBot.java +++ b/src/de/steamwar/bungeecore/bot/SteamwarDiscordBot.java @@ -22,20 +22,16 @@ package de.steamwar.bungeecore.bot; import de.steamwar.bungeecore.BungeeCore; import de.steamwar.bungeecore.bot.config.SteamwarDiscordBotConfig; import de.steamwar.bungeecore.bot.events.EventManager; -import de.steamwar.bungeecore.bot.listeners.AnnouncementListener; -import de.steamwar.bungeecore.bot.listeners.DiscordAuthListener; -import de.steamwar.bungeecore.bot.listeners.DiscordTicketListener; -import de.steamwar.bungeecore.bot.listeners.RolesInteractionButtonListener; -import de.steamwar.bungeecore.bot.util.DiscordTicketMessage; +import de.steamwar.bungeecore.bot.listeners.*; import de.steamwar.bungeecore.bot.util.DiscordRolesMessage; import de.steamwar.bungeecore.bot.util.DiscordRulesMessage; +import de.steamwar.bungeecore.bot.util.DiscordTicketMessage; import de.steamwar.bungeecore.sql.Event; 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.entities.Member; import net.dv8tion.jda.api.hooks.ListenerAdapter; import net.dv8tion.jda.api.utils.MemberCachePolicy; import net.md_5.bungee.api.ProxyServer; @@ -54,6 +50,12 @@ public class SteamwarDiscordBot { @Getter private AnnouncementListener announcementListener; + @Getter + private IngameChatListener ingameChatListener; + + @Getter + private ServerTeamChatListener serverTeamChatListener; + @Getter private final JDA jda; @@ -86,6 +88,8 @@ public class SteamwarDiscordBot { new DiscordTicketListener(); new DiscordAuthListener(); announcementListener = new AnnouncementListener(); + ingameChatListener = new IngameChatListener(); + serverTeamChatListener = new ServerTeamChatListener(); } private int index = 0; diff --git a/src/de/steamwar/bungeecore/bot/config/SteamwarDiscordBotConfig.java b/src/de/steamwar/bungeecore/bot/config/SteamwarDiscordBotConfig.java index 14f1efb..cefedcc 100644 --- a/src/de/steamwar/bungeecore/bot/config/SteamwarDiscordBotConfig.java +++ b/src/de/steamwar/bungeecore/bot/config/SteamwarDiscordBotConfig.java @@ -33,6 +33,8 @@ public class SteamwarDiscordBotConfig { public static String GUILD; public static String ANNOUNCEMENTS_CHANNEL; public static String EVENTS_CHANNEL; + public static String INGAME_CHANNEL; + public static String SERVER_TEAM_CHANNEL; public static String ROLES_CHANNEL; public static String ROLES_BASE_MESSAGE; public static String ROLES_ADDED; @@ -55,6 +57,8 @@ public class SteamwarDiscordBotConfig { GUILD = config.getString("guild"); ANNOUNCEMENTS_CHANNEL = config.getString("announcements-channel"); EVENTS_CHANNEL = config.getString("events-channel"); + INGAME_CHANNEL = config.getString("ingame-channel"); + SERVER_TEAM_CHANNEL = config.getString("server-team-channel"); Configuration rolesSection = config.getSection("roles-claim"); ROLES_CHANNEL = rolesSection.getString("channel"); ROLES_BASE_MESSAGE = rolesSection.getString("base"); diff --git a/src/de/steamwar/bungeecore/bot/listeners/IngameChatListener.java b/src/de/steamwar/bungeecore/bot/listeners/IngameChatListener.java new file mode 100644 index 0000000..ae39213 --- /dev/null +++ b/src/de/steamwar/bungeecore/bot/listeners/IngameChatListener.java @@ -0,0 +1,58 @@ +/* + * 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 de.steamwar.bungeecore.bot.config.SteamwarDiscordBotConfig; +import de.steamwar.bungeecore.listeners.ChatListener; +import de.steamwar.bungeecore.sql.SteamwarUser; +import net.dv8tion.jda.api.MessageBuilder; +import net.dv8tion.jda.api.entities.Member; +import net.dv8tion.jda.api.entities.TextChannel; +import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent; +import org.jetbrains.annotations.NotNull; + +public class IngameChatListener extends BasicDiscordListener { + + @Override + public void onGuildMessageReceived(@NotNull GuildMessageReceivedEvent event) { + if (!event.getChannel().getId().equals(SteamwarDiscordBotConfig.INGAME_CHANNEL)) { + return; + } + if (event.getAuthor().isBot()) { + return; + } + Member member = event.getMember(); + SteamwarUser steamwarUser = SteamwarUser.get(member.getIdLong()); + if (steamwarUser == null) { + event.getMessage().delete().complete(); + } else { + ChatListener.discordChat(steamwarUser, event.getMessage().getContentRaw()); + } + } + + public void send(String message) { + TextChannel textChannel = SteamwarDiscordBot.instance().getJda().getGuildById(SteamwarDiscordBotConfig.GUILD).getTextChannelById(SteamwarDiscordBotConfig.INGAME_CHANNEL); + assert textChannel != null; + MessageBuilder messageBuilder = new MessageBuilder(); + messageBuilder.append(message.replace("&", "")); + textChannel.sendMessage(messageBuilder.build()).complete(); + } +} diff --git a/src/de/steamwar/bungeecore/bot/listeners/ServerTeamChatListener.java b/src/de/steamwar/bungeecore/bot/listeners/ServerTeamChatListener.java new file mode 100644 index 0000000..432c287 --- /dev/null +++ b/src/de/steamwar/bungeecore/bot/listeners/ServerTeamChatListener.java @@ -0,0 +1,59 @@ +/* + * 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 de.steamwar.bungeecore.bot.config.SteamwarDiscordBotConfig; +import de.steamwar.bungeecore.commands.ServerTeamchatCommand; +import de.steamwar.bungeecore.listeners.ChatListener; +import de.steamwar.bungeecore.sql.SteamwarUser; +import net.dv8tion.jda.api.MessageBuilder; +import net.dv8tion.jda.api.entities.Member; +import net.dv8tion.jda.api.entities.TextChannel; +import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent; +import org.jetbrains.annotations.NotNull; + +public class ServerTeamChatListener extends BasicDiscordListener { + + @Override + public void onGuildMessageReceived(@NotNull GuildMessageReceivedEvent event) { + if (!event.getChannel().getId().equals(SteamwarDiscordBotConfig.SERVER_TEAM_CHANNEL)) { + return; + } + if (event.getAuthor().isBot()) { + return; + } + Member member = event.getMember(); + SteamwarUser steamwarUser = SteamwarUser.get(member.getIdLong()); + if (steamwarUser == null) { + event.getMessage().delete().complete(); + } else { + ServerTeamchatCommand.sendToTeam(event.getMessage().getContentRaw(), steamwarUser); + } + } + + public void send(String message) { + TextChannel textChannel = SteamwarDiscordBot.instance().getJda().getGuildById(SteamwarDiscordBotConfig.GUILD).getTextChannelById(SteamwarDiscordBotConfig.SERVER_TEAM_CHANNEL); + assert textChannel != null; + MessageBuilder messageBuilder = new MessageBuilder(); + messageBuilder.append(message.replace("&", "")); + textChannel.sendMessage(messageBuilder.build()).complete(); + } +} diff --git a/src/de/steamwar/bungeecore/commands/ServerTeamchatCommand.java b/src/de/steamwar/bungeecore/commands/ServerTeamchatCommand.java index 0aca552..b9b0ab1 100644 --- a/src/de/steamwar/bungeecore/commands/ServerTeamchatCommand.java +++ b/src/de/steamwar/bungeecore/commands/ServerTeamchatCommand.java @@ -20,7 +20,9 @@ package de.steamwar.bungeecore.commands; import de.steamwar.bungeecore.Message; +import de.steamwar.bungeecore.bot.SteamwarDiscordBot; import de.steamwar.bungeecore.listeners.ChatListener; +import de.steamwar.bungeecore.sql.SteamwarUser; import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.CommandSender; import net.md_5.bungee.api.ProxyServer; @@ -49,10 +51,20 @@ public class ServerTeamchatCommand extends BasicCommand { String msg = msgBuilder.toString(); msg = ChatColor.translateAlternateColorCodes('&', msg); + SteamwarDiscordBot.instance().getServerTeamChatListener().send(" " + (player.getName() + "» " + msg).replaceAll("§[a-f0-9r]", "")); sendToTeam(msg, player); } } + public static void sendToTeam(String message, SteamwarUser sender){ + for (ProxiedPlayer target : ProxyServer.getInstance().getPlayers()){ + if ((target.hasPermission("bungeecore.teamchat")) + && target.getChatMode() == ProxiedPlayer.ChatMode.SHOWN){ + Message.sendPrefixless("STC_FORMAT", target, sender.getUserName(), ChatListener.parseAtMessage(message, "§r", target)); + } + } + } + public static void sendToTeam(String message, ProxiedPlayer sender){ for (ProxiedPlayer target : ProxyServer.getInstance().getPlayers()){ if ((target.hasPermission("bungeecore.teamchat")) diff --git a/src/de/steamwar/bungeecore/listeners/ChatListener.java b/src/de/steamwar/bungeecore/listeners/ChatListener.java index a73ea6d..d7c96ca 100644 --- a/src/de/steamwar/bungeecore/listeners/ChatListener.java +++ b/src/de/steamwar/bungeecore/listeners/ChatListener.java @@ -20,6 +20,8 @@ package de.steamwar.bungeecore.listeners; import de.steamwar.bungeecore.*; +import de.steamwar.bungeecore.bot.SteamwarDiscordBot; +import de.steamwar.bungeecore.bot.listeners.IngameChatListener; import de.steamwar.bungeecore.commands.BauCommand; import de.steamwar.bungeecore.commands.TpCommand; import de.steamwar.bungeecore.comms.packets.PingPacket; @@ -170,6 +172,27 @@ public class ChatListener extends BasicListener { } } + public static void discordChat(SteamwarUser user, String message){ + String name = user.getUserGroup().getColorCode() + user.getUserGroup().name() + " " + user.getUserName() + "§r"; + String chatcolor = user.getUserGroup().getChatColorCode(); + + if(user.getUserGroup() != UserGroup.Member || user.getTeam() == 12 || user.getTeam() == 285 || user.getTeam() == 54) + message = ChatColor.translateAlternateColorCodes('&', message); + + String msg = name + "§7»" + chatcolor + " " + message; + if(user.getTeam() != 0){ + Team team = Team.get(user.getTeam()); + msg = "§" + team.getTeamColor() + team.getTeamKuerzel() + " §r" + msg; + } + + for(ProxiedPlayer target : ProxyServer.getInstance().getPlayers()){ + Subserver targetServer = Subserver.getSubserver(target); + if(!(targetServer == null || targetServer.getType() != Servertype.ARENA || targetServer.getServer() != target.getServer().getInfo())) continue; + + BungeeCore.send(target, ChatMessageType.CHAT, parseAtMessage(msg, chatcolor, target)); + } + } + private void publicChat(SteamwarUser user, ProxiedPlayer sender, String message){ String name = sender.getDisplayName(); String chatcolor = user.getUserGroup().getChatColorCode(); @@ -183,6 +206,7 @@ public class ChatListener extends BasicListener { msg = "§" + team.getTeamColor() + team.getTeamKuerzel() + " §r" + msg; } + SteamwarDiscordBot.instance().getIngameChatListener().send(" " + msg.replaceAll("§[a-f0-9r]", "")); for(ProxiedPlayer target : ProxyServer.getInstance().getPlayers()){ Subserver targetServer = Subserver.getSubserver(target); if(!(targetServer == null || targetServer.getType() != Servertype.ARENA || targetServer.getServer() != target.getServer().getInfo())) continue;