use of net.dv8tion.jda.api.JDA in project Saber-Bot by notem.
the class VerifyUtilities method verifyEmoji.
/**
* verify that a supplied emoji string is a valid discord emoji
* @param emoji emoji string (either raw emoji char or discord emoji ID)
* @return true if valid
*/
public static boolean verifyEmoji(String emoji) {
if (!EmojiManager.isEmoji(emoji)) {
// split on colons to isolate the reaction name from it's ID
String[] split = emoji.split(":");
// trim to include only the ID
String emoteId = split[split.length - 1].replaceAll("[^\\d]", "");
Emote emote = null;
try {
for (JDA jda : Main.getShardManager().getShards()) {
emote = jda.getEmoteById(emoteId);
if (emote != null)
break;
}
} catch (Exception e) {
return false;
}
if (emote == null) {
return false;
}
}
return true;
}
use of net.dv8tion.jda.api.JDA in project Saber-Bot by notem.
the class VerifyUtilities method verifyAnnouncementAdd.
/**
* Returns error message (or empty string) for announcement add keyword verification
*/
public static String verifyAnnouncementAdd(String[] args, int index, String head, MessageReceivedEvent event) {
if (args.length - index < 3) {
return "That's not the right number of arguments for **" + args[index - 2] + " " + args[index - 1] + "**!\n" + "Use ``" + head + " " + args[0] + " " + args[index - 2] + " " + args[index - 1] + " [#target] [time] [message]``";
}
JDA jda = Main.getShardManager().getJDA(event.getGuild().getId());
String channelId = args[index].replaceAll("[^\\d]", "");
if (!channelId.matches("\\d+") || jda.getTextChannelById(channelId) == null) {
return "**" + args[index] + "** is not a channel on your server!";
}
if (!VerifyUtilities.verifyTimeString(args[index + 1])) {
return "**" + args[index + 1] + "** is not a properly formed announcement time!\n" + "Times use the format \"TYPE+/-OFFSET\". Ex: ``START+10m``, ``END-1h``";
}
return "";
}
use of net.dv8tion.jda.api.JDA in project Saber-Bot by notem.
the class AnnouncementsCommand method action.
@Override
public void action(String prefix, String[] args, MessageReceivedEvent event) {
int index = 0;
// get entry object
Integer entryId = ParsingUtilities.encodeIDToInt(args[index]);
ScheduleEntry entry = Main.getEntryManager().getEntry(entryId);
// verify the entry's message exists
Message msg = entry.getMessageObject();
if (msg == null)
return;
index++;
if (args.length > 2) {
// if additional args have been provided. . .
switch(args[index++].toLowerCase()) {
case "a":
case "add":
String target = args[index].replaceAll("[^\\d]", "");
String time = args[index + 1];
String message = args[index + 2];
entry.addAnnouncementOverride(target, time, message);
break;
case "r":
case "remove":
Integer id = Integer.parseInt(args[index].replaceAll("[^\\d]", "")) - 1;
entry.removeAnnouncementOverride(id);
break;
case "qs":
case "quiet-start":
entry.setQuietStart(!entry.isQuietStart());
break;
case "qe":
case "quiet-end":
entry.setQuietEnd(!entry.isQuietEnd());
break;
case "qr":
case "quiet-remind":
entry.setQuietRemind(!entry.isQuietRemind());
break;
case "qa":
case "quiet-all":
if (entry.isQuietRemind() && entry.isQuietEnd() && entry.isQuietStart()) {
entry.setQuietRemind(false).setQuietEnd(false).setQuietStart(false);
} else {
entry.setQuietRemind(true).setQuietEnd(true).setQuietStart(true);
}
break;
}
Main.getEntryManager().updateEntry(entry, false);
}
/*
* generate output message
*/
String content = "```js\n// Schedule Announcements\n";
if (!entry.isQuietStart()) {
String format = Main.getScheduleManager().getStartAnnounceFormat(entry.getChannelId());
if (!format.isEmpty()) {
String target = Main.getScheduleManager().getStartAnnounceChan(entry.getChannelId());
if (target.matches("\\d+")) {
JDA jda = Main.getShardManager().getJDA(entry.getGuildId());
try {
target = jda.getTextChannelById(target).getName();
} catch (Exception ignored) {
}
}
content += "[s] \"" + format + "\" at \"START\" on \"#" + target + "\"\n";
}
}
if (!entry.isQuietEnd()) {
String format = Main.getScheduleManager().getEndAnnounceFormat(entry.getChannelId());
if (!format.isEmpty()) {
String target = Main.getScheduleManager().getEndAnnounceChan(entry.getChannelId());
if (target.matches("\\d+")) {
JDA jda = Main.getShardManager().getJDA(entry.getGuildId());
try {
target = jda.getTextChannelById(target).getName();
} catch (Exception ignored) {
}
}
content += "[e] \"" + format + "\" at \"END\" on \"#" + target + "\"\n";
}
}
if (!entry.isQuietRemind()) {
String format = Main.getScheduleManager().getReminderFormat(entry.getChannelId());
String target = Main.getScheduleManager().getReminderChan(entry.getChannelId());
if (target.matches("\\d+")) {
JDA jda = Main.getShardManager().getJDA(entry.getGuildId());
try {
target = jda.getTextChannelById(target).getName();
} catch (Exception ignored) {
}
}
for (Integer reminder : Main.getScheduleManager().getReminders(entry.getChannelId())) {
content += "[r] \"" + format + "\" at \"START" + (reminder > 0 ? "-" + reminder : "+" + Math.abs(reminder)) + "m\" on \"#" + target + "\"\n";
}
format = Main.getScheduleManager().getReminderFormat(entry.getChannelId());
for (Integer reminder : Main.getScheduleManager().getEndReminders(entry.getChannelId())) {
content += "[r] \"" + format + "\" at \"END" + (reminder > 0 ? "-" + reminder : "+" + Math.abs(reminder)) + "m\" on \"#" + target + "\"\n";
}
}
if (!entry.getAnnouncementTimes().values().isEmpty()) {
content += entry.announcementsToString();
}
content += "```";
MessageUtilities.sendMsg(content, event.getChannel(), null);
}
use of net.dv8tion.jda.api.JDA in project Saber-Bot by notem.
the class ConfigCommand method genMsgStr.
/**
* Generates the schedule config message to display to the user
* type codes: 0 - full message
* 1 - announcement settings
* 2 - reminder settings
* 3 - miscellaneous settings
* 4 - sync settings
* 5 - rsvp settings
* @param cId (String) the ID of the schedule/channel
* @return (String) the message to display
*/
private String genMsgStr(String cId, Mode mode, JDA jda) {
// message body contents
String content = "**Configuration for** <#" + cId + ">\n";
switch(mode) {
default:
case ANN:
String form1 = Main.getScheduleManager().getStartAnnounceFormat(cId);
String form2 = Main.getScheduleManager().getEndAnnounceFormat(cId);
String chanIdentifier = Main.getScheduleManager().getStartAnnounceChan(cId);
String endChanIdentifier = Main.getScheduleManager().getEndAnnounceChan(cId);
content += "```js\n" + "// Event Announcement Settings" + "\n[message] " + (form1.isEmpty() ? "(off)" : "\"" + form1.replace("```", "`\uFEFF`\uFEFF`") + "\"") + "\n[channel] " + "\"" + this.channelIdentifierToString(chanIdentifier, jda) + "\"" + "\n[end-msg] " + (Main.getScheduleManager().isEndFormatOverridden(cId) ? (form2.isEmpty() ? "(off)" : "\"" + form2.replace("```", "`\uFEFF`\uFEFF`") + "\"") : "(using [message])") + "\n[end-chan] " + (Main.getScheduleManager().isEndChannelOverridden(cId) ? "\"" + this.channelIdentifierToString(endChanIdentifier, jda) + "\"" : "(using [channel])") + "```";
if (mode == Mode.ANN)
break;
case REM:
if (content.length() > 1900) {
return content;
}
String form3 = Main.getScheduleManager().getReminderFormat(cId);
List<Integer> reminders = Main.getScheduleManager().getReminders(cId);
List<Integer> endReminders = Main.getScheduleManager().getEndReminders(cId);
String remindChanIdentifier = Main.getScheduleManager().getReminderChan(cId);
content += "```js\n" + "// Event Reminder Settings" + "\n[reminders] " + "\"" + makeReminderString(reminders) + "\"" + "\n[end-remind] " + "\"" + makeReminderString(endReminders) + "\"" + "\n[remind-msg] " + (Main.getScheduleManager().isRemindFormatOverridden(cId) ? (form3.isEmpty() ? "(off)" : "\"" + form3.replace("```", "`\uFEFF`\uFEFF`") + "\"") : "(using [msg])") + "\n[remind-chan] " + (Main.getScheduleManager().isRemindChanOverridden(cId) ? "\"" + this.channelIdentifierToString(remindChanIdentifier, jda) + "\"" : "(using [chan])") + "```";
if (mode == Mode.REM)
break;
case MISC:
if (content.length() > 1900) {
return content;
}
int sortType = Main.getScheduleManager().getAutoSort(cId);
String sort = "";
switch(sortType) {
case 0:
sort = "disabled";
break;
case 1:
sort = "ascending";
break;
case 2:
sort = "descending";
break;
}
// create list of zones & convert to string for printout
List<ZoneId> zones = new ArrayList<>();
zones.add(Main.getScheduleManager().getTimeZone(cId));
zones.addAll(Main.getScheduleManager().getAltZones(cId));
String zoneNames = String.join(", ", zones.stream().map(ZoneId::toString).collect(Collectors.toList()));
content += "```js\n" + "// Misc. Settings" + "\n[zone] " + "\"" + zoneNames + "\"" + "\n[clock] " + "\"" + Main.getScheduleManager().getClockFormat(cId) + "\"" + "\n[style] " + "\"" + Main.getScheduleManager().getStyle(cId).toLowerCase() + "\"" + "\n[sort] " + "\"" + sort + "\"" + "```";
if (mode == Mode.MISC)
break;
case SYNC:
if (content.length() > 1900) {
return content;
}
// get sync user object (if exists)
User user = null;
if (Main.getScheduleManager().getSyncUser(cId) != null)
user = jda.getUserById(Main.getScheduleManager().getSyncUser(cId));
content += "```js\n" + "// Schedule Sync Settings" + "\n[sync] " + "\"" + Main.getScheduleManager().getAddress(cId) + "\"" + (user != null ? " (authorized by " + user.getName() + ")" : "");
// display full body only if sync is on
ZoneId mainZone = Main.getScheduleManager().getTimeZone(cId);
if (!Main.getScheduleManager().getAddress(cId).equalsIgnoreCase("off")) {
Date syncTime = Main.getScheduleManager().getSyncTime(cId);
OffsetTime sync_time_display = ZonedDateTime.ofInstant(syncTime.toInstant(), mainZone).toOffsetDateTime().toOffsetTime().truncatedTo(ChronoUnit.MINUTES);
content += "\n[time] " + "\"" + sync_time_display + "\"" + "\n[length] " + "\"" + Main.getScheduleManager().getSyncLength(cId) + "\"";
}
content += "```";
if (mode == Mode.SYNC)
break;
case RSVP:
if (content.length() > 1900) {
return content;
}
String clear = Main.getScheduleManager().getRSVPClear(cId);
content += "```js\n" + "// RSVP Settings" + "\n[rsvp] " + "\"" + (Main.getScheduleManager().isRSVPEnabled(cId) ? "on" : "off") + "\"";
// only display full settings message when rsvp is enabled
if (Main.getScheduleManager().isRSVPEnabled(cId)) {
// rsvp logging channel
String logging = Main.getScheduleManager().getRSVPLogging(cId);
content += "\n[clear] " + "\"" + (clear.isEmpty() ? "off" : clear) + "\"" + "\n[confirm] " + "\"" + (Main.getScheduleManager().isRSVPConfirmationsEnabled(cId) ? "on" : "off") + "\"" + "\n[exclude] " + "\"" + (Main.getScheduleManager().isRSVPExclusive(cId) ? "on" : "off") + "\"" + "\n[logging] " + "\"" + (logging.isEmpty() ? "off" : channelIdentifierToString(logging, jda)) + "\"" + "\n<Groups> ";
// generate the list of rsvp groups
Map<String, String> options = Main.getScheduleManager().getRSVPOptions(cId);
for (String key : options.keySet()) {
if (EmojiManager.isEmoji(key)) {
content += " (" + options.get(key) + " - " + key + ")";
} else {
Emote emote = null;
for (JDA shard : Main.getShardManager().getShards()) {
emote = shard.getEmoteById(key);
if (emote != null)
break;
}
if (emote != null) {
String displayName = emote.getName();
content += " (" + options.get(key) + " - :" + displayName + ":)";
}
}
}
}
content += "```";
if (mode == Mode.RSVP)
break;
}
return content;
}
Aggregations