Skip to content

Commit

Permalink
Merge pull request #15 from ice-games/feature/timestamps
Browse files Browse the repository at this point in the history
Timestamp Util & Fix concurrentmodifiedexception
  • Loading branch information
seailz authored Jul 27, 2022
2 parents 42c2933 + 6463687 commit 1c3905f
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,18 @@
public class ModalListener extends ListenerAdapter {

@Override
public void onModalInteraction(@NotNull ModalInteractionEvent e){
public void onModalInteraction(@NotNull ModalInteractionEvent e) {
Map.Entry<Member, Modal> culprit = null;
for (Map.Entry<Member, Modal> modalEntry : ModalManager.getModals()) {
if (modalEntry.getValue().getId().equals(e.getModalId()) && modalEntry.getKey().getId().equals(e.getMember().getId())){
if (modalEntry.getValue().getId().equals(e.getModalId()) && modalEntry.getKey().getId().equals(e.getMember().getId())) {
ModalMapping[] mappings = e.getValues().toArray(new ModalMapping[0]);
modalEntry.getValue().getOnSubmit().accept(e.getMember(), mappings, e);
try {
ModalManager.getModals().remove(modalEntry);
} catch (ConcurrentModificationException ignored) {}
culprit = modalEntry;
break;
}
}
}

if (culprit != null)
ModalManager.getModals().remove(culprit);
}
}
65 changes: 65 additions & 0 deletions src/main/java/com/seailz/jdaframework/utils/TimestampUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.seailz.jdaframework.utils;

import java.util.Date;

/**
* A util for creating timestamps, with ease.
* @author Seailz
*/
public class TimestampUtil {

/**
* Generates a Discord timestamp
* @param type The type of timestamp you want to create
* @param time The time it will represent
* @return The generated timestamp
*/
public static String createTimestamp(Type type, Date time) {
if (type == Type.BASE)
return String.valueOf(time.getTime());
String base = "<t:" + time.getTime() + ":";

switch (type) {
case SHORT_DATE:
base += "d";
case LONG_DATE:
base += "D";
case SHORT_TIME:
base += "t";
case LONG_TIME:
base += "T";
case DATE_AND_TIME:
base += "f";
case LONG_DATE_AND_TIME:
base += "F";
case AGO:
base += "R";
}

base += ">";
return base;
}

/**
* Timestamp types
*/
public enum Type {
SHORT_DATE,
// For example: 26/07/2022
LONG_DATE,
// For example: 26 July 2022
SHORT_TIME,
// For example 19:50
LONG_TIME,
// For example: 19:50:43
DATE_AND_TIME,
// For example: 26 July 2022 19:50
LONG_DATE_AND_TIME,
// For example: Tuesday, 26 July 2022 19:50
AGO,
// For example: 10 minutes ago
BASE
// For example: 1658861400
}

}

0 comments on commit 1c3905f

Please sign in to comment.