feat: begun implementing Time Planner feature

This commit is contained in:
limited_dev 2022-09-29 23:58:10 +02:00
parent fec77bfbab
commit 35097041c0
9 changed files with 235 additions and 8 deletions

View file

@ -1,6 +1,8 @@
package de.limited_dev.lil_judd;
import de.limited_dev.lil_judd.commands.components.CommandManager;
import de.limited_dev.lil_judd.features.TimePlanner;
import de.limited_dev.lil_judd.features.storage.GuildTimePlannerStorage;
import de.limited_dev.lil_judd.listeners.GuildStateListener;
import de.limited_dev.lil_judd.listeners.ReadyListener;
import de.limited_dev.lil_judd.listeners.SlashCommandInteractionListener;
@ -27,6 +29,7 @@ public class Main {
tokenManager.load();
jda = JDABuilder.createDefault(tokenManager.getToken())
.setActivity(Activity.watching("You"))
.setStatus(OnlineStatus.DO_NOT_DISTURB)
@ -39,6 +42,9 @@ public class Main {
jda.addEventListener(new GuildStateListener());
GuildTimePlannerStorage.getInstance().load();
TimePlanner.registerMessageThread();
try {
jda.awaitReady();
} catch (InterruptedException e) {
@ -55,4 +61,8 @@ public class Main {
public static Logger getLgr() {
return lgr;
}
public static long getLaunchTime() {
return launchTime;
}
}

View file

@ -2,6 +2,7 @@ package de.limited_dev.lil_judd.commands;
import de.limited_dev.lil_judd.commands.components.Command;
import de.limited_dev.lil_judd.commands.components.Option;
import de.limited_dev.lil_judd.features.TimePlanner;
import de.limited_dev.lil_judd.util.EmbeddedMessageHelper;
import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
@ -17,6 +18,11 @@ public class SetupFeatureCommand extends Command {
EmbeddedMessageHelper.sendSimpleOneLiner(event, "You do not have the Permission", "Sorry, but you don't have the Permission to run this command", null);
return;
}
if(event.getOption("feature").getAsString().equals("timefinder")){
TimePlanner.getGtps().guildsWithPlanner.put(event.getGuild().getIdLong(), event.getOption("channel").getAsChannel().getIdLong());
TimePlanner.getGtps().save();
EmbeddedMessageHelper.sendSimpleOneLiner(event, "Channel has been selected for the ritual",
"The Channel has been added. You may now recieve notifications", null);
}
}
}

View file

@ -0,0 +1,74 @@
package de.limited_dev.lil_judd.features;
import de.limited_dev.lil_judd.Main;
import de.limited_dev.lil_judd.features.storage.GuildTimePlannerStorage;
import de.limited_dev.lil_judd.util.EmbeddedMessageHelper;
import de.limited_dev.lil_judd.util.Logger;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.Guild;
import java.time.Duration;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class TimePlanner {
private static final GuildTimePlannerStorage gtps = GuildTimePlannerStorage.getInstance();
private static final Logger lgr = Main.getLgr();
public static GuildTimePlannerStorage getGtps() {
return gtps;
}
public static void registerMessageThread(){
// get the current ZonedDateTime of your TimeZone
ZonedDateTime now = ZonedDateTime.now(ZoneId.of("Europe/Berlin"));//.withHour(8).withMinute(0);
// set the ZonedDateTime of the first lesson at 8:05
//ZonedDateTime nextNotifyDay = now.plusDays(DayUtil.getDelay(now.getDayOfWeek().name()));
ZonedDateTime nextNotifyDay = now.plusSeconds(10);
lgr.info(now.getDayOfMonth() + "");
//lgr.info(now.getDayOfWeek().name() + " " + nextNotifyDay.getDayOfWeek().name());
// if it's already past the time (in this case 8:05) the first lesson will be scheduled for the next day
if (now.compareTo(nextNotifyDay) > 0) {
nextNotifyDay = nextNotifyDay.plusDays(7);
lgr.info("See you next week");
}
// duration between now and the beginning of the next first lesson
Duration timeUntilNextNotification = Duration.between(now, nextNotifyDay);
// in seconds
long initialDelayUntilNextNotification = timeUntilNextNotification.getSeconds();
lgr.info("Duration: " + initialDelayUntilNextNotification);
// schedules the reminder at a fixed rate of one day
ScheduledExecutorService schedulerFirstLesson = Executors.newScheduledThreadPool(1);
schedulerFirstLesson.scheduleAtFixedRate(() -> {
// send a message
String msg = "<Msg Content>";
JDA jda = Main.getJda();
Set<Long> gs = gtps.guildsWithPlanner.keySet();
lgr.info(gs.toString());
for (Long l : gs) {
//guild.getDefaultChannel().sendMessage(message).queue();
//jda.getGuildById(l).getTextChannelById(gtps.getChannelID(l)).sendMessage("").queue();
EmbeddedMessageHelper.sendSimpleOneLiner(jda.getGuildById(l), gtps.guildsWithPlanner.get(l), "You got time?", "Ain't nobody got time for that?", null);
lgr.info("Send Code to Server " + jda.getGuildById(l).getName());
}
lgr.info("Send Push Notification for time Management");
},
initialDelayUntilNextNotification,
TimeUnit.SECONDS.toSeconds(10),//TimeUnit.DAYS.toSeconds(7),
TimeUnit.SECONDS);
}
}

View file

@ -0,0 +1,78 @@
package de.limited_dev.lil_judd.features.storage;
import de.limited_dev.lil_judd.Main;
import net.dv8tion.jda.api.entities.Guild;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
public class GuildTimePlannerStorage {
private static GuildTimePlannerStorage guildTimePlannerStorage;
private final String basePath = "./data/GuildTimePlanner/";
private final String filename = "guildsLinkedToChannels.judd";
private final String filePath = basePath + filename;
public Map<Long, Long> guildsWithPlanner = new HashMap<>();
public static GuildTimePlannerStorage getInstance(){
if(guildTimePlannerStorage == null){
guildTimePlannerStorage = new GuildTimePlannerStorage();
}
return guildTimePlannerStorage;
}
public void load(){
File dir = new File(basePath);
if(!dir.exists()){
save();
return;
}
File file = new File(dir, filename);
if(!file.exists()){
save();
return;
}
try{
InputStream input = new FileInputStream(filePath);
Properties prop = new Properties();
prop.load(input);
for(Object o : prop.keySet()){
guildsWithPlanner.put(Long.parseLong(o.toString()), Long.parseLong(prop.get(o).toString()));
Main.getLgr().info("key: " + o.toString() + ", val: " + prop.getProperty(o.toString()));
}
} catch (IOException e) {
e.printStackTrace();
}
}
public void save(){
File dir = new File(basePath);
if(!dir.exists()){
dir.mkdirs();
}
File file = new File(dir, filename);
if(!file.exists()){
try{
file.createNewFile();
} catch(IOException e){
e.printStackTrace();
}
}
try{
OutputStream output = new FileOutputStream(filePath);
Properties prop = new Properties();
//prop.setProperty("token", "empty");
for(long l : guildsWithPlanner.keySet()){
prop.setProperty(String.valueOf(l), guildsWithPlanner.get(l).toString());
}
prop.store(output, null);
} catch (IOException e) {
e.printStackTrace();
}
}
}

View file

@ -15,8 +15,8 @@ public class GuildStateListener extends ListenerAdapter {
@Override
public void onGuildJoin(@NotNull GuildJoinEvent event) {
lgr.info("I have been added to a guild.");
Guild g = event.getGuild();
SlashCommandHelper.addSlashCommandsToGuild(g);
//Guild g = event.getGuild();
//SlashCommandHelper.addSlashCommandsToGuild(g);
}
@Override

View file

@ -1,17 +1,36 @@
package de.limited_dev.lil_judd.listeners;
import de.limited_dev.lil_judd.Main;
import de.limited_dev.lil_judd.features.TimePlanner;
import de.limited_dev.lil_judd.features.storage.GuildTimePlannerStorage;
import de.limited_dev.lil_judd.util.DayUtil;
import de.limited_dev.lil_judd.util.EmbeddedMessageHelper;
import de.limited_dev.lil_judd.util.Logger;
import de.limited_dev.lil_judd.util.SlashCommandHelper;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.events.ReadyEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import org.jetbrains.annotations.NotNull;
import java.time.Duration;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Set;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class ReadyListener extends ListenerAdapter {
private final Logger lgr = Main.getLgr();
private final GuildTimePlannerStorage gtps = TimePlanner.getGtps();
@Override
public void onReady(@NotNull ReadyEvent event) {
String usernameOfSelf = Main.getJda().getSelfUser().getName();
SlashCommandHelper.addSlashCommandsToGuild();
lgr.info("Logged into: " + usernameOfSelf);
lgr.info(usernameOfSelf + " is ready for Freddy");
}

View file

@ -0,0 +1,20 @@
package de.limited_dev.lil_judd.util;
import java.util.HashMap;
import java.util.Map;
public class DayUtil {
private static final Map<String, Integer> DaysUntilMonday = new HashMap<>(){{
put("MONDAY", 0);
put("TUESDAY", 6);
put("WEDNESDAY", 5);
put("THURSDAY", 4);
put("FRIDAY", 3);
put("SATURDAY", 2);
put("SUNDAY", 1);
}};
public static int getDelay(String day){
return DaysUntilMonday.get(day);
}
}

View file

@ -2,6 +2,8 @@ package de.limited_dev.lil_judd.util;
import de.limited_dev.lil_judd.Main;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
import net.dv8tion.jda.api.events.Event;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
@ -25,4 +27,18 @@ public class EmbeddedMessageHelper {
eb.setFooter(">" + dtf.format(now) + " - " + event.getUser().getName() + "#" + event.getUser().getDiscriminator());
event.replyEmbeds(eb.build()).queue();
}
public static void sendSimpleOneLiner(Guild g, long channelID, String title, String description, String thumbnailURL){
LocalDateTime now = LocalDateTime.now();
EmbedBuilder eb = new EmbedBuilder();
eb.setAuthor(Main.getJda().getSelfUser().getName());
eb.setTitle(title);
eb.setColor(Color.ORANGE);
eb.setDescription(description);
if(thumbnailURL != null)
eb.setThumbnail(thumbnailURL);
eb.setFooter(">" + dtf.format(now) + " - Automated Message");
g.getTextChannelById(channelID).sendMessageEmbeds(eb.build()).queue();
}
}

View file

@ -1,5 +1,6 @@
package de.limited_dev.lil_judd.util;
import de.limited_dev.lil_judd.Main;
import de.limited_dev.lil_judd.commands.components.Command;
import de.limited_dev.lil_judd.commands.components.CommandManager;
import de.limited_dev.lil_judd.commands.components.Option;
@ -10,15 +11,18 @@ import net.dv8tion.jda.api.interactions.commands.build.OptionData;
public class SlashCommandHelper {
public static void addSlashCommandsToGuild(Guild g){
g.updateCommands().addCommands(
public static void addSlashCommandsToGuild(){
Main.getJda().updateCommands().addCommands(
Commands.slash("ping", "Pong! Display the Ping"),
Commands.slash("info", "Shows Info about me"),
Commands.slash("setup", "Setup a feature")
.addOptions(
new OptionData(OptionType.STRING, "feature", "The Feature you want to setup")
.addChoice("Send Time finder", "timefinder")
, new OptionData(OptionType.CHANNEL, "channel", "The Channel the feature will post in.")
)
).queue();
}
}