Add new settings system, and allow youtube-dl command to be set via it.

jda-v5
Nekojimi 1 year ago
parent 2ec6da49b5
commit 7cd118985a
  1. 14
      src/main/java/moe/nekojimi/chords/Chords.java
  2. 28
      src/main/java/moe/nekojimi/chords/CommandOptions.java
  3. 7
      src/main/java/moe/nekojimi/chords/Downloader.java
  4. 59
      src/main/java/moe/nekojimi/chords/Settings.java

@ -44,6 +44,7 @@ public final class Chords extends ListenerAdapter
{
private static final CommandOptions options = new CommandOptions();
private static Settings settings;
private final File dataDirectory;
private final File playlistsDirectory;
@ -85,7 +86,9 @@ public final class Chords extends ListenerAdapter
GatewayIntent.GUILD_MEMBERS
);
JDABuilder builder = JDABuilder.createDefault(options.getToken(), intents);
settings = new Settings(new File(options.getSettingsPath()));
JDABuilder builder = JDABuilder.createDefault(settings.getDiscordToken(), intents);
// Disable parts of the cache
builder.disableCache(CacheFlag.MEMBER_OVERRIDES);
@ -101,11 +104,11 @@ public final class Chords extends ListenerAdapter
builder.addEventListeners(listener);
builder.setAutoReconnect(true);
if (options.getLocalAddress() != null)
if (settings.getLocalAddr() != null)
{
final WebSocketFactory webSocketFactory = new WebSocketFactory();
final LocalBindSocketFactory localBindSocketFactory = new LocalBindSocketFactory();
localBindSocketFactory.setLocalAddress(InetAddress.getByName(options.getLocalAddress()));
localBindSocketFactory.setLocalAddress(InetAddress.getByName(settings.getLocalAddr()));
webSocketFactory.setSocketFactory(localBindSocketFactory);
builder.setWebsocketFactory(webSocketFactory);
}
@ -399,6 +402,11 @@ public final class Chords extends ListenerAdapter
return trackNumber;
}
public static Settings getSettings()
{
return settings;
}
private class DownloaderMessageHandler implements BiConsumer<SongRequest, Exception>
{

@ -25,20 +25,28 @@ import com.beust.jcommander.Parameter;
public class CommandOptions
{
@Parameter(names = "-token", description = "The API token for Discord.", required = true)
private String token;
@Parameter(names = "-settings", description = "A path to the settings.yml file.")
private String settingsPath = "settings.yml";
@Parameter(names = "-local-addr", description = "The local address to bind to.")
private String localAddress;
// @Parameter(names = "-token", description = "The API token for Discord.", required = true)
// private String token;
//
// @Parameter(names = "-local-addr", description = "The local address to bind to.")
// private String localAddress;
public String getToken()
{
return token;
}
// public String getToken()
// {
// return token;
// }
//
// public String getLocalAddress()
// {
// return localAddress;
// }
public String getLocalAddress()
public String getSettingsPath()
{
return localAddress;
return settingsPath;
}
}

@ -133,7 +133,7 @@ public class Downloader implements Consumer<DownloadTask>
return;
try
{
String cmd = "/usr/bin/youtube-dl --skip-download -F " + song.getUrl().toString();
String cmd = Chords.getSettings().getYtdlCommand() + " --skip-download -F " + song.getUrl().toString();
Process exec = runCommand(cmd, FORMAT_TIMEOUT);
InputStream input = exec.getInputStream();
String output = new String(input.readAllBytes(), Charset.defaultCharset());
@ -166,7 +166,7 @@ public class Downloader implements Consumer<DownloadTask>
{
try
{
String cmd = "/usr/bin/youtube-dl --skip-download --print-json " + song.getUrl().toString();
String cmd = Chords.getSettings().getYtdlCommand() + " --skip-download --print-json " + song.getUrl().toString();
Process exec = runCommand(cmd, INFO_TIMEOUT);
InputStream input = exec.getInputStream();
JsonReader reader = Json.createReader(input);
@ -213,7 +213,8 @@ public class Downloader implements Consumer<DownloadTask>
try
{
messageHandler.accept(task.request, null);
String cmd = "/usr/bin/youtube-dl -x"
String cmd = Chords.getSettings().getYtdlCommand()
+ " -x"
+ " -f " + formatCodes + "worstaudio/bestaudio/worst/best"
+ " --audio-format=wav"
+ " --no-playlist"

@ -0,0 +1,59 @@
/*
* Copyright (C) 2023 jimj316
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package moe.nekojimi.chords;
import com.amihaiemil.eoyaml.Yaml;
import com.amihaiemil.eoyaml.YamlInput;
import com.amihaiemil.eoyaml.YamlMapping;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
/**
*
* @author jimj316
*/
public class Settings
{
private final YamlMapping mapping;
public Settings(File file) throws FileNotFoundException, IOException
{
YamlInput input = Yaml.createYamlInput(file);
mapping = input.readYamlMapping();
}
public String getDiscordToken()
{
return mapping.string("discord-token");
}
public String getLocalAddr()
{
return mapping.string("local-addr");
}
public String getYtdlCommand()
{
String ret = mapping.string("ytdl-cmd");
if (ret == null)
ret = "/usr/bin/youtube-dl";
return ret;
}
}
Loading…
Cancel
Save