v0.1.6 - Read/write from local settings file
This commit is contained in:
@@ -36,11 +36,16 @@ public class MusicBot {
|
||||
}
|
||||
|
||||
private static void start() throws IOException {
|
||||
SettingsManager settingsManager = new SettingsManager();
|
||||
Settings settings = settingsManager.load();
|
||||
Settings settings = SettingsManager.load();
|
||||
Listener textListener = new TextListener(settings);
|
||||
// Listener slashListener = new SlashListener(settings);
|
||||
|
||||
if (settings.getToken() == null || settings.getToken().isEmpty()) {
|
||||
throw new IOException("Token field may not be empty, please set the value in " + SettingsManager.PATH);
|
||||
} else if (settings.getOwner() == null || settings.getOwner().isEmpty()) {
|
||||
throw new IOException("Owner field may not be empty, please set the value in " + SettingsManager.PATH);
|
||||
}
|
||||
|
||||
JDA jda = JDABuilder.create(settings.getToken(), Arrays.asList(INTENTS))
|
||||
.enableCache(Arrays.asList(ENABLED_FLAGS))
|
||||
.disableCache(Arrays.asList(DISABLED_FLAGS))
|
||||
|
||||
@@ -83,13 +83,13 @@ public class AudioHandler extends AudioEventAdapter implements AudioSendHandler
|
||||
|
||||
@Override
|
||||
public void onTrackException(AudioPlayer player, AudioTrack track, FriendlyException exception) {
|
||||
LOGGER.warn("Exception on track" + track.getInfo().title + ": " + exception.getMessage());
|
||||
LOGGER.warn("Exception on track '{}': {}", track.getInfo().title, exception.getMessage());
|
||||
super.onTrackException(player, track, exception);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTrackStuck(AudioPlayer player, AudioTrack track, long thresholdMs) {
|
||||
LOGGER.warn(guildID + " " + track.getInfo().title + " is stuck");
|
||||
LOGGER.warn("{} - 'track {}' is stuck", guildID, track.getInfo().title);
|
||||
super.onTrackStuck(player, track, thresholdMs);
|
||||
}
|
||||
|
||||
|
||||
@@ -17,6 +17,7 @@ import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.*;
|
||||
|
||||
public abstract class Listener extends ListenerAdapter {
|
||||
@@ -61,7 +62,7 @@ public abstract class Listener extends ListenerAdapter {
|
||||
AudioHandler audioHandler = musicManagers.get(guildId);
|
||||
|
||||
if (audioHandler == null) {
|
||||
LOGGER.info("Creating Audio Handler for guild " + guildId);
|
||||
LOGGER.info("Creating Audio Handler for guild {}", guildId);
|
||||
audioHandler = new AudioHandler(playerManager, guildId);
|
||||
musicManagers.put(guildId, audioHandler);
|
||||
}
|
||||
@@ -104,7 +105,7 @@ public abstract class Listener extends ListenerAdapter {
|
||||
audioHandler.setPaused(false);
|
||||
}
|
||||
|
||||
protected void changeVolume(Guild guild, int volume) {
|
||||
protected void changeVolume(Guild guild, int volume) throws IOException {
|
||||
AudioHandler audioHandler = getGuildAudioPlayer(guild);
|
||||
getSettings().setVolume(volume);
|
||||
audioHandler.setVolume(volume);
|
||||
|
||||
@@ -13,6 +13,7 @@ import net.dv8tion.jda.api.interactions.commands.OptionType;
|
||||
import net.dv8tion.jda.api.interactions.commands.build.Commands;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Objects;
|
||||
|
||||
public class SlashListener extends Listener {
|
||||
@@ -61,7 +62,12 @@ public class SlashListener extends Listener {
|
||||
}
|
||||
case "volume" -> {
|
||||
int volume = Objects.requireNonNull(event.getOption("volume")).getAsInt();
|
||||
changeVolume(guild, volume);
|
||||
try {
|
||||
changeVolume(guild, volume);
|
||||
} catch (IOException ex) {
|
||||
event.getHook().sendMessage("Unable to set the volume").queue();
|
||||
LOGGER.error(ex.getMessage());
|
||||
}
|
||||
event.getHook().sendMessage("Set volume to " + volume).queue();
|
||||
}
|
||||
case "pause" -> {
|
||||
|
||||
@@ -12,6 +12,7 @@ import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
|
||||
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
|
||||
public class TextListener extends Listener {
|
||||
@@ -47,7 +48,12 @@ public class TextListener extends Listener {
|
||||
channel.sendMessage("Stopped track and cleared queue").queue();
|
||||
} else if ("!volume".equals(command[0]) && command.length == 2) {
|
||||
int volume = Integer.parseInt(command[1]);
|
||||
changeVolume(guild, volume);
|
||||
try {
|
||||
changeVolume(guild, volume);
|
||||
} catch (IOException ex) {
|
||||
channel.sendMessage("Unable to update the settings file.").queue();
|
||||
LOGGER.error(ex.getMessage());
|
||||
}
|
||||
channel.sendMessage("Set volume to " + command[1]).queue();
|
||||
} else if ("!pause".equals(command[0])) {
|
||||
pauseTrack(guild);
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package com.bensherriff.siren.settings;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class Settings {
|
||||
|
||||
private String token = "";
|
||||
@@ -36,7 +38,8 @@ public class Settings {
|
||||
return volume;
|
||||
}
|
||||
|
||||
public void setVolume(int volume) {
|
||||
public void setVolume(int volume) throws IOException {
|
||||
this.volume = volume;
|
||||
SettingsManager.write(this);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,24 +1,49 @@
|
||||
package com.bensherriff.siren.settings;
|
||||
|
||||
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.ObjectWriter;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
public class SettingsManager {
|
||||
private static final Logger LOGGER = LogManager.getLogger(SettingsManager.class);
|
||||
private final ObjectMapper mapper = new ObjectMapper();
|
||||
public static final String SEPARATOR = File.separator;
|
||||
public static final String PATH = String.join(SEPARATOR, System.getProperty("user.dir"), "settings.json");
|
||||
private static final ObjectMapper mapper = new ObjectMapper();
|
||||
private static final ObjectWriter writer = mapper.writer(new DefaultPrettyPrinter());
|
||||
|
||||
public Settings load() throws IOException {
|
||||
public static Settings load() throws IOException {
|
||||
return load(PATH);
|
||||
}
|
||||
|
||||
public static Settings load(String path) throws IOException {
|
||||
// If settings is not available, create new default settings
|
||||
try(InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("settings.json")) {
|
||||
// try(InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("settings.json")) {
|
||||
// return mapper.readValue(inputStream, Settings.class);
|
||||
// }
|
||||
File file = new File(path);
|
||||
if (!file.exists()) {
|
||||
LOGGER.warn("Settings file does not exist, creating new file at: {}", file.getPath());
|
||||
write(new Settings());
|
||||
}
|
||||
LOGGER.info("Reading settings from {}", file.getPath());
|
||||
try (InputStream inputStream = new FileInputStream(file)) {
|
||||
return mapper.readValue(inputStream, Settings.class);
|
||||
}
|
||||
}
|
||||
|
||||
public void write(Settings settings) throws IOException {
|
||||
public static void write(Settings settings) throws IOException {
|
||||
write(PATH, settings);
|
||||
}
|
||||
|
||||
public static void write(String path, Settings settings) throws IOException {
|
||||
File file = new File(path);
|
||||
writer.writeValue(file, settings);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +0,0 @@
|
||||
{
|
||||
"token": "OTMyMzAxMjQ4NTQ2MzYxMzQ2.YeQ_Mg.n4H8Cl3dQ1u5aFL1ZvTmfcGwpEY",
|
||||
"owner": "250842261221277697",
|
||||
"prefix": "!",
|
||||
"volume": 30
|
||||
}
|
||||
Reference in New Issue
Block a user