Browse Source

Hier und a ein bissel

master
Paul Glaß 3 years ago
parent
commit
64f033a618
10 changed files with 67 additions and 128 deletions
  1. +1
    -2
      app/src/main/java/de/yannicpunktdee/yoshibot/command/YoshiCommand.java
  2. +1
    -1
      app/src/main/java/de/yannicpunktdee/yoshibot/command/commands/BonkCommand.java
  3. +1
    -1
      app/src/main/java/de/yannicpunktdee/yoshibot/command/commands/PatCommand.java
  4. +27
    -15
      app/src/main/java/de/yannicpunktdee/yoshibot/listeners/DiscordEventListener.java
  5. +7
    -3
      app/src/main/java/de/yannicpunktdee/yoshibot/main/YoshiBot.java
  6. +0
    -42
      app/src/main/java/de/yannicpunktdee/yoshibot/utils/RedditProvider.java
  7. +22
    -64
      app/src/main/java/de/yannicpunktdee/yoshibot/utils/Resources.java
  8. +1
    -0
      rsc/Config.properties
  9. +3
    -0
      rsc/comebacks.txt
  10. +4
    -0
      rsc/departs.txt

+ 1
- 2
app/src/main/java/de/yannicpunktdee/yoshibot/command/YoshiCommand.java View File

@ -3,7 +3,6 @@ package de.yannicpunktdee.yoshibot.command;
import de.yannicpunktdee.yoshibot.main.YoshiBot;
import de.yannicpunktdee.yoshibot.utils.Logger;
import de.yannicpunktdee.yoshibot.utils.Resources;
import lombok.NonNull;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.Message.Attachment;
import net.dv8tion.jda.api.entities.MessageEmbed;
@ -93,7 +92,7 @@ public abstract class YoshiCommand {
protected File downloadAttachmentToFile(String directoryPath, String name) {
if (directoryPath == null) directoryPath = Resources.getTempPath();
if (directoryPath == null) directoryPath = Resources.getEnsuredTempPath();
if (name == null) name = UUID.randomUUID().toString();
if (!(new File(directoryPath)).isDirectory()) {


+ 1
- 1
app/src/main/java/de/yannicpunktdee/yoshibot/command/commands/BonkCommand.java View File

@ -24,7 +24,7 @@ public class BonkCommand extends YoshiCommand {
public boolean execute() {
if(!super.execute()) return false;
File outFile = new File(Resources.getTempPath() + UUID.randomUUID().toString() + ".gif");
File outFile = new File(Resources.getEnsuredTempPath() + UUID.randomUUID().toString() + ".gif");
try {
BufferedImage inPicture = ImageIO.read(downloadAttachmentToFile(null, null));


+ 1
- 1
app/src/main/java/de/yannicpunktdee/yoshibot/command/commands/PatCommand.java View File

@ -24,7 +24,7 @@ public class PatCommand extends YoshiCommand {
public boolean execute() {
if (!super.execute()) return false;
File outFile = new File(Resources.getTempPath() + UUID.randomUUID().toString() + ".gif");
File outFile = new File(Resources.getEnsuredTempPath() + UUID.randomUUID().toString() + ".gif");
try {
BufferedImage inPicture= ImageIO.read(downloadAttachmentToFile(null, null));


+ 27
- 15
app/src/main/java/de/yannicpunktdee/yoshibot/listeners/DiscordEventListener.java View File

@ -5,6 +5,7 @@ import de.yannicpunktdee.yoshibot.command.YoshiCommandContext;
import de.yannicpunktdee.yoshibot.main.YoshiBot;
import de.yannicpunktdee.yoshibot.utils.Resources;
import net.dv8tion.jda.api.entities.ChannelType;
import net.dv8tion.jda.api.entities.VoiceChannel;
import net.dv8tion.jda.api.events.guild.voice.GuildVoiceJoinEvent;
import net.dv8tion.jda.api.events.guild.voice.GuildVoiceLeaveEvent;
import net.dv8tion.jda.api.events.guild.voice.GuildVoiceMoveEvent;
@ -48,38 +49,49 @@ public class DiscordEventListener extends ListenerAdapter {
@Override
public void onGuildVoiceJoin(@NotNull GuildVoiceJoinEvent event) {
super.onGuildVoiceJoin(event);
if (event.getMember().getUser().isBot()) return;
if(!AudioPlayerListener.isPlayingTrack()) YoshiBot.getInstance().joinVoiceChannelWithMostMembers();
if (Resources.isGreetings_and_byebyes_on()){
if (!AudioPlayerListener.isPlayingTrack()) YoshiBot.getInstance().joinVoiceChannelWithMostMembers();
if (Resources.isGreetings_and_byebyes_on()) {
String nameToPlay = event.getMember().getNickname();
if(nameToPlay == null) nameToPlay = event.getMember().getUser().getName();
if (nameToPlay == null) nameToPlay = event.getMember().getUser().getName();
YoshiBot.getInstance().sayTTS(
Resources.getRandomGreeting(nameToPlay),
event.getMember().getVoiceState().getChannel());
}
}
@Override
public void onGuildVoiceMove(@NotNull GuildVoiceMoveEvent event) {
if(!AudioPlayerListener.isPlayingTrack()) YoshiBot.getInstance().joinVoiceChannelWithMostMembers();
super.onGuildVoiceMove(event);
VoiceChannel afkChannel = YoshiBot.getInstance().getGuild().getAfkChannel();
String nameToPlay = event.getMember().getNickname();
nameToPlay = nameToPlay == null ? event.getMember().getUser().getName() : nameToPlay;
if (event.getChannelJoined() == afkChannel) {
YoshiBot.getInstance().sayTTS(Resources.getRandomAfk(nameToPlay, false), event.getChannelLeft());
} else if (event.getChannelLeft() == afkChannel) {
YoshiBot.getInstance().sayTTS(Resources.getRandomAfk(nameToPlay, true), event.getChannelJoined());
}
if (!AudioPlayerListener.isPlayingTrack()) YoshiBot.getInstance().joinVoiceChannelWithMostMembers();
}
@Override
public void onGuildVoiceLeave(@NotNull GuildVoiceLeaveEvent event) {
super.onGuildVoiceLeave(event);
if (event.getMember().getUser().isBot()) return;
if(!AudioPlayerListener.isPlayingTrack()) YoshiBot.getInstance().joinVoiceChannelWithMostMembers();
if (!AudioPlayerListener.isPlayingTrack()) YoshiBot.getInstance().joinVoiceChannelWithMostMembers();
if (Resources.isGreetings_and_byebyes_on()) {
String nameToPlay = event.getMember().getNickname();
nameToPlay = nameToPlay == null ? event.getMember().getUser().getName() : nameToPlay;
YoshiBot.getInstance().sayTTS(
Resources.getRandomByebye(nameToPlay),
event.getChannelLeft());


+ 7
- 3
app/src/main/java/de/yannicpunktdee/yoshibot/main/YoshiBot.java View File

@ -121,10 +121,12 @@ public final class YoshiBot {
allProvides.add(new StatusProvider("Valhelsia 3", Resources.getStatus_message_modded(), guild,
Resources.getStatus_update(),
25566));
allProvides.add(new StatusProvider("1.18", Resources.getStatus_message_18(), guild,
Resources.getStatus_update(),
25567));
SauceProvider.init(300);
//RedditProvider.init();
Executors.newScheduledThreadPool(1).scheduleAtFixedRate(YoshiBot::setRandomActivity, 0, 10, TimeUnit.HOURS);
@ -184,9 +186,10 @@ public final class YoshiBot {
}
}
public void joinVoiceChannelWithMostMembers() {
public VoiceChannel joinVoiceChannelWithMostMembers() {
Map<VoiceChannel, Long> vcAmount =
guild.getVoiceChannels().stream()
.filter(channel -> channel != guild.getAfkChannel())
.collect(Collectors.toMap(vc -> vc,
vc -> vc.getMembers().stream()
.filter(m -> !m.getUser().isBot())
@ -203,6 +206,7 @@ public final class YoshiBot {
Objects.requireNonNull(guild.getMember(jda.getSelfUser())));
}
joinVoiceChannel(channel);
return channel;
}
public boolean playSound(File file, VoiceChannel vc) {
@ -216,7 +220,7 @@ public final class YoshiBot {
}
public boolean sayTTS(String text, VoiceChannel vc) {
String path = Resources.getTempPath() + UUID.randomUUID() + ".opus";
String path = Resources.getEnsuredTempPath() + UUID.randomUUID() + ".opus";
try {
ProcessBuilder pb = new ProcessBuilder(


+ 0
- 42
app/src/main/java/de/yannicpunktdee/yoshibot/utils/RedditProvider.java View File

@ -1,42 +0,0 @@
package de.yannicpunktdee.yoshibot.utils;
import lombok.Getter;
import net.dean.jraw.RedditClient;
import net.dean.jraw.http.NetworkAdapter;
import net.dean.jraw.http.OkHttpNetworkAdapter;
import net.dean.jraw.http.UserAgent;
import net.dean.jraw.oauth.Credentials;
import net.dean.jraw.oauth.OAuthHelper;
public final class RedditProvider implements Provider {
@Getter
private static boolean isInit = false;
private static RedditClient reddit;
public static void init() {
if (isInit) Logger.logWarning("RedditProvider wird reinitialisiert");
String username = Resources.getRedditData().get("username");
String password = Resources.getRedditData().get("password");
String clientId = Resources.getRedditData().get("client_id");
String clientSecret = Resources.getRedditData().get("client_secret");
UserAgent userAgent = new UserAgent("bot", "de.yannicpunktdee.yoshibot", "v1", username);
Credentials creds = Credentials.script(username, password, clientId, clientSecret);
NetworkAdapter adapter = new OkHttpNetworkAdapter(userAgent);
reddit = OAuthHelper.automatic(adapter, creds);
isInit = true;
}
@Override
public void onStop() {
Logger.logInfo("Stopping RedditProvider!");
}
}

+ 22
- 64
app/src/main/java/de/yannicpunktdee/yoshibot/utils/Resources.java View File

@ -13,73 +13,35 @@ import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
public final class Resources {
@Getter
private static String resourcePath;
@Getter
private static String configPath;
@Getter
private static String audioPath;
private static String tempPath;
@Getter
private static String activitiesPath;
@Getter
private static String greetingsPath;
@Getter
private static String byebyesPath;
@Getter
private static String sauceConfigPath;
@Getter
private static String ttsPath;
@Getter
private static String patPngPath;
@Getter
private static String imagePath;
@Getter
private static String bonkPngPath;
@Getter
private static String jda_builder_string;
private static boolean greetings_and_byebyes_on;
@Getter
private static Map<String, String> redditData;
private static int status_update;
@Getter
private static Long guild_id;
@Getter
private static boolean greetings_and_byebyes_on;
private static List<String> greetings;
private static List<String> byebyes;
private static String resourcePath, configPath, audioPath, tempPath, activitiesPath, greetingsPath, byebyesPath,
sauceConfigPath, ttsPath, patPngPath, imagePath, bonkPngPath, jda_builder_string, mc_server, status_channel,
status_message_vanilla, status_message_modded, status_message_18, path_to_mcstatus, comebacksPath,
departsPath;
@Getter
private static String[] restrict_commands_to_channel;
private static List<String> greetings, byebyes, departs, comebacks;
@Getter
private static String[] filteredTags;
private static String[] restrict_commands_to_channel, filteredTags;
@Getter
private static final Map<String, List<String>> feedDetails = new HashMap<>();
@Getter
private static String mc_server;
@Getter
private static String status_channel;
@Getter
private static String status_message_vanilla;
@Getter
private static String status_message_modded;
@Getter
private static int status_update;
@Getter
private static String path_to_mcstatus;
private static Properties propertiesFile;
private static Properties redditCreds;
public synchronized static boolean init(String resourcePathArg) {
@ -98,7 +60,6 @@ public final class Resources {
if (isOk) isOk = initPatPngPath();
if (isOk) isOk = initImages();
if (isOk) isOk = initBonkPngPath();
if (isOk) isOk = initReddit();
if (isOk) isOk = initStatusMessage();
if (isOk) Logger.logInfo("Die Konfigurationen wurden erfolgreich geladen.");
@ -127,15 +88,11 @@ public final class Resources {
propertiesFile = new Properties();
propertiesFile.load(new FileInputStream(configPath));
redditCreds = new Properties();
redditCreds.load(new FileInputStream(resourcePath + "RedditCredentials.properties"));
return true;
}
private static boolean initAudio() {
audioPath = verifyExists(resourcePath + "audio/", File::isDirectory);
return audioPath != null;
return (audioPath = verifyExists(resourcePath + "audio/", File::isDirectory)) != null;
}
public static String getPathToAudioFile(String name) {
@ -161,7 +118,7 @@ public final class Resources {
}
}
public static String getTempPath() {
public static String getEnsuredTempPath() {
if (tempPath == null) {
initTemp();
}
@ -171,12 +128,6 @@ public final class Resources {
return tempPath;
}
private static boolean initReddit() {
redditData = redditCreds.stringPropertyNames().stream()
.collect(Collectors.toMap(p -> p, property -> redditCreds.getProperty(property)));
return true;
}
private static boolean initActivities() {
activitiesPath = verifyExists(resourcePath + "activities.txt", File::isFile);
return activitiesPath != null;
@ -231,12 +182,12 @@ public final class Resources {
@SneakyThrows
private static boolean initGreetingsAndByebyes() {
greetingsPath = verifyExists(resourcePath + "greetings.txt", File::isFile);
if (greetingsPath == null) {
return false;
}
byebyesPath = verifyExists(resourcePath + "byebyes.txt", File::isFile);
byebyesPath = verifyExists(resourcePath + "byebyes.txt", File::isFile);
comebacksPath = verifyExists(resourcePath + "comebacks.txt", File::isFile);
departsPath = verifyExists(resourcePath + "departs.txt", File::isFile);
if (propertiesFile.containsKey("greetings_and_byebyes_on")) {
greetings_and_byebyes_on = Boolean.parseBoolean(propertiesFile.getProperty("greetings_and_byebyes_on"));
@ -245,6 +196,8 @@ public final class Resources {
greetings = Files.readAllLines(Paths.get(greetingsPath));
byebyes = Files.readAllLines(Paths.get(byebyesPath));
comebacks = Files.readAllLines(Paths.get(comebacksPath));
departs = Files.readAllLines(Paths.get(departsPath));
return true;
}
@ -256,6 +209,10 @@ public final class Resources {
return greetings_and_byebyes_on ? String.format(getRandomFrom(byebyes), name) : null;
}
public static String getRandomAfk(String name, boolean didComeBack) {
return greetings_and_byebyes_on ? String.format(getRandomFrom(didComeBack ? comebacks : departs), name) : null;
}
private static String getRandomFrom(List<String> pool) {
return pool.get(YoshiBot.getInstance().getRandom().nextInt(pool.size()));
}
@ -331,7 +288,8 @@ public final class Resources {
status_channel = propertiesFile.getProperty("status_channel");
status_message_vanilla = propertiesFile.getProperty("status_message_vanilla");
status_message_modded = propertiesFile.getProperty("status_message_modded");
path_to_mcstatus = propertiesFile.getProperty("path_to_mcstatus");
status_message_18 = propertiesFile.getProperty("status_message_eighteen");
path_to_mcstatus = propertiesFile.getProperty("path_to_mcstatus");
try {
status_update = Integer.parseInt(propertiesFile.getProperty("status_update"));
} catch (NumberFormatException e) {


+ 1
- 0
rsc/Config.properties View File

@ -6,5 +6,6 @@ mc_server=85.214.148.23
status_channel=889880296168755231
status_message_vanilla=890007862456238120
status_message_modded=901742870552281148
status_message_eighteen=916382187023106068
status_update=30
path_to_mcstatus=/home/paul/.local/bin/mcstatus

+ 3
- 0
rsc/comebacks.txt View File

@ -0,0 +1,3 @@
%s hat jetzt fertig.
%s ist wieder aufnahmebereit.
%s hat sich wieder erholt.

+ 4
- 0
rsc/departs.txt View File

@ -0,0 +1,4 @@
%s ist dann mal abwesend.
%s hat jetzt erstmal nix mehr zu sagen.
%s ist verschwunden.
%s kommt bald wieder.... vielleicht.

Loading…
Cancel
Save