Adding config
This commit is contained in:
11
pom.xml
11
pom.xml
@@ -39,6 +39,17 @@
|
||||
<artifactId>lavaplayer</artifactId>
|
||||
<version>1.3.77</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-core</artifactId>
|
||||
<version>2.14.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
<version>2.14.0</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Logging -->
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
|
||||
42
src/main/java/com/bensherriff/siren/Config.java
Normal file
42
src/main/java/com/bensherriff/siren/Config.java
Normal file
@@ -0,0 +1,42 @@
|
||||
package com.bensherriff.siren;
|
||||
|
||||
public class Config {
|
||||
|
||||
private String token;
|
||||
private String owner;
|
||||
|
||||
private String prefix;
|
||||
private int volume;
|
||||
|
||||
public String getToken() {
|
||||
return token;
|
||||
}
|
||||
|
||||
public void setToken(String token) {
|
||||
this.token = token;
|
||||
}
|
||||
|
||||
public String getOwner() {
|
||||
return owner;
|
||||
}
|
||||
|
||||
public void setOwner(String owner) {
|
||||
this.owner = owner;
|
||||
}
|
||||
|
||||
public String getPrefix() {
|
||||
return prefix;
|
||||
}
|
||||
|
||||
public void setPrefix(String prefix) {
|
||||
this.prefix = prefix;
|
||||
}
|
||||
|
||||
public int getVolume() {
|
||||
return volume;
|
||||
}
|
||||
|
||||
public void setVolume(int volume) {
|
||||
this.volume = volume;
|
||||
}
|
||||
}
|
||||
19
src/main/java/com/bensherriff/siren/ConfigManager.java
Normal file
19
src/main/java/com/bensherriff/siren/ConfigManager.java
Normal file
@@ -0,0 +1,19 @@
|
||||
package com.bensherriff.siren;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
public class ConfigManager {
|
||||
private static final Logger LOGGER = LogManager.getLogger(ConfigManager.class);
|
||||
private final ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
public Config load() throws IOException {
|
||||
try(InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("config.json")) {
|
||||
return mapper.readValue(inputStream, Config.class);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -14,26 +14,44 @@ import net.dv8tion.jda.api.entities.VoiceChannel;
|
||||
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
|
||||
import net.dv8tion.jda.api.hooks.ListenerAdapter;
|
||||
import net.dv8tion.jda.api.managers.AudioManager;
|
||||
import net.dv8tion.jda.api.requests.GatewayIntent;
|
||||
import net.dv8tion.jda.api.utils.cache.CacheFlag;
|
||||
|
||||
import javax.security.auth.login.LoginException;
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static net.dv8tion.jda.api.requests.GatewayIntent.GUILD_MESSAGES;
|
||||
import static net.dv8tion.jda.api.requests.GatewayIntent.GUILD_VOICE_STATES;
|
||||
|
||||
public class MusicBot extends ListenerAdapter {
|
||||
public static void main(String[] args) throws Exception {
|
||||
private final static GatewayIntent[] INTENTS = {
|
||||
GatewayIntent.DIRECT_MESSAGES, GatewayIntent.GUILD_MESSAGES, GatewayIntent.GUILD_MESSAGE_REACTIONS,
|
||||
GatewayIntent.GUILD_VOICE_STATES
|
||||
};
|
||||
private final AudioPlayerManager playerManager;
|
||||
private final Map<Long, MusicManager> musicManagers;
|
||||
private final Config config;
|
||||
|
||||
JDABuilder.create("OTMyMzAxMjQ4NTQ2MzYxMzQ2.YeQ_Mg.n4H8Cl3dQ1u5aFL1ZvTmfcGwpEY", GUILD_MESSAGES, GUILD_VOICE_STATES)
|
||||
.addEventListeners(new MusicBot())
|
||||
public static void main(String[] args) throws Exception {
|
||||
start();
|
||||
}
|
||||
|
||||
private static void start() throws IOException, LoginException {
|
||||
ConfigManager configManager = new ConfigManager();
|
||||
Config config = configManager.load();
|
||||
MusicBot musicBot = new MusicBot(config);
|
||||
|
||||
JDABuilder.create(config.getToken(), Arrays.asList(INTENTS))
|
||||
.enableCache(CacheFlag.MEMBER_OVERRIDES, CacheFlag.VOICE_STATE)
|
||||
.disableCache(CacheFlag.ACTIVITY, CacheFlag.CLIENT_STATUS, CacheFlag.EMOTE)
|
||||
.addEventListeners(musicBot)
|
||||
.setBulkDeleteSplittingEnabled(true)
|
||||
.build();
|
||||
}
|
||||
|
||||
private final AudioPlayerManager playerManager;
|
||||
private final Map<Long, MusicManager> musicManagers;
|
||||
|
||||
private MusicBot() {
|
||||
private MusicBot(Config config) {
|
||||
this.musicManagers = new HashMap<>();
|
||||
this.config = config;
|
||||
|
||||
this.playerManager = new DefaultAudioPlayerManager();
|
||||
AudioSourceManagers.registerRemoteSources(playerManager);
|
||||
@@ -66,7 +84,9 @@ public class MusicBot extends ListenerAdapter {
|
||||
} else if ("!skip".equals(command[0])) {
|
||||
skipTrack(channel);
|
||||
} else if ("!stop".equals(command[0])) {
|
||||
|
||||
stop(channel);
|
||||
} else if ("!volume".equals(command[0])) {
|
||||
changeVolume(channel, command[1]);
|
||||
}
|
||||
|
||||
super.onGuildMessageReceived(event);
|
||||
@@ -110,10 +130,16 @@ public class MusicBot extends ListenerAdapter {
|
||||
|
||||
private void play(Guild guild, MusicManager musicManager, AudioTrack track) {
|
||||
connectToFirstVoiceChannel(guild.getAudioManager());
|
||||
|
||||
musicManager.scheduler.queue(track);
|
||||
}
|
||||
|
||||
private void stop(TextChannel channel) {
|
||||
MusicManager musicManager = getGuildAudioPlayer(channel.getGuild());
|
||||
musicManager.player.stopTrack();
|
||||
channel.getGuild().getAudioManager().closeAudioConnection();
|
||||
channel.sendMessage("Stopping music").queue();
|
||||
}
|
||||
|
||||
private void skipTrack(TextChannel channel) {
|
||||
MusicManager musicManager = getGuildAudioPlayer(channel.getGuild());
|
||||
musicManager.scheduler.nextTrack();
|
||||
@@ -121,6 +147,14 @@ public class MusicBot extends ListenerAdapter {
|
||||
channel.sendMessage("Skipped to next track.").queue();
|
||||
}
|
||||
|
||||
private void changeVolume(TextChannel channel, String vol) {
|
||||
MusicManager musicManager = getGuildAudioPlayer(channel.getGuild());
|
||||
int volume = Integer.parseInt(vol);
|
||||
config.setVolume(volume);
|
||||
musicManager.player.setVolume(volume);
|
||||
channel.sendMessage("Set volume to " + volume).queue();
|
||||
}
|
||||
|
||||
private static void connectToFirstVoiceChannel(AudioManager audioManager) {
|
||||
if (!audioManager.isConnected()) {
|
||||
for (VoiceChannel voiceChannel : audioManager.getGuild().getVoiceChannels()) {
|
||||
|
||||
6
src/main/resources/config.json
Normal file
6
src/main/resources/config.json
Normal file
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"token": "OTMyMzAxMjQ4NTQ2MzYxMzQ2.YeQ_Mg.n4H8Cl3dQ1u5aFL1ZvTmfcGwpEY",
|
||||
"owner": "250842261221277697",
|
||||
"prefix": "!",
|
||||
"volume": 50
|
||||
}
|
||||
Reference in New Issue
Block a user