sxcybot

Discord bot for OSRS based channels
git clone git://git.wimdupont.com/sxcybot.git
Log | Files | Refs | README | LICENSE

commit 9ab9b6cb1e8029972327fc955e84189c4b538b4d
parent f158619ba834ed186b00232a7bd0800a52a92a01
Author: Wim Dupont <wim@wimdupont.com>
Date:   Thu, 21 Mar 2024 19:10:29 +0100

added editboss admin command

Diffstat:
Msrc/main/java/com/wimdupont/sxcybot/enums/Command.java | 3++-
Msrc/main/java/com/wimdupont/sxcybot/listeners/AdminCommandListener.java | 8++++++++
Asrc/main/java/com/wimdupont/sxcybot/listeners/admin/BossUpdateMultiplierListener.java | 67+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/main/java/com/wimdupont/sxcybot/services/osrs/OsrsHiscoreBossService.java | 4++++
4 files changed, 81 insertions(+), 1 deletion(-)

diff --git a/src/main/java/com/wimdupont/sxcybot/enums/Command.java b/src/main/java/com/wimdupont/sxcybot/enums/Command.java @@ -29,7 +29,8 @@ public enum Command { BANLIST("Show list of banned users."), ROLE("Add/remove a role to/from a member."), EDITROLE("Add/Delete record of roles."), - EDITPVM("Add/Edit/Delete record of the PvM Role competition"); + EDITPVM("Add/Edit/Delete record of the PvM Role competition"), + EDITBOSS("Update multiplier for a OSRS Boss of the PvM Role competition"); public final String description; diff --git a/src/main/java/com/wimdupont/sxcybot/listeners/AdminCommandListener.java b/src/main/java/com/wimdupont/sxcybot/listeners/AdminCommandListener.java @@ -4,6 +4,7 @@ import com.wimdupont.sxcybot.enums.Command; import com.wimdupont.sxcybot.enums.Command.Admin; import com.wimdupont.sxcybot.exceptions.InsufficientPrivilegesException; import com.wimdupont.sxcybot.listeners.admin.BanlistListener; +import com.wimdupont.sxcybot.listeners.admin.BossUpdateMultiplierListener; import com.wimdupont.sxcybot.listeners.admin.EditBanlistListener; import com.wimdupont.sxcybot.listeners.admin.EditPvmListener; import com.wimdupont.sxcybot.listeners.admin.EditRoleListener; @@ -38,6 +39,7 @@ public class AdminCommandListener extends ListenerAdapter { private final BanlistListener banlistListener; private final EditRoleListener editRoleListener; private final EditPvmListener editPvmListener; + private final BossUpdateMultiplierListener bossUpdateMultiplierListener; private final GuildRoleService guildRoleService; public AdminCommandListener(EditRuleListener editRuleListener, @@ -46,6 +48,7 @@ public class AdminCommandListener extends ListenerAdapter { BanlistListener banlistListener, EditRoleListener editRoleListener, EditPvmListener editPvmListener, + BossUpdateMultiplierListener bossUpdateMultiplierListener, GuildRoleService guildRoleService) { this.editRuleListener = editRuleListener; this.editBanlistListener = editBanlistListener; @@ -53,6 +56,7 @@ public class AdminCommandListener extends ListenerAdapter { this.banlistListener = banlistListener; this.editRoleListener = editRoleListener; this.editPvmListener = editPvmListener; + this.bossUpdateMultiplierListener = bossUpdateMultiplierListener; this.guildRoleService = guildRoleService; } @@ -90,6 +94,10 @@ public class AdminCommandListener extends ListenerAdapter { isPrivileged(roleStream, event, STAFF_ROLE); editPvmListener.process(event); } + case EDITBOSS -> { + isPrivileged(roleStream, event, STAFF_ROLE); + bossUpdateMultiplierListener.process(event); + } default -> { } } diff --git a/src/main/java/com/wimdupont/sxcybot/listeners/admin/BossUpdateMultiplierListener.java b/src/main/java/com/wimdupont/sxcybot/listeners/admin/BossUpdateMultiplierListener.java @@ -0,0 +1,67 @@ +package com.wimdupont.sxcybot.listeners.admin; + +import com.wimdupont.sxcybot.exceptions.EntityNotFoundException; +import com.wimdupont.sxcybot.listeners.EventWaiterUtil; +import com.wimdupont.sxcybot.services.osrs.OsrsHiscoreBossService; +import com.wimdupont.sxcybot.util.JdaUtil; +import net.dv8tion.jda.api.entities.PrivateChannel; +import net.dv8tion.jda.api.events.message.MessageReceivedEvent; +import net.dv8tion.jda.api.exceptions.RateLimitedException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Component; + +import java.util.EventListener; + +import static com.wimdupont.sxcybot.enums.Command.BOSSLIST; +import static com.wimdupont.sxcybot.util.Constants.Commands.COMMAND_PREFIX; + +@Component +public class BossUpdateMultiplierListener implements EventListener { + + private static final Logger LOGGER = LoggerFactory.getLogger(BossUpdateMultiplierListener.class); + + private final EventWaiterUtil eventWaiterUtil; + private final OsrsHiscoreBossService osrsHiscoreBossService; + + public BossUpdateMultiplierListener(EventWaiterUtil eventWaiterUtil, + OsrsHiscoreBossService osrsHiscoreBossService) { + this.eventWaiterUtil = eventWaiterUtil; + this.osrsHiscoreBossService = osrsHiscoreBossService; + } + + public void process(MessageReceivedEvent event) { + if (event.getMember() != null) { + try { + PrivateChannel privateChannel = event.getMember().getUser().openPrivateChannel().complete(true); + privateChannel.sendMessage("Name of the boss you want to update?").queue(); + eventWaiterUtil.waitForPrivateChannelEvent(nameReceiver -> { + try { + var osrsHiscoreBoss = osrsHiscoreBossService.findByName(nameReceiver.getMessage().getContentRaw()); + var oldMultiplier = osrsHiscoreBoss.getMultiplier(); + privateChannel.sendMessage(String.format("Type the multiplier for the boss of %s (current = %s).", + nameReceiver.getMessage().getContentRaw(),oldMultiplier)).queue(); + eventWaiterUtil.waitForPrivateChannelEvent(descriptionReceiver -> { + try { + osrsHiscoreBoss.setMultiplier(Double.parseDouble(descriptionReceiver.getMessage().getContentRaw())); + var saved = osrsHiscoreBossService.save(osrsHiscoreBoss); + privateChannel.sendMessage(saved + " edited.").queue(); + event.getChannel().sendMessage(String.format("Boss multiplier for boss %s has been changed from %s to %s by %s.", + saved.getName(), oldMultiplier, saved.getMultiplier(), JdaUtil.getUser(event))) + .queue(); + } catch (NumberFormatException e) { + privateChannel.sendMessage("Please enter a valid multiplier number.").queue(); + } + }, nameReceiver, privateChannel); + } catch (EntityNotFoundException e) { + privateChannel.sendMessage(e.getMessage()).queue(); + privateChannel.sendMessage(String.format("use %s%s command in the guild channel to see all bosses.", + COMMAND_PREFIX, BOSSLIST.name().toLowerCase())).queue(); + } + }, event, privateChannel); + } catch (RateLimitedException e) { + LOGGER.error(e.getMessage(), e); + } + } + } +} diff --git a/src/main/java/com/wimdupont/sxcybot/services/osrs/OsrsHiscoreBossService.java b/src/main/java/com/wimdupont/sxcybot/services/osrs/OsrsHiscoreBossService.java @@ -27,4 +27,8 @@ public class OsrsHiscoreBossService { return osrsHiscoreBossRepository.findByName(name).orElseThrow( () -> new EntityNotFoundException(String.format("No hiscoreBoss found with name %s.", name))); } + + public OsrsHiscoreBoss save(OsrsHiscoreBoss osrsHiscoreBoss) { + return osrsHiscoreBossRepository.save(osrsHiscoreBoss); + } }