Adding manual trigger for dayListener

This commit is contained in:
BrokenFire 2018-02-16 16:47:49 +01:00
parent 70bc064cf7
commit 0e08522eaa
2 changed files with 76 additions and 5 deletions

View File

@ -0,0 +1,67 @@
package net.Broken.Commands;
import net.Broken.Commande;
import net.Broken.MainBot;
import net.Broken.Tools.DayListener.DayListener;
import net.Broken.Tools.EmbedMessageUtils;
import net.Broken.Tools.MessageTimeOut;
import net.Broken.Tools.PrivateMessage;
import net.dv8tion.jda.core.Permission;
import net.dv8tion.jda.core.entities.ChannelType;
import net.dv8tion.jda.core.entities.Message;
import net.dv8tion.jda.core.events.message.MessageReceivedEvent;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.util.ArrayList;
import java.util.List;
public class DayTrigger implements Commande{
Logger logger = LogManager.getLogger();
@Override
public boolean called(String[] args, MessageReceivedEvent event) {
return false;
}
@Override
public void action(String[] args, MessageReceivedEvent event) {
if(event.getMember().hasPermission(Permission.ADMINISTRATOR)){
DayListener.getInstance().trigger();
event.getMessage().delete().queue();
}
else{
logger.warn("Not admin! DENY!");
if(!event.isFromType(ChannelType.PRIVATE)){
Message message = event.getTextChannel().sendMessage(EmbedMessageUtils.getError("Vous n'avez pas l'autorisation de faire ça!")).complete();
List<Message> messages = new ArrayList<Message>(){{
add(message);
add(event.getMessage());
}};
new MessageTimeOut(messages, MainBot.messageTimeOut).start();
}
else{
PrivateMessage.send(event.getAuthor(),EmbedMessageUtils.getError("Vous n'avez pas l'autorisation de faire ça!"),logger);
}
}
}
@Override
public String help(String[] args) {
return null;
}
@Override
public void executed(boolean success, MessageReceivedEvent event) {
}
@Override
public boolean isPrivateUsable() {
return true;
}
}

View File

@ -16,7 +16,7 @@ public class DayListener extends Thread {
private DayListener() { private DayListener() {
calendar = new GregorianCalendar(); calendar = new GregorianCalendar();
previousDay = 0; previousDay = calendar.get(GregorianCalendar.DAY_OF_MONTH);
} }
private static DayListener INSTANCE = new DayListener(); private static DayListener INSTANCE = new DayListener();
@ -30,6 +30,12 @@ public class DayListener extends Thread {
listeners.add(listener); listeners.add(listener);
} }
public void trigger(){
for(NewDayListener listener : listeners){
listener.onNewDay();
}
}
@Override @Override
public void run() { public void run() {
@ -38,13 +44,11 @@ public class DayListener extends Thread {
if(calendar.get(GregorianCalendar.DAY_OF_MONTH) != previousDay) if(calendar.get(GregorianCalendar.DAY_OF_MONTH) != previousDay)
{ {
LogManager.getLogger().info("New day triggered!"); LogManager.getLogger().info("New day triggered!");
for(NewDayListener listener : listeners){ trigger();
listener.onNewDay();
}
previousDay = calendar.get(GregorianCalendar.DAY_OF_MONTH); previousDay = calendar.get(GregorianCalendar.DAY_OF_MONTH);
} }
try { try {
sleep(7200000); sleep(1800000);
} catch (InterruptedException e) { } catch (InterruptedException e) {
LogManager.getLogger().catching(e); LogManager.getLogger().catching(e);
} }