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.DefaultAudioPlayerManager;
import com.sedmelluq.discord.lavaplayer.source.AudioSourceManagers;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.Setter;
import net.dv8tion.jda.api.JDA;
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_VOICE_STATES;
@Getter
@Setter(AccessLevel.PRIVATE)
public class Bot {
private static @Getter Config config;
private static @Getter JDA bot;
public static final Bot INSTANCE = new Bot();
private static @Getter final Map<String, Command> commandMap = new HashMap<>();
private static @Getter final Map<String, String> commandAliases = new HashMap<>();
private final Map<String, Command> commandMap = new HashMap<>();
private final Map<String, String> commandAliases = new HashMap<>();
public static AudioPlayerManager playerManager;
public static Map<Long, GuildMusicPlayer> guildMusicPlayerMap = new HashMap<>();
private Config config;
private JDA jda;
private AudioPlayerManager playerManager;
private Map<Long, GuildMusicPlayer> guildMusicPlayerMap = new HashMap<>();
private Bot() {
}
public static Bot getInstance() {
return Bot.INSTANCE;
}
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();
Bot bot = Bot.getInstance();
setupAudio();
setCommands();
setListeners();
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();
}
public static void setCommands() {
private void setCommands() {
Ping ping = new Ping();
commandMap.put(ping.getMeta().getName(), ping);
this.getCommandMap().put(ping.getMeta().getName(), ping);
Play play = new Play();
commandMap.put(play.getMeta().getName(), play);
this.getCommandMap().put(play.getMeta().getName(), play);
Skip skip = new Skip();
commandMap.put(skip.getMeta().getName(), skip);
this.getCommandMap().put(skip.getMeta().getName(), skip);
for (String commandName : commandMap.keySet()) {
for (String aliasName : commandMap.get(commandName).getMeta().getAliases()) {
commandAliases.put(aliasName, commandName);
for (String commandName : this.getCommandAliases().keySet()) {
for (String aliasName : this.getCommandMap().get(commandName).getMeta().getAliases()) {
this.getCommandAliases().put(aliasName, commandName);
}
}
}
public static void setupAudio() {
playerManager = new DefaultAudioPlayerManager();
AudioSourceManagers.registerRemoteSources(playerManager);
AudioSourceManagers.registerLocalSource(playerManager);
private void setupAudio() {
this.setPlayerManager(new DefaultAudioPlayerManager());
AudioSourceManagers.registerRemoteSources(this.getPlayerManager());
AudioSourceManagers.registerLocalSource(this.getPlayerManager());
}
public static void setListeners() {
bot.addEventListener(new CommandListener());
private void setListeners() {
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 (!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) {
musicPlayer = new GuildMusicPlayer(Bot.playerManager, infoChannel);
Bot.guildMusicPlayerMap.put(guildId, musicPlayer);
musicPlayer = new GuildMusicPlayer(Bot.getInstance().getPlayerManager(), infoChannel);
Bot.getInstance().getGuildMusicPlayerMap().put(guildId, musicPlayer);
guild.getAudioManager().openAudioConnection(requestedChannel);
}
@ -33,7 +33,7 @@ public class GuildMusicPlayers {
// stops player, disconnects from vc, and deletes the wrapper
public static void removeGuildAudioPlayer(Guild guild) {
long guildId = guild.getIdLong();
GuildMusicPlayer musicPlayer = Bot.guildMusicPlayerMap.get(guildId);
GuildMusicPlayer musicPlayer = Bot.getInstance().getGuildMusicPlayerMap().get(guildId);
if (musicPlayer == null) return;
@ -41,6 +41,6 @@ public class GuildMusicPlayers {
guild.getAudioManager().closeAudioConnection();
//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();
MessageChannel messageChannel = messageEvent.getChannel();
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.
String mergedArgs = String.join(" ", args);

View file

@ -12,9 +12,9 @@ import java.util.Map;
public class CommandListener extends ListenerAdapter {
// TODO: load prefix from a config
private static final String PREFIX = Bot.getConfig().getPropreties().get("prefix");
private static final Map<String, Command> COMMAND_MAP = Bot.getCommandMap();
private static final Map<String, String> COMMAND_ALIASES = Bot.getCommandAliases();
private static final String PREFIX = Bot.getInstance().getConfig().getPropreties().get("prefix");
private static final Map<String, Command> COMMAND_MAP = Bot.getInstance().getCommandMap();
private static final Map<String, String> COMMAND_ALIASES = Bot.getInstance().getCommandAliases();
@Override
public void onMessageReceived(@Nonnull MessageReceivedEvent event) {