sxcybot

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

commit db52689db7bfb2773248da7aaa7c5f25b3feb075
parent d020ccc18da4f7172cf2105263695c7de65bac72
Author: Wim Dupont <wim@wimdupont.com>
Date:   Sat, 30 Jul 2022 14:30:48 +0200

added monitoring for osrs hiscore updates

Diffstat:
Msrc/main/java/com/sxcy/sxcybot/client/ClientErrorHandler.java | 9++++++---
Asrc/main/java/com/sxcy/sxcybot/services/OsrsMonitoringService.java | 73+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/main/resources/logback.xml | 35+++++++++++++++++++++++++++++++++++
3 files changed, 114 insertions(+), 3 deletions(-)

diff --git a/src/main/java/com/sxcy/sxcybot/client/ClientErrorHandler.java b/src/main/java/com/sxcy/sxcybot/client/ClientErrorHandler.java @@ -1,6 +1,5 @@ package com.sxcy.sxcybot.client; -import lombok.NonNull; import lombok.RequiredArgsConstructor; import net.dv8tion.jda.api.entities.MessageChannel; import org.springframework.http.client.ClientHttpResponse; @@ -11,12 +10,16 @@ import java.io.IOException; @RequiredArgsConstructor public class ClientErrorHandler extends DefaultResponseErrorHandler { - @NonNull private final MessageChannel channel; @Override public void handleError(ClientHttpResponse response) throws IOException { - channel.sendMessage(String.format("No results found. (%s)", response.getStatusText())).queue(); + String errorMsg = String.format("No results found. (%s)", response.getStatusText()); + if (channel != null) { + channel.sendMessage(errorMsg).queue(); + } else { + throw new RuntimeException(errorMsg); + } } } diff --git a/src/main/java/com/sxcy/sxcybot/services/OsrsMonitoringService.java b/src/main/java/com/sxcy/sxcybot/services/OsrsMonitoringService.java @@ -0,0 +1,73 @@ +package com.sxcy.sxcybot.services; + +import com.sxcy.sxcybot.client.HiScoreClient; +import com.sxcy.sxcybot.exceptions.EntityNotFoundException; +import com.sxcy.sxcybot.model.OsrsBossKc; +import com.sxcy.sxcybot.repository.guild.pvmrole.dao.PvmRoleUser; +import com.sxcy.sxcybot.services.guild.pvmrole.PvmRoleUserService; +import lombok.RequiredArgsConstructor; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Service; + +import java.util.List; + +@Service +@RequiredArgsConstructor +public class OsrsMonitoringService { + + private final Logger logger = LoggerFactory.getLogger("monitor"); + private final HiScoreClient hiScoreClient; + private final PvmRoleUserService pvmRoleUserService; + + @Value("${monitor.user.name}") + private String monitorUserName; + @Value("${monitor.boss.name}") + private String monitorBossName; + @Value("${monitor.boss.kc}") + private String monitorBossKc; + + @Scheduled(cron = "${monitor.schedule}") + @SuppressWarnings("unused") + public void monitor() { + try { + monitorHiscores(); + monitorPvmRoleUserHiscores(); + } catch (Exception e) { + logger.error(e.getMessage()); + } + + } + + public void monitorPvmRoleUserHiscores() { + int errors = 0; + for (PvmRoleUser pvmRoleUser : pvmRoleUserService.findAll()) { + try { + hiScoreClient.getHiScoreBossKc(pvmRoleUser.getRsn(), null).orElseThrow(); + } catch (Exception e) { + logger.error("PvmRoleUserHiscores: no hiScores found for {}", pvmRoleUser.getRsn()); + errors++; + } + } + if (errors == 0) + logger.info("All pvmRoleUser hiscores have been found."); + } + + public void monitorHiscores() throws EntityNotFoundException { + List<OsrsBossKc> osrsBossKcs = hiScoreClient.getHiScoreBossKc(monitorUserName, null) + .orElseThrow(() -> new EntityNotFoundException(String.format("No hiscores available for %s.", monitorUserName))); + OsrsBossKc wintertodtBossKc = osrsBossKcs.stream() + .filter(f -> f.getName().equals(monitorBossName)) + .findAny() + .orElseThrow(() -> new EntityNotFoundException(String.format("No \"%s\" hiscores found.", monitorBossName))); + + if (wintertodtBossKc.getKc().equals(monitorBossKc)) { + logger.info(String.format("Hiscores check is OK. (%s has %s kc on %s)", monitorUserName, monitorBossKc, monitorBossName)); + } else { + logger.error(String.format("Hiscores check not OK: %s kc for user %s is not %s, but is %s", + monitorBossName, monitorUserName, monitorBossKc, wintertodtBossKc.getKc())); + } + } +} diff --git a/src/main/resources/logback.xml b/src/main/resources/logback.xml @@ -0,0 +1,35 @@ +<configuration> + + <property name="APP_LOG_ROOT" value="${user.home}/sxcybot/monitor/"/> + <property name="LOG_PATTERN" value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1} - %m%n"/> + + <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> + <encoder> + <pattern>${LOG_PATTERN}</pattern> + </encoder> + </appender> + + <appender name="monitor" class="ch.qos.logback.core.rolling.RollingFileAppender"> + <file>${APP_LOG_ROOT}/monitor.log</file> + <encoder> + <pattern>${LOG_PATTERN}</pattern> + </encoder> + <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy"> + <fileNamePattern>${APP_LOG_ROOT}/monitor-%i.log</fileNamePattern> + <minIndex>1</minIndex> + <maxIndex>10</maxIndex> + </rollingPolicy> + <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy"> + <maxFileSize>10MB</maxFileSize> + </triggeringPolicy> + </appender> + + <logger name="monitor" level="INFO"> + <appender-ref ref="monitor"/> + </logger> + + <root level="INFO"> + <appender-ref ref="STDOUT"/> + </root> + +</configuration>