public void cancelTimeout(User user, Server server, Channel invocationChannel) { String serverId = server.getId(); TempServerConfig serverConfig = serverStorage.get(serverId); if (serverConfig == null) { serverConfig = new TempServerConfig(serverId); serverStorage.put(serverId, serverConfig); } ServerTimeoutStorage storage = serverConfig.getServerTimeouts(); removeTimeoutRole(user, server, apiClient.getChannelById(serverId)); if (storage != null) { ServerTimeout timeout = storage.getTimeouts().remove(user.getId()); saveServerConfig(serverConfig); if (timeout != null) { SafeNav.of(timeout.getTimerFuture()).ifPresent(f -> f.cancel(true)); LOGGER.info( "Cancelling timeout for {} ({}) in {} ({})", user.getUsername(), user.getId(), server.getName(), serverId); apiClient.sendMessage( loc.localize("commands.mod.stoptimeout.response", user.getUsername(), user.getId()), invocationChannel); return; } } LOGGER.warn( "Unable to cancel: cannot find server or timeout entry for {} ({}) in {} ({})", user.getUsername(), user.getId(), server.getName(), server.getId()); apiClient.sendMessage( loc.localize( "commands.mod.stoptimeout.response.not_found", user.getUsername(), user.getId()), invocationChannel); }
public void loadServerConfig(Path path) { boolean purge = false; TempServerConfig config; ServerTimeoutStorage storage; try (Reader reader = Files.newBufferedReader(path, UTF_8)) { config = gson.fromJson(reader, TempServerConfig.class); serverStorage.put(config.getServerId(), config); storage = config.getServerTimeouts(); if (storage != null) { Server server = apiClient.getServerByID(config.getServerId()); if (server == NO_SERVER) { LOGGER.warn("Rejecting {} server storage file: server not found", config.getServerId()); return; } LOGGER.info( "Loaded {} ({}) server storage file", server.getName(), server.getId(), storage.getTimeoutRoleId()); // Prune expired entries for (Iterator<Map.Entry<String, ServerTimeout>> iter = storage.getTimeouts().entrySet().iterator(); iter.hasNext(); ) { Map.Entry<String, ServerTimeout> e = iter.next(); ServerTimeout timeout = e.getValue(); String userId = timeout.getUserId(); User user = apiClient.getUserById(userId, server); if (!isUserTimedOut(userId, server.getId())) { // Purge! purge = true; if (user == NO_USER) { LOGGER.info( "Ending timeout for departed user {} ({}) in {} ({})", timeout.getLastUsername(), userId, server.getName(), server.getId()); // // apiClient.sendMessage(loc.localize("message.mod.timeout.expire.not_found", // user.getId()), // server.getId()); // Don't need to remove the timeout role because leaving does that for us } else { // Duplicated from onTimeoutExpire except without remove since we're removing in an // iter LOGGER.info( "Expiring timeout for {} ({}) in {} ({})", user.getUsername(), user.getId(), server.getName(), server.getId()); // Only send message if they still have the role if (removeTimeoutRole(user, server, apiClient.getChannelById(server.getId()))) { // // apiClient.sendMessage(loc.localize("message.mod.timeout.expire", // user.getId()), // server.getId()); } } SafeNav.of(timeout.getTimerFuture()).ifPresent(f -> f.cancel(true)); iter.remove(); } else { // Start our futures Duration duration = Duration.between(Instant.now(), timeout.getEndTime()); ScheduledFuture future = timeoutService.schedule( () -> onTimeoutExpire(user, server), duration.getSeconds(), TimeUnit.SECONDS); timeout.setTimerFuture(future); } } } } catch (IOException | JsonParseException e) { LOGGER.warn("Unable to load server storage file " + path.toString(), e); return; } if (purge) { saveServerConfig(config); } }