Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor/enhance codebase quality #1777

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 31 additions & 11 deletions src/main/java/com/jagrosh/jmusicbot/BotConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import com.jagrosh.jmusicbot.utils.OtherUtil;
import com.jagrosh.jmusicbot.utils.TimeUtil;
import com.sedmelluq.discord.lavaplayer.track.AudioTrack;
import com.typesafe.config.*;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
Expand All @@ -34,21 +33,44 @@
public class BotConfig
{
private final Prompt prompt;

private final static String CONTEXT = "Config";
private final static String START_TOKEN = "/// START OF JMUSICBOT CONFIG ///";
private final static String END_TOKEN = "/// END OF JMUSICBOT CONFIG ///";

private Path path = null;
private String token, prefix, altprefix, helpWord, playlistsFolder, logLevel,
successEmoji, warningEmoji, errorEmoji, loadingEmoji, searchingEmoji,
evalEngine;
private boolean stayInChannel, songInGame, npImages, updatealerts, useEval, dbots;
private long owner, maxSeconds, aloneTimeUntilStop;
private String token;
private String prefix;
private String altprefix;
private String helpWord;
private String playlistsFolder;
private String logLevel;
private String successEmoji;
private String warningEmoji;
private String errorEmoji;
private String loadingEmoji;
private String searchingEmoji;
private String evalEngine;

private boolean stayInChannel;
private boolean songInGame;
private boolean npImages;
private boolean updatealerts;
private boolean useEval;
private boolean dbots;

private long owner;
private long maxSeconds;
private long aloneTimeUntilStop;

private int maxYTPlaylistPages;

private double skipratio;

private OnlineStatus status;
private Activity game;
private Config aliases, transforms;
private Config aliases;
private Config transforms;

private boolean valid = false;

Expand Down Expand Up @@ -360,9 +382,7 @@ public long getAloneTimeUntilStop()

public boolean isTooLong(AudioTrack track)
{
if(maxSeconds<=0)
return false;
return Math.round(track.getDuration()/1000.0) > maxSeconds;
return !maxSeconds<=0 || Math.round(track.getDuration()/1000.0) > maxSeconds;
}

public String[] getAliases(String command)
Expand Down
7 changes: 0 additions & 7 deletions src/main/java/com/jagrosh/jmusicbot/JMusicBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,13 @@
import com.jagrosh.jdautilities.command.CommandClient;
import com.jagrosh.jdautilities.command.CommandClientBuilder;
import com.jagrosh.jdautilities.commons.waiter.EventWaiter;
import com.jagrosh.jdautilities.examples.command.*;
import com.jagrosh.jmusicbot.commands.admin.*;
import com.jagrosh.jmusicbot.commands.dj.*;
import com.jagrosh.jmusicbot.commands.general.*;
import com.jagrosh.jmusicbot.commands.music.*;
import com.jagrosh.jmusicbot.commands.owner.*;
import com.jagrosh.jmusicbot.entities.Prompt;
import com.jagrosh.jmusicbot.gui.GUI;
import com.jagrosh.jmusicbot.settings.SettingsManager;
import com.jagrosh.jmusicbot.utils.OtherUtil;
import java.awt.Color;
import java.util.Arrays;
import javax.security.auth.login.LoginException;
import net.dv8tion.jda.api.*;
import net.dv8tion.jda.api.entities.Activity;
import net.dv8tion.jda.api.requests.GatewayIntent;
import net.dv8tion.jda.api.utils.cache.CacheFlag;
Expand Down
36 changes: 27 additions & 9 deletions src/main/java/com/jagrosh/jmusicbot/audio/AloneInVoiceHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,25 @@ public AloneInVoiceHandler(Bot bot)
public void init()
{
aloneTimeUntilStop = bot.getConfig().getAloneTimeUntilStop();
if(aloneTimeUntilStop > 0)
if (aloneTimeUntilStop > 0)
{
bot.getThreadpool().scheduleWithFixedDelay(() -> check(), 0, 5, TimeUnit.SECONDS);
}
}

private void check()
{
Set<Long> toRemove = new HashSet<>();
for(Map.Entry<Long, Instant> entrySet: aloneSince.entrySet())
for (Map.Entry<Long, Instant> entrySet : aloneSince.entrySet())
{
if(entrySet.getValue().getEpochSecond() > Instant.now().getEpochSecond() - aloneTimeUntilStop) continue;
if (entrySet.getValue().getEpochSecond() > Instant.now().getEpochSecond() - aloneTimeUntilStop)
{
continue;
}

Guild guild = bot.getJDA().getGuildById(entrySet.getKey());

if(guild == null)
if (guild == null)
{
toRemove.add(entrySet.getKey());
continue;
Expand All @@ -73,23 +78,36 @@ private void check()

public void onVoiceUpdate(GuildVoiceUpdateEvent event)
{
if(aloneTimeUntilStop <= 0) return;
if (aloneTimeUntilStop <= 0)
{
return;
}

Guild guild = event.getEntity().getGuild();
if(!bot.getPlayerManager().hasHandler(guild)) return;
if (!bot.getPlayerManager().hasHandler(guild))
{
return;
}

boolean alone = isAlone(guild);
boolean inList = aloneSince.containsKey(guild.getIdLong());

if(!alone && inList)
if (!alone && inList)
{
aloneSince.remove(guild.getIdLong());
else if(alone && !inList)
}
else if (alone && !inList)
{
aloneSince.put(guild.getIdLong(), Instant.now());
}
}

private boolean isAlone(Guild guild)
{
if(guild.getAudioManager().getConnectedChannel() == null) return false;
if (guild.getAudioManager().getConnectedChannel() == null)
{
return false;
}
return guild.getAudioManager().getConnectedChannel().getMembers().stream()
.noneMatch(x ->
!x.getVoiceState().isDeafened()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,15 @@

import com.jagrosh.jmusicbot.Bot;
import com.jagrosh.jmusicbot.entities.Pair;
import com.jagrosh.jmusicbot.settings.Settings;
import com.sedmelluq.discord.lavaplayer.track.AudioTrack;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.entities.Activity;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.TextChannel;
import net.dv8tion.jda.api.exceptions.PermissionException;
import net.dv8tion.jda.api.exceptions.RateLimitedException;

/**
*
Expand Down
1 change: 0 additions & 1 deletion src/main/java/com/jagrosh/jmusicbot/audio/QueuedTrack.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import com.sedmelluq.discord.lavaplayer.track.AudioTrack;
import com.sedmelluq.discord.lavaplayer.track.AudioTrackInfo;
import com.jagrosh.jmusicbot.queue.Queueable;
import net.dv8tion.jda.api.entities.User;

/**
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ public static RequestMetadata fromResultHandler(AudioTrack track, CommandEvent e

public static class RequestInfo
{
public final String query, url;
public final String query;
public final String url;
public final long startTimestamp;

public RequestInfo(String query, String url)
Expand All @@ -78,7 +79,9 @@ private static long tryGetTimestamp(String url)
public static class UserInfo
{
public final long id;
public final String username, discrim, avatar;
public final String username;
public final String discrim;
public final String avatar;

private UserInfo(long id, String username, String discrim, String avatar)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@
public class TransformativeAudioSourceManager extends YoutubeAudioSourceManager
{
private final static Logger log = LoggerFactory.getLogger(TransformativeAudioSourceManager.class);
private final String name, regex, replacement, selector, format;
private final String name;
private final String regex;
private final String replacement;
private final String selector;
private final String format;

public TransformativeAudioSourceManager(String name, Config object)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,9 @@ public AdminCommand()
{
this.category = new Category("Admin", event ->
{
if(event.getAuthor().getId().equals(event.getClient().getOwnerId()))
return true;
if(event.getGuild()==null)
return true;
return event.getMember().hasPermission(Permission.MANAGE_SERVER);
return event.getAuthor().getId().equals(event.getClient().getOwnerId())
|| event.getGuild() == null
|| event.getMember().hasPermission(Permission.MANAGE_SERVER);
});
this.guildOnly = true;
}
Expand Down
1 change: 0 additions & 1 deletion src/main/java/com/jagrosh/jmusicbot/utils/OtherUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import net.dv8tion.jda.api.entities.Activity;
import net.dv8tion.jda.api.entities.ApplicationInfo;
import net.dv8tion.jda.api.entities.User;
import okhttp3.*;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;
Expand Down