Skip to content

Commit 73db48e

Browse files
committed
Committing
1 parent 5c7c542 commit 73db48e

14 files changed

Lines changed: 175 additions & 13 deletions

File tree

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@
8080
<artifactId>jlayer</artifactId>
8181
<version>1.0.1-1</version>
8282
</dependency>
83+
<dependency>
84+
<groupId>com.github.twitch4j</groupId>
85+
<artifactId>twitch4j</artifactId>
86+
<version>1.20.0</version>
87+
</dependency>
8388
</dependencies>
8489

8590
</project>

src/main/java/simplexity/Main.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,18 @@
66
import simplexity.commands.CommandManager;
77
import simplexity.commands.ExitCommand;
88
import simplexity.commands.HelpCommand;
9+
import simplexity.commands.ReloadCommand;
910
import simplexity.config.TTSConfig;
1011
import simplexity.messages.Errors;
12+
import simplexity.twitch.TwitchClientHandler;
1113

1214
import java.util.Scanner;
1315

1416
public class Main {
1517
private static CommandManager commandManager;
1618
private static PollyHandler pollyHandler;
1719
private static SpeechHandler speechHandler;
20+
private static TwitchClientHandler twitchClientHandler;
1821

1922
public static void main(String[] args) {
2023
Scanner scanner = new Scanner(System.in);
@@ -23,31 +26,34 @@ public static void main(String[] args) {
2326
TTSConfig.getInstance().reloadConfig();
2427
pollyHandler = createPollyHandler();
2528
speechHandler = new SpeechHandler();
29+
twitchClientHandler = new TwitchClientHandler();
30+
twitchClientHandler.setupClient();
31+
twitchClientHandler.getTwitchClient().getChat().joinChannel("RhythmWeHear");
32+
System.out.println(twitchClientHandler.getTwitchClient().getChat().sendMessage("RhythmWeHear", "Test"));
2633
while (true) {
2734
String input = scanner.nextLine();
28-
if (input.equals("exit")) {
35+
if (input.equals("--exit")) {
2936
break;
3037
}
3138
if (!commandManager.runCommand(input)) {
32-
System.out.println("that is not a command");
3339
speechHandler.processSpeech(input);
3440
} else {
3541
System.out.println("command executed");
36-
3742
}
3843
}
3944
}
4045

41-
private static void registerCommands(CommandManager commandManager){
46+
private static void registerCommands(CommandManager commandManager) {
4247
commandManager.registerCommand(new HelpCommand("--help", "Displays the help messages"));
4348
commandManager.registerCommand(new ExitCommand("--exit", "Terminates the program"));
49+
commandManager.registerCommand(new ReloadCommand("--reload", "Reloads the configuration"));
4450
}
4551

4652
public static CommandManager getCommandManager() {
4753
return commandManager;
4854
}
4955

50-
public static PollyHandler createPollyHandler(){
56+
public static PollyHandler createPollyHandler() {
5157
PollyHandler pollyHandler = null;
5258
String awsAccessID = TTSConfig.getInstance().getAwsAccessID();
5359
String awsSecretKey = TTSConfig.getInstance().getAwsSecretKey();
@@ -65,6 +71,8 @@ public static PollyHandler createPollyHandler(){
6571
return pollyHandler;
6672
}
6773

74+
75+
6876
public static PollyHandler getPollyHandler() {
6977
return pollyHandler;
7078
}

src/main/java/simplexity/amazon/PollyHandler.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import com.amazonaws.ClientConfiguration;
44
import com.amazonaws.auth.BasicAWSCredentials;
5-
import com.amazonaws.client.builder.AwsClientBuilder;
65
import com.amazonaws.regions.Region;
76
import com.amazonaws.services.polly.AmazonPolly;
87
import com.amazonaws.services.polly.AmazonPollyClient;

src/main/java/simplexity/commands/HelpCommand.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package simplexity.commands;
22

33
import simplexity.Main;
4+
import simplexity.messages.Output;
5+
import simplexity.util.ConsoleColors;
46

57
import java.util.Arrays;
68

@@ -11,9 +13,10 @@ public HelpCommand(String name, String usage) {
1113

1214
@Override
1315
public void execute() {
14-
System.out.println("Help command");
16+
System.out.println(Output.HELP_HEADER);
1517
for (Command command : Main.getCommandManager().getCommands().values()) {
16-
System.out.println(command.getName() + " - " + command.getDescription());
18+
System.out.println(Output.HELP_COMMAND_MESSAGE.replace("%command_name%", command.getName())
19+
.replace("%command_description%", command.getDescription()));
1720
}
1821
}
1922
}

src/main/java/simplexity/commands/ReloadCommand.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package simplexity.commands;
22

33
import simplexity.config.TTSConfig;
4+
import simplexity.messages.Output;
45

56
public class ReloadCommand extends Command {
67
public ReloadCommand(String name, String usage) {
@@ -10,5 +11,6 @@ public ReloadCommand(String name, String usage) {
1011
@Override
1112
public void execute() {
1213
TTSConfig.getInstance().reloadConfig();
14+
System.out.println(Output.RELOAD_MESSAGE);
1315
}
1416
}

src/main/java/simplexity/config/ConfigDefaults.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,5 @@ public class ConfigDefaults {
3535
"Bri:"= "Brian"
3636
}
3737
""";
38+
public static final String TWITCH_OAUTH = "twitch-oauth= %code%";
3839
}

src/main/java/simplexity/config/SimplexityFileHandler.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package simplexity.config;
22

3+
import simplexity.messages.Errors;
4+
35
import java.io.File;
46
import java.io.FileWriter;
57

@@ -13,16 +15,16 @@ public static SimplexityFileHandler getInstance(){
1315
}
1416
return instance;
1517
}
16-
public File createOrLoadFile(){
18+
public File createOrLoadConfigFile(){
1719
File file = new File("tts-config.conf");
1820
if (file.exists()){
1921
return file;
2022
}
21-
createFile();
23+
createConfigFile();
2224
return file;
2325
}
2426

25-
private void createFile(){
27+
private void createConfigFile(){
2628
File file = new File("tts-config.conf");
2729
try (FileWriter writer = new FileWriter(file)) {
2830
writer.write(ConfigDefaults.AWS_REGION);
@@ -34,7 +36,16 @@ private void createFile(){
3436
writer.write(ConfigDefaults.DEFAULT_VOICE);
3537
writer.write(ConfigDefaults.VOICE_PREFIXES);
3638
} catch (Exception e){
37-
e.printStackTrace();
39+
System.out.println(Errors.CAUGHT_EXCEPTION.replace("%error%", e.getMessage()));
40+
}
41+
}
42+
43+
public static void createTwitchFile(String authCode){
44+
File file = new File("twitch-oauth.conf");
45+
try (FileWriter writer = new FileWriter(file)) {
46+
writer.write(ConfigDefaults.TWITCH_OAUTH.replace("%code%", authCode));
47+
} catch (Exception e){
48+
System.out.println(Errors.CAUGHT_EXCEPTION.replace("%error%", e.getMessage()));
3849
}
3950
}
4051
}

src/main/java/simplexity/config/TTSConfig.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public class TTSConfig {
1818
private VoiceId defaultVoice;
1919
private String awsAccessID, awsSecretKey, twitchChannel;
2020
private boolean connectToTwitch;
21+
private String twitchOAuth;
2122
private TTSConfig(){}
2223
private static TTSConfig instance;
2324
public static TTSConfig getInstance(){
@@ -47,13 +48,28 @@ public void reloadConfig(){
4748
reloadDefaultVoice(config);
4849
reloadStrings(config);
4950
reloadBooleans(config);
51+
reloadTwitchOAuth();
52+
}
53+
54+
public void reloadTwitchOAuth(){
55+
Config config = initializeTwitchAuth();
56+
if (config == null) return;
57+
twitchOAuth = config.getString("twitch-oauth");
5058
}
5159

5260
private Config initializeConfig() {
53-
File configFile = SimplexityFileHandler.getInstance().createOrLoadFile();
61+
File configFile = SimplexityFileHandler.getInstance().createOrLoadConfigFile();
5462
return ConfigFactory.parseFile(configFile).resolve();
5563
}
5664

65+
private Config initializeTwitchAuth(){
66+
File oauthFile = new File("twitch-oauth.conf");
67+
if(oauthFile.exists()){
68+
return ConfigFactory.parseFile(oauthFile).resolve();
69+
}
70+
return null;
71+
}
72+
5773
private void reloadReplaceText(Config config){
5874
replaceText.clear();
5975
config.getConfig("replace-text").entrySet().forEach(entry -> {
@@ -120,4 +136,8 @@ public String getTwitchChannel() {
120136
public boolean isConnectToTwitch() {
121137
return connectToTwitch;
122138
}
139+
140+
public String getTwitchOAuth() {
141+
return twitchOAuth;
142+
}
123143
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package simplexity.httpserver;
2+
3+
import com.sun.net.httpserver.HttpExchange;
4+
import com.sun.net.httpserver.HttpHandler;
5+
import simplexity.config.SimplexityFileHandler;
6+
import simplexity.messages.Errors;
7+
8+
import java.io.IOException;
9+
import java.io.OutputStream;
10+
11+
public class AuthHandler implements HttpHandler {
12+
@Override
13+
public void handle(HttpExchange exchange) {
14+
try {
15+
String twitchCode = exchange.getRequestURI().getQuery().split("=")[1];
16+
twitchCode = twitchCode.split("&")[0];
17+
System.out.println(twitchCode);
18+
SimplexityFileHandler.createTwitchFile(twitchCode);
19+
String response = "<h1>HI</h1>";
20+
exchange.sendResponseHeaders(200, response.length());
21+
OutputStream os = exchange.getResponseBody();
22+
os.write(response.getBytes());
23+
os.close();
24+
} catch (IOException e) {
25+
System.out.println(Errors.CAUGHT_EXCEPTION.replace("%error%", e.getMessage()));
26+
}
27+
}
28+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package simplexity.httpserver;
2+
3+
import com.sun.net.httpserver.HttpServer;
4+
import simplexity.messages.Errors;
5+
6+
import java.io.IOException;
7+
import java.net.InetSocketAddress;
8+
9+
public class AuthServer {
10+
11+
public static HttpServer server;
12+
13+
public static void run() {
14+
try {
15+
setupServer();
16+
} catch (Exception exception) {
17+
System.out.println(Errors.CAUGHT_EXCEPTION.replace("%error%", exception.getMessage()));
18+
}
19+
}
20+
public static void stop() {
21+
server.stop(0);
22+
}
23+
24+
private static void setupServer() {
25+
try {
26+
server = HttpServer.create(new InetSocketAddress(3000), 0);
27+
server.createContext("/", new AuthHandler());
28+
server.setExecutor(null);
29+
server.start();
30+
} catch (IOException exception) {
31+
System.out.println(Errors.CAUGHT_EXCEPTION.replace("%error%", exception.getMessage()));
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)