mirror of
https://github.com/JonasunderscoreJones/ZtereoMUSIC.git
synced 2025-10-25 13:39:19 +02:00
Merge pull request #1 from ReperakDev/code-quality
Miscellaneous code quality improvements
This commit is contained in:
commit
6e42fbfa3a
7 changed files with 117 additions and 88 deletions
|
|
@ -4,7 +4,7 @@ plugins {
|
||||||
id "io.freefair.lombok" version "6.2.0"
|
id "io.freefair.lombok" version "6.2.0"
|
||||||
}
|
}
|
||||||
|
|
||||||
mainClassName = 'codes.ztereohype.ztereomusic.Bot'
|
mainClassName = 'codes.ztereohype.ztereomusic.ZtereoMUSIC'
|
||||||
|
|
||||||
version '0.0.1'
|
version '0.0.1'
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,68 +0,0 @@
|
||||||
package codes.ztereohype.ztereomusic;
|
|
||||||
|
|
||||||
import codes.ztereohype.ztereomusic.audio.GuildMusicPlayer;
|
|
||||||
import codes.ztereohype.ztereomusic.command.Command;
|
|
||||||
import codes.ztereohype.ztereomusic.command.commands.Ping;
|
|
||||||
import codes.ztereohype.ztereomusic.command.commands.Play;
|
|
||||||
import codes.ztereohype.ztereomusic.command.commands.Skip;
|
|
||||||
import codes.ztereohype.ztereomusic.database.Config;
|
|
||||||
import codes.ztereohype.ztereomusic.listeners.CommandListener;
|
|
||||||
import com.sedmelluq.discord.lavaplayer.player.AudioPlayerManager;
|
|
||||||
import com.sedmelluq.discord.lavaplayer.player.DefaultAudioPlayerManager;
|
|
||||||
import com.sedmelluq.discord.lavaplayer.source.AudioSourceManagers;
|
|
||||||
import lombok.Getter;
|
|
||||||
import net.dv8tion.jda.api.JDA;
|
|
||||||
import net.dv8tion.jda.api.JDABuilder;
|
|
||||||
|
|
||||||
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 Bot {
|
|
||||||
private static @Getter Config config;
|
|
||||||
private static @Getter JDA bot;
|
|
||||||
|
|
||||||
private static @Getter final Map<String, Command> commandMap = new HashMap<>();
|
|
||||||
private static @Getter final Map<String, String> commandAliases = new HashMap<>();
|
|
||||||
|
|
||||||
public static AudioPlayerManager playerManager;
|
|
||||||
public static Map<Long, GuildMusicPlayer> guildMusicPlayerMap = new HashMap<>();
|
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
|
||||||
config = new Config("./config.json5");
|
|
||||||
bot = JDABuilder.createDefault(config.getPropreties().get("token"), GUILD_MESSAGES, GUILD_VOICE_STATES).build().awaitReady();
|
|
||||||
|
|
||||||
setupAudio();
|
|
||||||
setCommands();
|
|
||||||
setListeners();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void setCommands() {
|
|
||||||
Ping ping = new Ping();
|
|
||||||
commandMap.put(ping.getMeta().getName(), ping);
|
|
||||||
|
|
||||||
Play play = new Play();
|
|
||||||
commandMap.put(play.getMeta().getName(), play);
|
|
||||||
|
|
||||||
Skip skip = new Skip();
|
|
||||||
commandMap.put(skip.getMeta().getName(), skip);
|
|
||||||
|
|
||||||
for (String commandName : commandMap.keySet()) {
|
|
||||||
for (String aliasName : commandMap.get(commandName).getMeta().getAliases()) {
|
|
||||||
commandAliases.put(aliasName, commandName);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void setupAudio() {
|
|
||||||
playerManager = new DefaultAudioPlayerManager();
|
|
||||||
AudioSourceManagers.registerRemoteSources(playerManager);
|
|
||||||
AudioSourceManagers.registerLocalSource(playerManager);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void setListeners() {
|
|
||||||
bot.addEventListener(new CommandListener());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
90
src/main/java/codes/ztereohype/ztereomusic/ZtereoMUSIC.java
Normal file
90
src/main/java/codes/ztereohype/ztereomusic/ZtereoMUSIC.java
Normal file
|
|
@ -0,0 +1,90 @@
|
||||||
|
package codes.ztereohype.ztereomusic;
|
||||||
|
|
||||||
|
import codes.ztereohype.ztereomusic.audio.GuildMusicPlayer;
|
||||||
|
import codes.ztereohype.ztereomusic.command.Command;
|
||||||
|
import codes.ztereohype.ztereomusic.command.commands.Ping;
|
||||||
|
import codes.ztereohype.ztereomusic.command.commands.Play;
|
||||||
|
import codes.ztereohype.ztereomusic.command.commands.Skip;
|
||||||
|
import codes.ztereohype.ztereomusic.database.Config;
|
||||||
|
import codes.ztereohype.ztereomusic.listeners.CommandListener;
|
||||||
|
import com.sedmelluq.discord.lavaplayer.player.AudioPlayerManager;
|
||||||
|
import com.sedmelluq.discord.lavaplayer.player.DefaultAudioPlayerManager;
|
||||||
|
import com.sedmelluq.discord.lavaplayer.source.AudioSourceManagers;
|
||||||
|
import lombok.AccessLevel;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
|
import lombok.SneakyThrows;
|
||||||
|
import net.dv8tion.jda.api.JDA;
|
||||||
|
import net.dv8tion.jda.api.JDABuilder;
|
||||||
|
import net.shadew.json.JsonSyntaxException;
|
||||||
|
|
||||||
|
import javax.security.auth.login.LoginException;
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
|
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;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Setter(AccessLevel.PRIVATE)
|
||||||
|
public class ZtereoMUSIC {
|
||||||
|
public static final ZtereoMUSIC INSTANCE = new ZtereoMUSIC();
|
||||||
|
|
||||||
|
private final Map<String, Command> commandMap = new HashMap<>();
|
||||||
|
private final Map<String, String> commandAliases = new HashMap<>();
|
||||||
|
|
||||||
|
private Config config;
|
||||||
|
private JDA jda;
|
||||||
|
|
||||||
|
private AudioPlayerManager playerManager;
|
||||||
|
private Map<Long, GuildMusicPlayer> guildMusicPlayerMap = new HashMap<>();
|
||||||
|
|
||||||
|
private ZtereoMUSIC() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ZtereoMUSIC getInstance() {
|
||||||
|
return ZtereoMUSIC.INSTANCE;
|
||||||
|
}
|
||||||
|
|
||||||
|
@SneakyThrows({ JsonSyntaxException.class, FileNotFoundException.class, LoginException.class,
|
||||||
|
InterruptedException.class })
|
||||||
|
public static void main(String[] args) {
|
||||||
|
ZtereoMUSIC ztereoMUSIC = ZtereoMUSIC.getInstance();
|
||||||
|
|
||||||
|
ztereoMUSIC.setConfig(Config.loadFrom("./config.json5"));
|
||||||
|
ztereoMUSIC.setJda(JDABuilder.createDefault(ztereoMUSIC.getConfig().getPropreties().get("token"), GUILD_MESSAGES,
|
||||||
|
GUILD_VOICE_STATES).build().awaitReady());
|
||||||
|
|
||||||
|
ztereoMUSIC.setupAudio();
|
||||||
|
ztereoMUSIC.setCommands();
|
||||||
|
ztereoMUSIC.setListeners();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setCommands() {
|
||||||
|
Ping ping = new Ping();
|
||||||
|
this.getCommandMap().put(ping.getMeta().getName(), ping);
|
||||||
|
|
||||||
|
Play play = new Play();
|
||||||
|
this.getCommandMap().put(play.getMeta().getName(), play);
|
||||||
|
|
||||||
|
Skip skip = new Skip();
|
||||||
|
this.getCommandMap().put(skip.getMeta().getName(), skip);
|
||||||
|
|
||||||
|
for (String commandName : this.getCommandAliases().keySet()) {
|
||||||
|
for (String aliasName : this.getCommandMap().get(commandName).getMeta().getAliases()) {
|
||||||
|
this.getCommandAliases().put(aliasName, commandName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setupAudio() {
|
||||||
|
this.setPlayerManager(new DefaultAudioPlayerManager());
|
||||||
|
AudioSourceManagers.registerRemoteSources(this.getPlayerManager());
|
||||||
|
AudioSourceManagers.registerLocalSource(this.getPlayerManager());
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setListeners() {
|
||||||
|
this.getJda().addEventListener(new CommandListener());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
package codes.ztereohype.ztereomusic.audio;
|
package codes.ztereohype.ztereomusic.audio;
|
||||||
|
|
||||||
import codes.ztereohype.ztereomusic.Bot;
|
import codes.ztereohype.ztereomusic.ZtereoMUSIC;
|
||||||
import net.dv8tion.jda.api.entities.Guild;
|
import net.dv8tion.jda.api.entities.Guild;
|
||||||
import net.dv8tion.jda.api.entities.MessageChannel;
|
import net.dv8tion.jda.api.entities.MessageChannel;
|
||||||
import net.dv8tion.jda.api.entities.VoiceChannel;
|
import net.dv8tion.jda.api.entities.VoiceChannel;
|
||||||
|
|
@ -14,14 +14,14 @@ public class GuildMusicPlayers {
|
||||||
|
|
||||||
// If I get called in a different vc I delete the old manager
|
// If I get called in a different vc I delete the old manager
|
||||||
if (!isInSameVC) {
|
if (!isInSameVC) {
|
||||||
Bot.guildMusicPlayerMap.remove(guildId);
|
ZtereoMUSIC.getInstance().getGuildMusicPlayerMap().remove(guildId);
|
||||||
}
|
}
|
||||||
|
|
||||||
GuildMusicPlayer musicPlayer = Bot.guildMusicPlayerMap.get(guildId);
|
GuildMusicPlayer musicPlayer = ZtereoMUSIC.getInstance().getGuildMusicPlayerMap().get(guildId);
|
||||||
|
|
||||||
if (musicPlayer == null) {
|
if (musicPlayer == null) {
|
||||||
musicPlayer = new GuildMusicPlayer(Bot.playerManager, infoChannel);
|
musicPlayer = new GuildMusicPlayer(ZtereoMUSIC.getInstance().getPlayerManager(), infoChannel);
|
||||||
Bot.guildMusicPlayerMap.put(guildId, musicPlayer);
|
ZtereoMUSIC.getInstance().getGuildMusicPlayerMap().put(guildId, musicPlayer);
|
||||||
guild.getAudioManager().openAudioConnection(requestedChannel);
|
guild.getAudioManager().openAudioConnection(requestedChannel);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -33,7 +33,7 @@ public class GuildMusicPlayers {
|
||||||
// stops player, disconnects from vc, and deletes the wrapper
|
// stops player, disconnects from vc, and deletes the wrapper
|
||||||
public static void removeGuildAudioPlayer(Guild guild) {
|
public static void removeGuildAudioPlayer(Guild guild) {
|
||||||
long guildId = guild.getIdLong();
|
long guildId = guild.getIdLong();
|
||||||
GuildMusicPlayer musicPlayer = Bot.guildMusicPlayerMap.get(guildId);
|
GuildMusicPlayer musicPlayer = ZtereoMUSIC.getInstance().getGuildMusicPlayerMap().get(guildId);
|
||||||
|
|
||||||
if (musicPlayer == null) return;
|
if (musicPlayer == null) return;
|
||||||
|
|
||||||
|
|
@ -41,6 +41,6 @@ public class GuildMusicPlayers {
|
||||||
guild.getAudioManager().closeAudioConnection();
|
guild.getAudioManager().closeAudioConnection();
|
||||||
|
|
||||||
//todo: remove this when will be part of disconnection listener
|
//todo: remove this when will be part of disconnection listener
|
||||||
Bot.guildMusicPlayerMap.remove(guildId);
|
ZtereoMUSIC.getInstance().getGuildMusicPlayerMap().remove(guildId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
package codes.ztereohype.ztereomusic.command.commands;
|
package codes.ztereohype.ztereomusic.command.commands;
|
||||||
|
|
||||||
import codes.ztereohype.ztereomusic.Bot;
|
import codes.ztereohype.ztereomusic.ZtereoMUSIC;
|
||||||
import codes.ztereohype.ztereomusic.audio.GuildMusicPlayer;
|
import codes.ztereohype.ztereomusic.audio.GuildMusicPlayer;
|
||||||
import codes.ztereohype.ztereomusic.audio.GuildMusicPlayers;
|
import codes.ztereohype.ztereomusic.audio.GuildMusicPlayers;
|
||||||
import codes.ztereohype.ztereomusic.command.Command;
|
import codes.ztereohype.ztereomusic.command.Command;
|
||||||
|
|
@ -43,7 +43,7 @@ public class Play implements Command {
|
||||||
VoiceChannel voiceChannel = author.getVoiceState().getChannel();
|
VoiceChannel voiceChannel = author.getVoiceState().getChannel();
|
||||||
MessageChannel messageChannel = messageEvent.getChannel();
|
MessageChannel messageChannel = messageEvent.getChannel();
|
||||||
AudioManager manager = guild.getAudioManager();
|
AudioManager manager = guild.getAudioManager();
|
||||||
AudioPlayerManager playerManager = Bot.playerManager;
|
AudioPlayerManager playerManager = ZtereoMUSIC.getInstance().getPlayerManager();
|
||||||
|
|
||||||
// check if args merged are/have url, if so try to feed it into lava, else try to youtube api the fuck out of it.
|
// check if args merged are/have url, if so try to feed it into lava, else try to youtube api the fuck out of it.
|
||||||
String mergedArgs = String.join(" ", args);
|
String mergedArgs = String.join(" ", args);
|
||||||
|
|
|
||||||
|
|
@ -3,22 +3,29 @@ package codes.ztereohype.ztereomusic.database;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import net.shadew.json.Json;
|
import net.shadew.json.Json;
|
||||||
import net.shadew.json.JsonNode;
|
import net.shadew.json.JsonNode;
|
||||||
|
import net.shadew.json.JsonSyntaxException;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
public class Config {
|
public class Config {
|
||||||
private @Getter final Map<String, String> propreties = new HashMap<>();
|
private @Getter Map<String, String> propreties = new HashMap<>();
|
||||||
private final String path;
|
private String path;
|
||||||
|
|
||||||
|
public static Config loadFrom(String path) throws JsonSyntaxException, FileNotFoundException {
|
||||||
|
Config config = new Config();
|
||||||
|
|
||||||
public Config(String pathname) throws Exception {
|
|
||||||
Json json5 = Json.json5();
|
Json json5 = Json.json5();
|
||||||
JsonNode tree = json5.parse(new File(pathname));
|
JsonNode tree = json5.parse(new File(path));
|
||||||
this.path = pathname;
|
|
||||||
|
config.path = path;
|
||||||
|
|
||||||
for (String key : tree.keys()) {
|
for (String key : tree.keys()) {
|
||||||
propreties.put(key, tree.get(key).asString());
|
config.getPropreties().put(key, tree.get(key).asString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return config;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
package codes.ztereohype.ztereomusic.listeners;
|
package codes.ztereohype.ztereomusic.listeners;
|
||||||
|
|
||||||
import codes.ztereohype.ztereomusic.Bot;
|
import codes.ztereohype.ztereomusic.ZtereoMUSIC;
|
||||||
import codes.ztereohype.ztereomusic.command.Command;
|
import codes.ztereohype.ztereomusic.command.Command;
|
||||||
import net.dv8tion.jda.api.entities.Message;
|
import net.dv8tion.jda.api.entities.Message;
|
||||||
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
|
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
|
||||||
|
|
@ -12,9 +12,9 @@ import java.util.Map;
|
||||||
|
|
||||||
public class CommandListener extends ListenerAdapter {
|
public class CommandListener extends ListenerAdapter {
|
||||||
// TODO: load prefix from a config
|
// TODO: load prefix from a config
|
||||||
private static final String PREFIX = Bot.getConfig().getPropreties().get("prefix");
|
private static final String PREFIX = ZtereoMUSIC.getInstance().getConfig().getPropreties().get("prefix");
|
||||||
private static final Map<String, Command> COMMAND_MAP = Bot.getCommandMap();
|
private static final Map<String, Command> COMMAND_MAP = ZtereoMUSIC.getInstance().getCommandMap();
|
||||||
private static final Map<String, String> COMMAND_ALIASES = Bot.getCommandAliases();
|
private static final Map<String, String> COMMAND_ALIASES = ZtereoMUSIC.getInstance().getCommandAliases();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onMessageReceived(@Nonnull MessageReceivedEvent event) {
|
public void onMessageReceived(@Nonnull MessageReceivedEvent event) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue