Clean up static abuse, use traditional OOP style

This commit is contained in:
Reperak 2021-11-07 20:50:56 -06:00
parent a7a0e0fae6
commit 0fa9ceef8e
No known key found for this signature in database
GPG key ID: 12F30C9BA6950C0C
4 changed files with 51 additions and 35 deletions

View file

@ -10,7 +10,9 @@ import codes.ztereohype.ztereomusic.listeners.CommandListener;
import com.sedmelluq.discord.lavaplayer.player.AudioPlayerManager; import com.sedmelluq.discord.lavaplayer.player.AudioPlayerManager;
import com.sedmelluq.discord.lavaplayer.player.DefaultAudioPlayerManager; import com.sedmelluq.discord.lavaplayer.player.DefaultAudioPlayerManager;
import com.sedmelluq.discord.lavaplayer.source.AudioSourceManagers; import com.sedmelluq.discord.lavaplayer.source.AudioSourceManagers;
import lombok.AccessLevel;
import lombok.Getter; import lombok.Getter;
import lombok.Setter;
import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder; import net.dv8tion.jda.api.JDABuilder;
@ -20,49 +22,63 @@ import java.util.Map;
import static net.dv8tion.jda.api.requests.GatewayIntent.GUILD_MESSAGES; import static net.dv8tion.jda.api.requests.GatewayIntent.GUILD_MESSAGES;
import static net.dv8tion.jda.api.requests.GatewayIntent.GUILD_VOICE_STATES; import static net.dv8tion.jda.api.requests.GatewayIntent.GUILD_VOICE_STATES;
@Getter
@Setter(AccessLevel.PRIVATE)
public class Bot { public class Bot {
private static @Getter Config config; public static final Bot INSTANCE = new Bot();
private static @Getter JDA bot;
private static @Getter final Map<String, Command> commandMap = new HashMap<>(); private final Map<String, Command> commandMap = new HashMap<>();
private static @Getter final Map<String, String> commandAliases = new HashMap<>(); private final Map<String, String> commandAliases = new HashMap<>();
public static AudioPlayerManager playerManager; private Config config;
public static Map<Long, GuildMusicPlayer> guildMusicPlayerMap = new HashMap<>(); private JDA jda;
public static void main(String[] args) throws Exception { private AudioPlayerManager playerManager;
config = new Config("./config.json5"); private Map<Long, GuildMusicPlayer> guildMusicPlayerMap = new HashMap<>();
bot = JDABuilder.createDefault(config.getPropreties().get("token"), GUILD_MESSAGES, GUILD_VOICE_STATES).build().awaitReady();
setupAudio(); private Bot() {
setCommands();
setListeners();
} }
public static void setCommands() { public static Bot getInstance() {
return Bot.INSTANCE;
}
public static void main(String[] args) throws Exception {
Bot bot = Bot.getInstance();
bot.setConfig(new Config("./config.json5"));
bot.setJda(JDABuilder.createDefault(bot.getConfig().getPropreties().get("token"), GUILD_MESSAGES,
GUILD_VOICE_STATES).build().awaitReady());
bot.setupAudio();
bot.setCommands();
bot.setListeners();
}
private void setCommands() {
Ping ping = new Ping(); Ping ping = new Ping();
commandMap.put(ping.getMeta().getName(), ping); this.getCommandMap().put(ping.getMeta().getName(), ping);
Play play = new Play(); Play play = new Play();
commandMap.put(play.getMeta().getName(), play); this.getCommandMap().put(play.getMeta().getName(), play);
Skip skip = new Skip(); Skip skip = new Skip();
commandMap.put(skip.getMeta().getName(), skip); this.getCommandMap().put(skip.getMeta().getName(), skip);
for (String commandName : commandMap.keySet()) { for (String commandName : this.getCommandAliases().keySet()) {
for (String aliasName : commandMap.get(commandName).getMeta().getAliases()) { for (String aliasName : this.getCommandMap().get(commandName).getMeta().getAliases()) {
commandAliases.put(aliasName, commandName); this.getCommandAliases().put(aliasName, commandName);
} }
} }
} }
public static void setupAudio() { private void setupAudio() {
playerManager = new DefaultAudioPlayerManager(); this.setPlayerManager(new DefaultAudioPlayerManager());
AudioSourceManagers.registerRemoteSources(playerManager); AudioSourceManagers.registerRemoteSources(this.getPlayerManager());
AudioSourceManagers.registerLocalSource(playerManager); AudioSourceManagers.registerLocalSource(this.getPlayerManager());
} }
public static void setListeners() { private void setListeners() {
bot.addEventListener(new CommandListener()); this.getJda().addEventListener(new CommandListener());
} }
} }

View file

@ -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); Bot.getInstance().getGuildMusicPlayerMap().remove(guildId);
} }
GuildMusicPlayer musicPlayer = Bot.guildMusicPlayerMap.get(guildId); GuildMusicPlayer musicPlayer = Bot.getInstance().getGuildMusicPlayerMap().get(guildId);
if (musicPlayer == null) { if (musicPlayer == null) {
musicPlayer = new GuildMusicPlayer(Bot.playerManager, infoChannel); musicPlayer = new GuildMusicPlayer(Bot.getInstance().getPlayerManager(), infoChannel);
Bot.guildMusicPlayerMap.put(guildId, musicPlayer); Bot.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 = Bot.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); Bot.getInstance().getGuildMusicPlayerMap().remove(guildId);
} }
} }

View file

@ -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 = Bot.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);

View file

@ -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 = Bot.getInstance().getConfig().getPropreties().get("prefix");
private static final Map<String, Command> COMMAND_MAP = Bot.getCommandMap(); private static final Map<String, Command> COMMAND_MAP = Bot.getInstance().getCommandMap();
private static final Map<String, String> COMMAND_ALIASES = Bot.getCommandAliases(); private static final Map<String, String> COMMAND_ALIASES = Bot.getInstance().getCommandAliases();
@Override @Override
public void onMessageReceived(@Nonnull MessageReceivedEvent event) { public void onMessageReceived(@Nonnull MessageReceivedEvent event) {