From af79ef544bee2f07f4aa134409dd539f3dc8590b Mon Sep 17 00:00:00 2001 From: Lixfel Date: Mon, 20 Jan 2025 16:56:59 +0100 Subject: [PATCH] Fixes --- .../velocitycore/discord/DiscordBot.java | 17 +---- .../velocitycore/discord/DiscordConfig.java | 2 +- .../discord/channels/CouncilChannel.java | 68 +++++++++++++++++++ .../discord/channels/DiscordChannel.java | 3 +- .../discord/channels/RatsChannel.java | 63 ----------------- .../channels/StaticMessageChannel.java | 9 ++- .../discord/listeners/RoleListener.java | 38 +++++++++++ 7 files changed, 115 insertions(+), 85 deletions(-) create mode 100644 VelocityCore/src/de/steamwar/velocitycore/discord/channels/CouncilChannel.java delete mode 100644 VelocityCore/src/de/steamwar/velocitycore/discord/channels/RatsChannel.java create mode 100644 VelocityCore/src/de/steamwar/velocitycore/discord/listeners/RoleListener.java diff --git a/VelocityCore/src/de/steamwar/velocitycore/discord/DiscordBot.java b/VelocityCore/src/de/steamwar/velocitycore/discord/DiscordBot.java index 1c7bfa4f..89167565 100644 --- a/VelocityCore/src/de/steamwar/velocitycore/discord/DiscordBot.java +++ b/VelocityCore/src/de/steamwar/velocitycore/discord/DiscordBot.java @@ -24,10 +24,7 @@ import de.steamwar.messages.Chatter; import de.steamwar.sql.Event; import de.steamwar.velocitycore.VelocityCore; import de.steamwar.velocitycore.discord.channels.*; -import de.steamwar.velocitycore.discord.listeners.ChannelListener; -import de.steamwar.velocitycore.discord.listeners.DiscordSchemUpload; -import de.steamwar.velocitycore.discord.listeners.DiscordTeamEvent; -import de.steamwar.velocitycore.discord.listeners.DiscordTicketHandler; +import de.steamwar.velocitycore.discord.listeners.*; import de.steamwar.velocitycore.discord.util.AuthManager; import lombok.Getter; import net.dv8tion.jda.api.EmbedBuilder; @@ -38,7 +35,6 @@ import net.dv8tion.jda.api.entities.Activity; import net.dv8tion.jda.api.entities.Guild; import net.dv8tion.jda.api.entities.Member; import net.dv8tion.jda.api.entities.Role; -import net.dv8tion.jda.api.entities.channel.concrete.ThreadChannel; import net.dv8tion.jda.api.entities.emoji.Emoji; import net.dv8tion.jda.api.events.message.MessageReceivedEvent; import net.dv8tion.jda.api.exceptions.ErrorResponseException; @@ -166,6 +162,7 @@ public class DiscordBot { .setComponents(actionRows), DiscordTicketHandler::openTicket); eventChannel = new StaticMessageChannel(config.channel("events"), EventChannel::get); checklistChannel = new ChecklistChannel(config.channel("checklist")); + config.getCouncilThread().forEach((roleId, threadId) -> new CouncilChannel(DiscordBot.getGuild().getRoleById(roleId), DiscordBot.getGuild().getThreadChannelById(threadId))); announcementChannel = new DiscordChannel(config.channel("announcement")) { @Override @@ -191,17 +188,9 @@ public class DiscordBot { new DiscordTeamEvent(), new ChannelListener(), new DiscordSchemUpload(), - new RatsChannel.RastChannelListener() + new RoleListener() ); - config.getRatsRoleToThread().forEach((roleId, threadId) -> { - Role role = DiscordBot.getGuild().getRoleById(roleId); - if (role == null) return; - ThreadChannel threadChannel = DiscordBot.getGuild().getThreadChannelById(threadId); - if (threadChannel == null) return; - new RatsChannel(role, threadChannel); - }); - commandSetup(jda.retrieveCommands().complete(), jda.updateCommands()); } diff --git a/VelocityCore/src/de/steamwar/velocitycore/discord/DiscordConfig.java b/VelocityCore/src/de/steamwar/velocitycore/discord/DiscordConfig.java index 60a13f88..7f201abb 100644 --- a/VelocityCore/src/de/steamwar/velocitycore/discord/DiscordConfig.java +++ b/VelocityCore/src/de/steamwar/velocitycore/discord/DiscordConfig.java @@ -56,7 +56,7 @@ public class DiscordConfig { private String ticketcategory; - private Map ratsRoleToThread; + private Map councilThread; @NoArgsConstructor public static class DiscordRole { diff --git a/VelocityCore/src/de/steamwar/velocitycore/discord/channels/CouncilChannel.java b/VelocityCore/src/de/steamwar/velocitycore/discord/channels/CouncilChannel.java new file mode 100644 index 00000000..6a949d0b --- /dev/null +++ b/VelocityCore/src/de/steamwar/velocitycore/discord/channels/CouncilChannel.java @@ -0,0 +1,68 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2025 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.velocitycore.discord.channels; + +import de.steamwar.sql.SteamwarUser; +import de.steamwar.velocitycore.discord.DiscordBot; +import net.dv8tion.jda.api.EmbedBuilder; +import net.dv8tion.jda.api.entities.Member; +import net.dv8tion.jda.api.entities.Role; +import net.dv8tion.jda.api.entities.channel.concrete.ThreadChannel; +import net.dv8tion.jda.api.utils.messages.MessageCreateBuilder; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class CouncilChannel extends StaticMessageChannel { + + private static final Map channels = new HashMap<>(); + + public static void update(List roles) { + for(Role role : roles) { + if (channels.containsKey(role)) { + channels.get(role).update(); + } + } + } + + public CouncilChannel(Role role, ThreadChannel threadChannel) { + super(threadChannel, () -> { + MessageCreateBuilder messageCreateBuilder = new MessageCreateBuilder(); + messageCreateBuilder.setContent("# Ratsmitglieder"); + + for (Member member : DiscordBot.getGuild().getMembersWithRoles(role)) { + SteamwarUser steamwarUser = SteamwarUser.get(member.getIdLong()); + if (steamwarUser == null) + continue; + + messageCreateBuilder.addEmbeds(new EmbedBuilder() + .setTitle(steamwarUser.getUserName()) + .setImage("api.steamwar.de/data/skin/" + steamwarUser.getUUID().toString()) + .build()); + } + + return messageCreateBuilder; + }, event -> {}); + + channels.put(role, this); + } + +} diff --git a/VelocityCore/src/de/steamwar/velocitycore/discord/channels/DiscordChannel.java b/VelocityCore/src/de/steamwar/velocitycore/discord/channels/DiscordChannel.java index 3440e1f5..b420f601 100644 --- a/VelocityCore/src/de/steamwar/velocitycore/discord/channels/DiscordChannel.java +++ b/VelocityCore/src/de/steamwar/velocitycore/discord/channels/DiscordChannel.java @@ -52,8 +52,7 @@ public class DiscordChannel extends Chatter.PlayerlessChatter { } public DiscordChannel(String channel) { - this(SteamwarUser.get(-1), DiscordBot.getGuild().getTextChannelById(channel)); - ChannelListener.getChannels().put(this.channel, this); + this(DiscordBot.getGuild().getTextChannelById(channel)); } public DiscordChannel(MessageChannel channel) { diff --git a/VelocityCore/src/de/steamwar/velocitycore/discord/channels/RatsChannel.java b/VelocityCore/src/de/steamwar/velocitycore/discord/channels/RatsChannel.java deleted file mode 100644 index 7f8e9e44..00000000 --- a/VelocityCore/src/de/steamwar/velocitycore/discord/channels/RatsChannel.java +++ /dev/null @@ -1,63 +0,0 @@ -package de.steamwar.velocitycore.discord.channels; - -import de.steamwar.sql.SteamwarUser; -import de.steamwar.velocitycore.discord.DiscordBot; -import net.dv8tion.jda.api.EmbedBuilder; -import net.dv8tion.jda.api.entities.Member; -import net.dv8tion.jda.api.entities.Role; -import net.dv8tion.jda.api.entities.channel.concrete.ThreadChannel; -import net.dv8tion.jda.api.events.guild.member.GuildMemberRoleAddEvent; -import net.dv8tion.jda.api.events.guild.member.GuildMemberRoleRemoveEvent; -import net.dv8tion.jda.api.hooks.ListenerAdapter; -import net.dv8tion.jda.api.utils.messages.MessageCreateBuilder; -import org.jetbrains.annotations.NotNull; - -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -public class RatsChannel extends StaticMessageChannel { - - private static Map listener = new HashMap<>(); - - public RatsChannel(Role role, ThreadChannel threadChannel) { - super(threadChannel, () -> { - MessageCreateBuilder messageCreateBuilder = new MessageCreateBuilder(); - messageCreateBuilder.setContent("# Ratsmitglieder"); - - for (Member member : DiscordBot.getGuild().getMembersWithRoles(role)) { - SteamwarUser steamwarUser = SteamwarUser.get(member.getIdLong()); - if (steamwarUser == null) continue; - - messageCreateBuilder.addEmbeds(new EmbedBuilder() - .setTitle(steamwarUser.getUserName()) - .setImage("api.steamwar.de/data/skin/" + steamwarUser.getUUID().toString()) - .build()); - } - - return messageCreateBuilder; - }, genericComponentInteractionCreateEvent -> {}); - - listener.put(role, this); - } - - public static class RastChannelListener extends ListenerAdapter { - @Override - public void onGuildMemberRoleAdd(@NotNull GuildMemberRoleAddEvent event) { - updateRatsChannel(event.getRoles()); - } - - @Override - public void onGuildMemberRoleRemove(@NotNull GuildMemberRoleRemoveEvent event) { - updateRatsChannel(event.getRoles()); - } - - private void updateRatsChannel(List roles) { - roles.forEach(role -> { - if (listener.containsKey(role)) { - listener.get(role).update(); - } - }); - } - } -} diff --git a/VelocityCore/src/de/steamwar/velocitycore/discord/channels/StaticMessageChannel.java b/VelocityCore/src/de/steamwar/velocitycore/discord/channels/StaticMessageChannel.java index 0db34fc8..69549d38 100644 --- a/VelocityCore/src/de/steamwar/velocitycore/discord/channels/StaticMessageChannel.java +++ b/VelocityCore/src/de/steamwar/velocitycore/discord/channels/StaticMessageChannel.java @@ -49,18 +49,17 @@ public class StaticMessageChannel extends DiscordChannel { super(channel); this.supplier = supplier; this.interaction = interaction; - - if(getChannel().getLatestMessageIdLong() != 0) - message = getChannel().getIterableHistory().complete().stream().filter(m -> m.getAuthor().isBot()).findFirst().orElse(null); - - update(); + init(); } public StaticMessageChannel(MessageChannel channel, Supplier supplier, Consumer interaction) { super(channel); this.supplier = supplier; this.interaction = interaction; + init(); + } + private void init() { if(getChannel().getLatestMessageIdLong() != 0) message = getChannel().getIterableHistory().complete().stream().filter(m -> m.getAuthor().isBot()).findFirst().orElse(null); diff --git a/VelocityCore/src/de/steamwar/velocitycore/discord/listeners/RoleListener.java b/VelocityCore/src/de/steamwar/velocitycore/discord/listeners/RoleListener.java new file mode 100644 index 00000000..75063344 --- /dev/null +++ b/VelocityCore/src/de/steamwar/velocitycore/discord/listeners/RoleListener.java @@ -0,0 +1,38 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2025 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.velocitycore.discord.listeners; + +import de.steamwar.velocitycore.discord.channels.CouncilChannel; +import net.dv8tion.jda.api.events.guild.member.GuildMemberRoleAddEvent; +import net.dv8tion.jda.api.events.guild.member.GuildMemberRoleRemoveEvent; +import net.dv8tion.jda.api.hooks.ListenerAdapter; +import org.jetbrains.annotations.NotNull; + +public class RoleListener extends ListenerAdapter { + @Override + public void onGuildMemberRoleAdd(@NotNull GuildMemberRoleAddEvent event) { + CouncilChannel.update(event.getRoles()); + } + + @Override + public void onGuildMemberRoleRemove(@NotNull GuildMemberRoleRemoveEvent event) { + CouncilChannel.update(event.getRoles()); + } +}