feat: finished implementing TimePlanner Feature

This commit is contained in:
limited_dev 2022-09-30 23:26:16 +02:00
parent 35097041c0
commit 455ba04935
7 changed files with 123 additions and 23 deletions

2
.gitignore vendored
View file

@ -1,4 +1,4 @@
/.idea/ /.idea/
/.gradle/ /.gradle/
/data/token.judd /data/
/build/ /build/

View file

@ -50,8 +50,6 @@ public class Main {
} catch (InterruptedException e) { } catch (InterruptedException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
lgr.info("Ready to take over the world");
} }
public static JDA getJda() { public static JDA getJda() {

View file

@ -0,0 +1,28 @@
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;
public class RemoveFeatureCommand extends Command {
public RemoveFeatureCommand() {
super("remove", "Remove a feature", new Option[]{ new Option(null, "Feature name", "Des", false)});
}
@Override
public void onSlashCommandInteraction(SlashCommandInteractionEvent event) {
if(!event.getGuild().getMemberById(event.getUser().getId()).hasPermission(Permission.ADMINISTRATOR)){
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.remove(event.getGuild().getIdLong());
TimePlanner.getGtps().save();
EmbeddedMessageHelper.sendSimpleOneLiner(event, "The Feature has been disabled",
"The Feature has been removed from this Server", null);
}
}
}

View file

@ -2,6 +2,7 @@ package de.limited_dev.lil_judd.commands.components;
import de.limited_dev.lil_judd.commands.InfoCommand; import de.limited_dev.lil_judd.commands.InfoCommand;
import de.limited_dev.lil_judd.commands.PingCommand; import de.limited_dev.lil_judd.commands.PingCommand;
import de.limited_dev.lil_judd.commands.RemoveFeatureCommand;
import de.limited_dev.lil_judd.commands.SetupFeatureCommand; import de.limited_dev.lil_judd.commands.SetupFeatureCommand;
import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.CopyOnWriteArrayList;
@ -14,6 +15,7 @@ public class CommandManager {
commands.add(new PingCommand()); commands.add(new PingCommand());
commands.add(new InfoCommand()); commands.add(new InfoCommand());
commands.add(new SetupFeatureCommand()); commands.add(new SetupFeatureCommand());
commands.add(new RemoveFeatureCommand());
} }
public static CopyOnWriteArrayList<Command> getCommands() { public static CopyOnWriteArrayList<Command> getCommands() {

View file

@ -2,10 +2,16 @@ package de.limited_dev.lil_judd.features;
import de.limited_dev.lil_judd.Main; import de.limited_dev.lil_judd.Main;
import de.limited_dev.lil_judd.features.storage.GuildTimePlannerStorage; 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.EmbeddedMessageHelper;
import de.limited_dev.lil_judd.util.Logger; import de.limited_dev.lil_judd.util.Logger;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.Guild; import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.MessageEmbed;
import net.dv8tion.jda.api.entities.emoji.CustomEmoji;
import net.dv8tion.jda.api.entities.emoji.Emoji;
import java.time.Duration; import java.time.Duration;
import java.time.ZoneId; import java.time.ZoneId;
@ -27,12 +33,12 @@ public class TimePlanner {
public static void registerMessageThread(){ public static void registerMessageThread(){
// get the current ZonedDateTime of your TimeZone // get the current ZonedDateTime of your TimeZone
ZonedDateTime now = ZonedDateTime.now(ZoneId.of("Europe/Berlin"));//.withHour(8).withMinute(0); 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.plusDays(DayUtil.getDelay(now.getDayOfWeek().name()));
ZonedDateTime nextNotifyDay = now.plusSeconds(10); //ZonedDateTime nextNotifyDay = now.plusSeconds(10);
lgr.info(now.getDayOfMonth() + ""); lgr.info(nextNotifyDay.getDayOfWeek() + ", " + nextNotifyDay.getDayOfMonth() + "." + nextNotifyDay.getMonth() + "." + nextNotifyDay.getYear() + " / " + nextNotifyDay.getHour() + ":" + nextNotifyDay.getMinute());
//lgr.info(now.getDayOfWeek().name() + " " + nextNotifyDay.getDayOfWeek().name()); //lgr.info(now.getDayOfWeek().name() + " " + nextNotifyDay.getDayOfWeek().name());
@ -53,22 +59,64 @@ public class TimePlanner {
ScheduledExecutorService schedulerFirstLesson = Executors.newScheduledThreadPool(1); ScheduledExecutorService schedulerFirstLesson = Executors.newScheduledThreadPool(1);
schedulerFirstLesson.scheduleAtFixedRate(() -> { schedulerFirstLesson.scheduleAtFixedRate(() -> {
// send a message // send a message
String msg = "<Msg Content>";
JDA jda = Main.getJda(); JDA jda = Main.getJda();
Set<Long> gs = gtps.guildsWithPlanner.keySet(); Set<Long> gs = gtps.guildsWithPlanner.keySet();
lgr.info(gs.toString()); lgr.info(gs.toString());
for (Long l : gs) { Thread thr = new Thread(){
//guild.getDefaultChannel().sendMessage(message).queue(); @Override
//jda.getGuildById(l).getTextChannelById(gtps.getChannelID(l)).sendMessage("").queue(); public void run() {
EmbeddedMessageHelper.sendSimpleOneLiner(jda.getGuildById(l), gtps.guildsWithPlanner.get(l), "You got time?", "Ain't nobody got time for that?", null); for(Long l : gs) {
lgr.info("Send Code to Server " + jda.getGuildById(l).getName()); ZonedDateTime then;
EmbeddedMessageHelper.sendSimpleOneLiner(jda.getGuildById(l), gtps.guildsWithPlanner.get(l), "Timeplanning System", "Do you have time on the following Days?", null);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
} }
lgr.info("Send Push Notification for time Management"); for(int i = 0; i < 7; ++i){
then = now.plusDays(i);
MessageEmbed eb = EmbeddedMessageHelper.getEmbeddedMessageAutomated(false, null, then.getDayOfWeek() + ", " + then.getDayOfMonth() + "." + then.getMonthValue() + "." + then.getYear(), null, false).build();
jda.getGuildById(l).getTextChannelById(gtps.guildsWithPlanner.get(l)).sendMessageEmbeds(eb).queue(message -> addReactions(message));
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
lgr.info("Send to Server " + jda.getGuildById(l).getName());
}
lgr.info("Terminating Thread...");
Thread.currentThread().interrupt();
return;
}
private void addReactions(Message msg){
msg.addReaction(Emoji.fromUnicode("")).queue();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
msg.addReaction(Emoji.fromFormatted("")).queue();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
msg.addReaction(Emoji.fromFormatted("")).queue();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
};
thr.start();
lgr.info("Started sending Push Notifications for time Management");
}, },
initialDelayUntilNextNotification, initialDelayUntilNextNotification,
TimeUnit.SECONDS.toSeconds(10),//TimeUnit.DAYS.toSeconds(7), TimeUnit.DAYS.toSeconds(7),
TimeUnit.SECONDS); TimeUnit.SECONDS);
} }
} }

View file

@ -41,4 +41,22 @@ public class EmbeddedMessageHelper {
eb.setFooter(">" + dtf.format(now) + " - Automated Message"); eb.setFooter(">" + dtf.format(now) + " - Automated Message");
g.getTextChannelById(channelID).sendMessageEmbeds(eb.build()).queue(); g.getTextChannelById(channelID).sendMessageEmbeds(eb.build()).queue();
} }
public static EmbedBuilder getEmbeddedMessageAutomated(boolean doAuthor, String title, String description, String thumbnailURL, boolean shouldAddFooter){
LocalDateTime now = LocalDateTime.now();
EmbedBuilder eb = new EmbedBuilder();
if(doAuthor)
eb.setAuthor(Main.getJda().getSelfUser().getName());
if(title != null){
eb.setTitle(title);
}
eb.setColor(Color.ORANGE);
if(description != null)
eb.setDescription(description);
if(thumbnailURL != null)
eb.setThumbnail(thumbnailURL);
if(shouldAddFooter)
eb.setFooter(">" + dtf.format(now) + " - Automated Message");
return eb;
}
} }

View file

@ -21,6 +21,12 @@ public class SlashCommandHelper {
new OptionData(OptionType.STRING, "feature", "The Feature you want to setup") new OptionData(OptionType.STRING, "feature", "The Feature you want to setup")
.addChoice("Send Time finder", "timefinder") .addChoice("Send Time finder", "timefinder")
, new OptionData(OptionType.CHANNEL, "channel", "The Channel the feature will post in.") , new OptionData(OptionType.CHANNEL, "channel", "The Channel the feature will post in.")
),
Commands.slash("remove", "Remove a feature")
.addOptions(
new OptionData(OptionType.STRING, "feature", "The Feature you want to remove")
.addChoice("Send Time finder", "timefinder")
, new OptionData(OptionType.CHANNEL, "channel", "The Channel with the feature")
) )
).queue(); ).queue();