From bb17c984a23a6bc9e28719258d984a647ff09fd8 Mon Sep 17 00:00:00 2001 From: John Grosh Date: Mon, 4 Mar 2024 19:35:17 -0500 Subject: [PATCH] Codefactor Fixes (#1493) * codefactor fixes * anotha one --- scripts/run_jmusicbot.sh | 2 +- .../java/com/jagrosh/jmusicbot/BotConfig.java | 5 + .../java/com/jagrosh/jmusicbot/JMusicBot.java | 157 ++++++++++-------- .../java/com/jagrosh/jmusicbot/Listener.java | 2 +- .../jagrosh/jmusicbot/utils/OtherUtil.java | 4 +- 5 files changed, 93 insertions(+), 77 deletions(-) diff --git a/scripts/run_jmusicbot.sh b/scripts/run_jmusicbot.sh index 70a6874fe..75b94fa46 100644 --- a/scripts/run_jmusicbot.sh +++ b/scripts/run_jmusicbot.sh @@ -11,7 +11,7 @@ LOOP=true download() { if [ $DOWNLOAD == true ]; then URL=$(curl -s https://api.github.com/repos/jagrosh/MusicBot/releases/latest \ - | grep -i browser_download_url.*\.jar \ + | grep -i "browser_download_url.*\.jar" \ | sed 's/.*\(http.*\)"/\1/') FILENAME=$(echo $URL | sed 's/.*\/\([^\/]*\)/\1/') if [ -f $FILENAME ]; then diff --git a/src/main/java/com/jagrosh/jmusicbot/BotConfig.java b/src/main/java/com/jagrosh/jmusicbot/BotConfig.java index 53a155b31..a95556f8a 100644 --- a/src/main/java/com/jagrosh/jmusicbot/BotConfig.java +++ b/src/main/java/com/jagrosh/jmusicbot/BotConfig.java @@ -273,6 +273,11 @@ public Activity getGame() return game; } + public boolean isGameNone() + { + return game != null && game.getName().equalsIgnoreCase("none"); + } + public OnlineStatus getStatus() { return status; diff --git a/src/main/java/com/jagrosh/jmusicbot/JMusicBot.java b/src/main/java/com/jagrosh/jmusicbot/JMusicBot.java index a32a51048..67cff1475 100644 --- a/src/main/java/com/jagrosh/jmusicbot/JMusicBot.java +++ b/src/main/java/com/jagrosh/jmusicbot/JMusicBot.java @@ -15,6 +15,7 @@ */ package com.jagrosh.jmusicbot; +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.*; @@ -86,78 +87,8 @@ private static void startBot() EventWaiter waiter = new EventWaiter(); SettingsManager settings = new SettingsManager(); Bot bot = new Bot(waiter, config, settings); + CommandClient client = createCommandClient(config, settings, bot); - AboutCommand aboutCommand = new AboutCommand(Color.BLUE.brighter(), - "a music bot that is [easy to host yourself!](https://github.com/jagrosh/MusicBot) (v" + OtherUtil.getCurrentVersion() + ")", - new String[]{"High-quality music playback", "FairQueue™ Technology", "Easy to host yourself"}, - RECOMMENDED_PERMS); - aboutCommand.setIsAuthor(false); - aboutCommand.setReplacementCharacter("\uD83C\uDFB6"); // 🎶 - - // set up the command client - CommandClientBuilder cb = new CommandClientBuilder() - .setPrefix(config.getPrefix()) - .setAlternativePrefix(config.getAltPrefix()) - .setOwnerId(Long.toString(config.getOwnerId())) - .setEmojis(config.getSuccess(), config.getWarning(), config.getError()) - .setHelpWord(config.getHelp()) - .setLinkedCacheSize(200) - .setGuildSettingsManager(settings) - .addCommands(aboutCommand, - new PingCommand(), - new SettingsCmd(bot), - - new LyricsCmd(bot), - new NowplayingCmd(bot), - new PlayCmd(bot), - new PlaylistsCmd(bot), - new QueueCmd(bot), - new RemoveCmd(bot), - new SearchCmd(bot), - new SCSearchCmd(bot), - new ShuffleCmd(bot), - new SkipCmd(bot), - - new ForceRemoveCmd(bot), - new ForceskipCmd(bot), - new MoveTrackCmd(bot), - new PauseCmd(bot), - new PlaynextCmd(bot), - new RepeatCmd(bot), - new SkiptoCmd(bot), - new StopCmd(bot), - new VolumeCmd(bot), - - new PrefixCmd(bot), - new QueueTypeCmd(bot), - new SetdjCmd(bot), - new SkipratioCmd(bot), - new SettcCmd(bot), - new SetvcCmd(bot), - - new AutoplaylistCmd(bot), - new DebugCmd(bot), - new PlaylistCmd(bot), - new SetavatarCmd(bot), - new SetgameCmd(bot), - new SetnameCmd(bot), - new SetstatusCmd(bot), - new ShutdownCmd(bot) - ); - if(config.useEval()) - cb.addCommand(new EvalCmd(bot)); - boolean nogame = false; - if(config.getStatus()!=OnlineStatus.UNKNOWN) - cb.setStatus(config.getStatus()); - if(config.getGame()==null) - cb.useDefaultGame(); - else if(config.getGame().getName().equalsIgnoreCase("none")) - { - cb.setActivity(null); - nogame = true; - } - else - cb.setActivity(config.getGame()); if(!prompt.isNoGUI()) { @@ -183,10 +114,10 @@ else if(config.getGame().getName().equalsIgnoreCase("none")) JDA jda = JDABuilder.create(config.getToken(), Arrays.asList(INTENTS)) .enableCache(CacheFlag.MEMBER_OVERRIDES, CacheFlag.VOICE_STATE) .disableCache(CacheFlag.ACTIVITY, CacheFlag.CLIENT_STATUS, CacheFlag.EMOTE, CacheFlag.ONLINE_STATUS) - .setActivity(nogame ? null : Activity.playing("loading...")) + .setActivity(config.isGameNone() ? null : Activity.playing("loading...")) .setStatus(config.getStatus()==OnlineStatus.INVISIBLE || config.getStatus()==OnlineStatus.OFFLINE ? OnlineStatus.INVISIBLE : OnlineStatus.DO_NOT_DISTURB) - .addEventListeners(cb.build(), waiter, new Listener(bot)) + .addEventListeners(client, waiter, new Listener(bot)) .setBulkDeleteSplittingEnabled(true) .build(); bot.setJDA(jda); @@ -231,4 +162,84 @@ else if(config.getGame().getName().equalsIgnoreCase("none")) System.exit(1); } } + + private static CommandClient createCommandClient(BotConfig config, SettingsManager settings, Bot bot) + { + // instantiate about command + AboutCommand aboutCommand = new AboutCommand(Color.BLUE.brighter(), + "a music bot that is [easy to host yourself!](https://github.com/jagrosh/MusicBot) (v" + OtherUtil.getCurrentVersion() + ")", + new String[]{"High-quality music playback", "FairQueue™ Technology", "Easy to host yourself"}, + RECOMMENDED_PERMS); + aboutCommand.setIsAuthor(false); + aboutCommand.setReplacementCharacter("\uD83C\uDFB6"); // 🎶 + + // set up the command client + CommandClientBuilder cb = new CommandClientBuilder() + .setPrefix(config.getPrefix()) + .setAlternativePrefix(config.getAltPrefix()) + .setOwnerId(Long.toString(config.getOwnerId())) + .setEmojis(config.getSuccess(), config.getWarning(), config.getError()) + .setHelpWord(config.getHelp()) + .setLinkedCacheSize(200) + .setGuildSettingsManager(settings) + .addCommands(aboutCommand, + new PingCommand(), + new SettingsCmd(bot), + + new LyricsCmd(bot), + new NowplayingCmd(bot), + new PlayCmd(bot), + new PlaylistsCmd(bot), + new QueueCmd(bot), + new RemoveCmd(bot), + new SearchCmd(bot), + new SCSearchCmd(bot), + new ShuffleCmd(bot), + new SkipCmd(bot), + + new ForceRemoveCmd(bot), + new ForceskipCmd(bot), + new MoveTrackCmd(bot), + new PauseCmd(bot), + new PlaynextCmd(bot), + new RepeatCmd(bot), + new SkiptoCmd(bot), + new StopCmd(bot), + new VolumeCmd(bot), + + new PrefixCmd(bot), + new QueueTypeCmd(bot), + new SetdjCmd(bot), + new SkipratioCmd(bot), + new SettcCmd(bot), + new SetvcCmd(bot), + + new AutoplaylistCmd(bot), + new DebugCmd(bot), + new PlaylistCmd(bot), + new SetavatarCmd(bot), + new SetgameCmd(bot), + new SetnameCmd(bot), + new SetstatusCmd(bot), + new ShutdownCmd(bot) + ); + + // enable eval if applicable + if(config.useEval()) + cb.addCommand(new EvalCmd(bot)); + + // set status if set in config + if(config.getStatus() != OnlineStatus.UNKNOWN) + cb.setStatus(config.getStatus()); + + // set game + if(config.getGame() == null) + cb.useDefaultGame(); + else if(config.isGameNone()) + cb.setActivity(null); + else + cb.setActivity(config.getGame()); + + return cb.build(); + } } diff --git a/src/main/java/com/jagrosh/jmusicbot/Listener.java b/src/main/java/com/jagrosh/jmusicbot/Listener.java index 1134cafc1..2e50b793a 100644 --- a/src/main/java/com/jagrosh/jmusicbot/Listener.java +++ b/src/main/java/com/jagrosh/jmusicbot/Listener.java @@ -82,7 +82,7 @@ public void onReady(ReadyEvent event) owner.openPrivateChannel().queue(pc -> pc.sendMessage(msg).queue()); } } - catch(Exception ex) {} // ignored + catch(Exception ignored) {} // ignored }, 0, 24, TimeUnit.HOURS); } } diff --git a/src/main/java/com/jagrosh/jmusicbot/utils/OtherUtil.java b/src/main/java/com/jagrosh/jmusicbot/utils/OtherUtil.java index 63c39ce4b..9b6294933 100644 --- a/src/main/java/com/jagrosh/jmusicbot/utils/OtherUtil.java +++ b/src/main/java/com/jagrosh/jmusicbot/utils/OtherUtil.java @@ -64,7 +64,7 @@ public static Path getPath(String path) { result = Paths.get(new File(JMusicBot.class.getProtectionDomain().getCodeSource().getLocation().toURI()).getParentFile().getPath() + File.separator + path); } - catch(URISyntaxException ex) {} + catch(URISyntaxException ignored) {} } return result; } @@ -84,7 +84,7 @@ public static String loadResource(Object clazz, String name) reader.lines().forEach(line -> sb.append("\r\n").append(line)); return sb.toString().trim(); } - catch(IOException ex) + catch(IOException ignored) { return null; }