From e7f0e57e9d1b5630c033236ef4e0e6bd0f477c8e Mon Sep 17 00:00:00 2001 From: Lixfel Date: Sat, 29 Jun 2024 23:39:09 +0200 Subject: [PATCH] Fix DC alerts, DC commands, Poll command Signed-off-by: Lixfel --- .../velocitycore/commands/PollCommand.java | 2 +- .../discord/channels/DiscordChannel.java | 4 -- .../discord/listeners/ChannelListener.java | 8 +++- .../discord/util/DiscordAlert.java | 37 +++++++++++-------- 4 files changed, 29 insertions(+), 22 deletions(-) diff --git a/src/de/steamwar/velocitycore/commands/PollCommand.java b/src/de/steamwar/velocitycore/commands/PollCommand.java index a594c43..7264616 100644 --- a/src/de/steamwar/velocitycore/commands/PollCommand.java +++ b/src/de/steamwar/velocitycore/commands/PollCommand.java @@ -32,7 +32,7 @@ public class PollCommand extends SWCommand { } @Register - public void genericCommand(Chatter sender) { + public void genericCommand(@Validator Chatter sender) { PollSystem.sendPoll(sender); } diff --git a/src/de/steamwar/velocitycore/discord/channels/DiscordChannel.java b/src/de/steamwar/velocitycore/discord/channels/DiscordChannel.java index aac9206..304e2d0 100644 --- a/src/de/steamwar/velocitycore/discord/channels/DiscordChannel.java +++ b/src/de/steamwar/velocitycore/discord/channels/DiscordChannel.java @@ -51,10 +51,6 @@ public class DiscordChannel extends Chatter.PlayerlessChatter { this(userOrPublic(user), user.openPrivateChannel().complete()); } - public DiscordChannel(SteamwarUser user) { - this(user, DiscordBot.getInstance().getJda().getUserById(user.getDiscordId()).openPrivateChannel().complete()); - } - public DiscordChannel(String channel) { this(SteamwarUser.get(-1), DiscordBot.getGuild().getTextChannelById(channel)); ChannelListener.getChannels().put(this.channel, this); diff --git a/src/de/steamwar/velocitycore/discord/listeners/ChannelListener.java b/src/de/steamwar/velocitycore/discord/listeners/ChannelListener.java index 115be4f..2fffa1a 100644 --- a/src/de/steamwar/velocitycore/discord/listeners/ChannelListener.java +++ b/src/de/steamwar/velocitycore/discord/listeners/ChannelListener.java @@ -37,7 +37,6 @@ import org.jetbrains.annotations.NotNull; import java.util.HashMap; import java.util.Map; -import java.util.Optional; import java.util.logging.Level; public class ChannelListener extends ListenerAdapter { @@ -81,7 +80,12 @@ public class ChannelListener extends ListenerAdapter { if(permission != null && !sender.user().perms().contains(permission)) return; - command.execute(sender, Optional.ofNullable(event.getOption(DiscordBot.ARGUMENT_NAME)).map(OptionMapping::getAsString).orElse("").split(" ")); + OptionMapping option = event.getOption(DiscordBot.ARGUMENT_NAME); + String args = ""; + if(option != null) + args = option.getAsString(); + + command.execute(sender, args.split(" ")); }); } } diff --git a/src/de/steamwar/velocitycore/discord/util/DiscordAlert.java b/src/de/steamwar/velocitycore/discord/util/DiscordAlert.java index 5f65f99..0b115d4 100644 --- a/src/de/steamwar/velocitycore/discord/util/DiscordAlert.java +++ b/src/de/steamwar/velocitycore/discord/util/DiscordAlert.java @@ -19,14 +19,15 @@ package de.steamwar.velocitycore.discord.util; +import de.steamwar.messages.Chatter; import de.steamwar.messages.Message; import de.steamwar.velocitycore.discord.DiscordBot; import de.steamwar.velocitycore.discord.channels.DiscordChannel; -import de.steamwar.messages.Chatter; import lombok.experimental.UtilityClass; import net.dv8tion.jda.api.EmbedBuilder; import net.dv8tion.jda.api.MessageBuilder; import net.dv8tion.jda.api.entities.Emoji; +import net.dv8tion.jda.api.entities.User; import net.dv8tion.jda.api.interactions.components.ActionRow; import net.dv8tion.jda.api.interactions.components.Button; @@ -37,21 +38,27 @@ import java.time.Instant; public class DiscordAlert { public static void send(Chatter player, Color color, Message title, Message description, boolean success) { - Long discordId = player.user().getDiscordId(); - if(DiscordBot.getInstance() == null || discordId == null) - return; + DiscordBot.withBot(bot -> { + Long discordId = player.user().getDiscordId(); + if(discordId == null) + return; - MessageBuilder builder = new MessageBuilder() - .setEmbeds(new EmbedBuilder() - .setAuthor("SteamWar", "https://steamwar.de", "https://cdn.discordapp.com/app-icons/869606970099904562/60c884000407c02671d91d8e7182b8a1.png") - .setColor(color) - .setTitle(player.parseToPlain(title)) - .setDescription(player.parseToPlain(description)) - .setTimestamp(Instant.now()) - .build()); - if(success) - builder.setActionRows(ActionRow.of(Button.success("tada", Emoji.fromUnicode("U+1F389")))); + User user = DiscordBot.getInstance().getJda().getUserById(discordId); + if(user == null) + return; - new DiscordChannel(player.user()).send(builder); + MessageBuilder builder = new MessageBuilder() + .setEmbeds(new EmbedBuilder() + .setAuthor("SteamWar", "https://steamwar.de", "https://cdn.discordapp.com/app-icons/869606970099904562/60c884000407c02671d91d8e7182b8a1.png") + .setColor(color) + .setTitle(player.parseToPlain(title)) + .setDescription(player.parseToPlain(description)) + .setTimestamp(Instant.now()) + .build()); + if(success) + builder.setActionRows(ActionRow.of(Button.success("tada", Emoji.fromUnicode("U+1F389")))); + + new DiscordChannel(user).send(builder); + }); } }