v0.1.6 - Read/write from local settings file
This commit is contained in:
@@ -9,7 +9,7 @@ services:
|
|||||||
dockerfile: ./Dockerfile
|
dockerfile: ./Dockerfile
|
||||||
args:
|
args:
|
||||||
- JAVA_VERSION=17
|
- JAVA_VERSION=17
|
||||||
- VERSION=0.1.5
|
- VERSION=0.1.6
|
||||||
volumes:
|
volumes:
|
||||||
- ./data:/app
|
- ./data:/app
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
|
|||||||
2
pom.xml
2
pom.xml
@@ -3,7 +3,7 @@
|
|||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<groupId>com.bensherriff</groupId>
|
<groupId>com.bensherriff</groupId>
|
||||||
<artifactId>siren</artifactId>
|
<artifactId>siren</artifactId>
|
||||||
<version>0.1.5</version>
|
<version>0.1.6</version>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
<repositories>
|
<repositories>
|
||||||
|
|||||||
@@ -36,11 +36,16 @@ public class MusicBot {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static void start() throws IOException {
|
private static void start() throws IOException {
|
||||||
SettingsManager settingsManager = new SettingsManager();
|
Settings settings = SettingsManager.load();
|
||||||
Settings settings = settingsManager.load();
|
|
||||||
Listener textListener = new TextListener(settings);
|
Listener textListener = new TextListener(settings);
|
||||||
// Listener slashListener = new SlashListener(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))
|
JDA jda = JDABuilder.create(settings.getToken(), Arrays.asList(INTENTS))
|
||||||
.enableCache(Arrays.asList(ENABLED_FLAGS))
|
.enableCache(Arrays.asList(ENABLED_FLAGS))
|
||||||
.disableCache(Arrays.asList(DISABLED_FLAGS))
|
.disableCache(Arrays.asList(DISABLED_FLAGS))
|
||||||
|
|||||||
@@ -83,13 +83,13 @@ public class AudioHandler extends AudioEventAdapter implements AudioSendHandler
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onTrackException(AudioPlayer player, AudioTrack track, FriendlyException exception) {
|
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);
|
super.onTrackException(player, track, exception);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onTrackStuck(AudioPlayer player, AudioTrack track, long thresholdMs) {
|
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);
|
super.onTrackStuck(player, track, thresholdMs);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ import org.apache.logging.log4j.LogManager;
|
|||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
public abstract class Listener extends ListenerAdapter {
|
public abstract class Listener extends ListenerAdapter {
|
||||||
@@ -61,7 +62,7 @@ public abstract class Listener extends ListenerAdapter {
|
|||||||
AudioHandler audioHandler = musicManagers.get(guildId);
|
AudioHandler audioHandler = musicManagers.get(guildId);
|
||||||
|
|
||||||
if (audioHandler == null) {
|
if (audioHandler == null) {
|
||||||
LOGGER.info("Creating Audio Handler for guild " + guildId);
|
LOGGER.info("Creating Audio Handler for guild {}", guildId);
|
||||||
audioHandler = new AudioHandler(playerManager, guildId);
|
audioHandler = new AudioHandler(playerManager, guildId);
|
||||||
musicManagers.put(guildId, audioHandler);
|
musicManagers.put(guildId, audioHandler);
|
||||||
}
|
}
|
||||||
@@ -104,7 +105,7 @@ public abstract class Listener extends ListenerAdapter {
|
|||||||
audioHandler.setPaused(false);
|
audioHandler.setPaused(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void changeVolume(Guild guild, int volume) {
|
protected void changeVolume(Guild guild, int volume) throws IOException {
|
||||||
AudioHandler audioHandler = getGuildAudioPlayer(guild);
|
AudioHandler audioHandler = getGuildAudioPlayer(guild);
|
||||||
getSettings().setVolume(volume);
|
getSettings().setVolume(volume);
|
||||||
audioHandler.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 net.dv8tion.jda.api.interactions.commands.build.Commands;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
public class SlashListener extends Listener {
|
public class SlashListener extends Listener {
|
||||||
@@ -61,7 +62,12 @@ public class SlashListener extends Listener {
|
|||||||
}
|
}
|
||||||
case "volume" -> {
|
case "volume" -> {
|
||||||
int volume = Objects.requireNonNull(event.getOption("volume")).getAsInt();
|
int volume = Objects.requireNonNull(event.getOption("volume")).getAsInt();
|
||||||
|
try {
|
||||||
changeVolume(guild, volume);
|
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();
|
event.getHook().sendMessage("Set volume to " + volume).queue();
|
||||||
}
|
}
|
||||||
case "pause" -> {
|
case "pause" -> {
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
|
|||||||
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
|
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
||||||
public class TextListener extends Listener {
|
public class TextListener extends Listener {
|
||||||
@@ -47,7 +48,12 @@ public class TextListener extends Listener {
|
|||||||
channel.sendMessage("Stopped track and cleared queue").queue();
|
channel.sendMessage("Stopped track and cleared queue").queue();
|
||||||
} else if ("!volume".equals(command[0]) && command.length == 2) {
|
} else if ("!volume".equals(command[0]) && command.length == 2) {
|
||||||
int volume = Integer.parseInt(command[1]);
|
int volume = Integer.parseInt(command[1]);
|
||||||
|
try {
|
||||||
changeVolume(guild, volume);
|
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();
|
channel.sendMessage("Set volume to " + command[1]).queue();
|
||||||
} else if ("!pause".equals(command[0])) {
|
} else if ("!pause".equals(command[0])) {
|
||||||
pauseTrack(guild);
|
pauseTrack(guild);
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
package com.bensherriff.siren.settings;
|
package com.bensherriff.siren.settings;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
public class Settings {
|
public class Settings {
|
||||||
|
|
||||||
private String token = "";
|
private String token = "";
|
||||||
@@ -36,7 +38,8 @@ public class Settings {
|
|||||||
return volume;
|
return volume;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setVolume(int volume) {
|
public void setVolume(int volume) throws IOException {
|
||||||
this.volume = volume;
|
this.volume = volume;
|
||||||
|
SettingsManager.write(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,24 +1,49 @@
|
|||||||
package com.bensherriff.siren.settings;
|
package com.bensherriff.siren.settings;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectWriter;
|
||||||
import org.apache.logging.log4j.LogManager;
|
import org.apache.logging.log4j.LogManager;
|
||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileInputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
|
||||||
public class SettingsManager {
|
public class SettingsManager {
|
||||||
private static final Logger LOGGER = LogManager.getLogger(SettingsManager.class);
|
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
|
// 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);
|
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