Skip to content

Commit

Permalink
Search for GOOGLE_APPLICATION_CREDENTIALS
Browse files Browse the repository at this point in the history
  • Loading branch information
duncte123 committed Jan 6, 2022
1 parent 08c3c91 commit 9977dcf
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 5 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ lavalink:
# Configuration
The plugin exposes these configuration options
<br><b>NOTE:</b> this plugins block is a root level object, don't place it where you import the plugin
<br><b>NOTE:</b> This plugins block is a root level object, don't place it where you import the plugin
<br><b>NOTE:</b> The double quotes are required for the private key
<br><b>NOTE:</b> Having a `GOOGLE_APPLICATION_CREDENTIALS` env var set with the path to your credentials file overrides these settings
```yml
plugins:
dunctebot:
tts:
clientEmail: '' # The client_email from your service account json
privateKey: '' # The private_key from your service account json
clientEmail: "" # The client_email from your service account json
privateKey: "" # The private_key from your service account json
```

# Usage
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ plugins {
}

group "com.dunctebot"
version "1.0.0"
version "1.0.1"
mainClassName = "org.springframework.boot.loader.JarLauncher"
sourceCompatibility = 11
compileJava.options.encoding = "UTF-8"
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/com/dunctebot/tss/TTSPlugin.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package com.dunctebot.tss;

import com.sedmelluq.discord.lavaplayer.player.AudioPlayerManager;
import com.sedmelluq.discord.lavaplayer.tools.JsonBrowser;
import dev.arbjerg.lavalink.api.AudioPlayerManagerConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.File;
import java.nio.file.Files;

@Service
public class TTSPlugin implements AudioPlayerManagerConfiguration {
private static final Logger LOG = LoggerFactory.getLogger(TTSPlugin.class);
Expand All @@ -15,6 +19,8 @@ public class TTSPlugin implements AudioPlayerManagerConfiguration {
public TTSPlugin(LavalinkTTSConfig config) throws Exception {
LOG.info("Loading GCLOUD TTS plugin");

this.checkEnvVar(config);

if (StringUtils.isEmpty(config.getClientEmail())) {
LOG.error("Client email is not set in the config, aborting. Config key is 'plugins.dunctebot.tts.clientEmail'");
return;
Expand All @@ -36,4 +42,23 @@ public AudioPlayerManager configure(AudioPlayerManager manager) {

return manager;
}

private void checkEnvVar(LavalinkTTSConfig config) {
final String googleCreds = System.getenv("GOOGLE_APPLICATION_CREDENTIALS");

if (googleCreds == null) {
return;
}

try {
final String json = Files.readString(new File(googleCreds).toPath());
final JsonBrowser browser = JsonBrowser.parse(json);

config.setClientEmail(browser.get("client_email").text());
config.setPrivateKey(browser.get("private_key").text());
} catch (Exception e) {
// Fail silently?
LOG.error("Found google application credentials but could not parse json", e);
}
}
}
2 changes: 1 addition & 1 deletion src/main/resources/lavalink-plugins/tts.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
name=Google Cloud TTS
path=com.dunctebot.tss
version=1.0.0
version=1.0.1

0 comments on commit 9977dcf

Please sign in to comment.