use of ws.nmathe.saber.core.schedule.ScheduleEntry in project Saber-Bot by notem.
the class ListCommand method verify.
@Override
public String verify(String prefix, String[] args, MessageReceivedEvent event) {
String head = prefix + this.name();
if (args.length == 0) {
return "That's not enough arguments! Use ``" + head + " <ID> [filters]``";
}
int index = 0;
ScheduleEntry entry;
if (VerifyUtilities.verifyEntryID(args[index])) {
Integer entryId = ParsingUtilities.encodeIDToInt(args[index]);
entry = Main.getEntryManager().getEntryFromGuild(entryId, event.getGuild().getId());
if (entry == null) {
return "The requested entry does not exist!";
}
if (!Main.getScheduleManager().isRSVPEnabled(entry.getChannelId())) {
return "The schedule that the entry is on is not rsvp enabled!";
}
} else {
return "Argument *" + args[index] + "* is not a valid entry ID!";
}
index++;
boolean mobileFlag = false;
boolean IdFlag = false;
for (; index < args.length; index++) {
if (args[index].equalsIgnoreCase("mobile") || args[index].equalsIgnoreCase("m")) {
mobileFlag = true;
if (IdFlag) {
return "Both 'mobile' and 'id' modes cannot be active at the same time!\n" + "Include either the 'id' or 'mobile' argument in the command, not both.";
}
continue;
}
if (args[index].equalsIgnoreCase("id") || args[index].equalsIgnoreCase("i")) {
IdFlag = true;
if (mobileFlag) {
return "Both 'mobile' and 'id' modes cannot be active at the same time!\n" + "Include either the 'id' or 'mobile' argument in the command, not both.";
}
continue;
}
String[] filter = args[index].split(":");
if (filter.length != 2) {
return "Invalid filter ``" + args[index] + "``!\nFilters must be of the form \"r: @role\" or \"u: @user\"";
}
String filterType = filter[0].toLowerCase().trim();
String filterValue = filter[1].trim();
switch(filterType.toLowerCase()) {
case "r":
case "role":
break;
case "u":
case "user":
break;
case "t":
case "type":
if (filterValue.equalsIgnoreCase("no-input"))
break;
Map<String, String> options = Main.getScheduleManager().getRSVPOptions(entry.getChannelId());
if (!options.values().contains(filterValue)) {
return "Invalid ``[type]`` for type filter!" + "``[type]`` must be either an rsvp group used on that event or \"no-input\".";
}
break;
default:
return "Invalid filter ``" + args[index] + "``!" + "\nFilters must be of the form \"r: @role\", \"u: @user\", or \"t: [type]\"";
}
}
return "";
}
use of ws.nmathe.saber.core.schedule.ScheduleEntry in project Saber-Bot by notem.
the class ListCommand method action.
@Override
public void action(String prefix, String[] args, MessageReceivedEvent event) {
int index = 0;
Integer entryId = ParsingUtilities.encodeIDToInt(args[index++]);
ScheduleEntry se = Main.getEntryManager().getEntryFromGuild(entryId, event.getGuild().getId());
String titleUrl = se.getTitleUrl() == null ? "https://nnmathe.ws/saber" : se.getTitleUrl();
String title = se.getTitle() + " [" + ParsingUtilities.intToEncodedID(entryId) + "]";
String content = "";
List<String> userFilters = new ArrayList<>();
List<String> roleFilters = new ArrayList<>();
boolean filterByType = false;
Set<String> typeFilters = new HashSet<>();
boolean mobileFlag = false;
boolean IdFlag = false;
for (; index < args.length; index++) {
if (args[index].equalsIgnoreCase("mobile") || args[index].equalsIgnoreCase("m")) {
mobileFlag = true;
continue;
}
if (args[index].equalsIgnoreCase("id") || args[index].equalsIgnoreCase("i")) {
IdFlag = true;
continue;
}
String filterType = args[index].split(":")[0].toLowerCase().trim();
String filterValue = args[index].split(":")[1].trim();
switch(filterType.toLowerCase()) {
case "r":
case "role":
roleFilters.add(filterValue.replace("<@&", "").replace(">", ""));
break;
case "u":
case "user":
userFilters.add(filterValue.replace("<@", "").replace(">", ""));
break;
case "t":
case "type":
filterByType = true;
typeFilters.add(filterValue);
break;
}
}
// maximum number of characters before creating a new message
int lengthCap = 1900;
// maximum number of lines until new message, in mobile mode
int mobileLineCap = 25;
Set<String> uniqueMembers = new HashSet<>();
Map<String, String> options = Main.getScheduleManager().getRSVPOptions(se.getChannelId());
for (String type : options.values()) {
if (!filterByType || typeFilters.contains(type)) {
content += "**\"" + type + "\"\n======================**\n";
List<String> members = se.getRsvpMembersOfType(type);
for (String id : members) {
// if the message is nearing maximum length, or if in mobile mode and the max lines have been reached
if (content.length() > lengthCap || (mobileFlag && StringUtils.countMatches(content, "\n") > mobileLineCap)) {
// build and send the embedded message object
Message message = (new MessageBuilder()).setEmbed((new EmbedBuilder()).setDescription(content).setTitle(title, titleUrl).build()).build();
MessageUtilities.sendMsg(message, event.getChannel(), null);
// clear the content sting
content = "*continued. . .* \n";
}
if (id.matches("\\d+")) {
// cases in which the id is most likely a valid discord user's ID
Member member = event.getGuild().getMemberById(id);
if (// if the user is still a member of the guild, add to the list
member != null) {
uniqueMembers.add(member.getUser().getId());
content += this.getNameDisplay(mobileFlag, IdFlag, member);
} else // otherwise, remove the member from the event and update
{
se.getRsvpMembersOfType(type).remove(id);
Main.getEntryManager().updateEntry(se, false);
}
} else {
// handles cases in which a non-discord user was added by an admin
uniqueMembers.add(id);
content += "*" + id + "*\n";
}
}
}
content += "\n";
}
if (!filterByType || typeFilters.contains("no-input")) {
// generate a list of all members of the guild who pass the filter and map to their ID
List<String> noInput = event.getGuild().getMembers().stream().filter(member -> checkMember(member, userFilters, roleFilters)).map(member -> member.getUser().getId()).collect(Collectors.toList());
for (String type : options.values()) {
noInput.removeAll(se.getRsvpMembersOfType(type));
}
content += "**No input\n======================\n**";
if (!filterByType & noInput.size() > 10) {
content += " Too many users to show: " + noInput.size() + " users with no rsvp\n";
} else
for (String id : noInput) {
if (content.length() > lengthCap || (mobileFlag && StringUtils.countMatches(content, "\n") > mobileLineCap)) {
// build and send the embedded message object
Message message = (new MessageBuilder()).setEmbed((new EmbedBuilder()).setDescription(content).setTitle(title, titleUrl).build()).build();
MessageUtilities.sendMsg(message, event.getChannel(), null);
// clear the content sting
content = "*continued. . .* \n";
}
Member member = event.getGuild().getMemberById(id);
content += this.getNameDisplay(mobileFlag, IdFlag, member);
}
}
String footer = uniqueMembers.size() + " unique member(s) appear in this search";
// build and send the embedded message object
Message message = (new MessageBuilder()).setEmbed((new EmbedBuilder()).setDescription(content).setTitle(title, titleUrl).setFooter(footer, null).build()).build();
MessageUtilities.sendMsg(message, event.getChannel(), null);
}
use of ws.nmathe.saber.core.schedule.ScheduleEntry in project Saber-Bot by notem.
the class EventListener method onMessageReactionAdd.
@Override
@SuppressWarnings("unchecked")
public void onMessageReactionAdd(MessageReactionAddEvent event) {
// stop processing if the event is not from a guild text channel
if (!event.isFromType(ChannelType.TEXT))
return;
// don't process reactions added on non RSVP channels
if (!Main.getScheduleManager().isRSVPEnabled(event.getChannel().getId()))
return;
// don't process reactions added by the bot
if (event.getUser().getId().equals(event.getJDA().getSelfUser().getId()))
return;
if (reactionLimiter.isOnCooldown(event.getUser().getId()))
return;
// add the user to the appropriate rsvp list and remove the emoji
try {
Document doc = Main.getDBDriver().getEventCollection().find(eq("messageId", event.getMessageId())).first();
if (doc != null) {
ScheduleEntry se = new ScheduleEntry(doc);
boolean removeReaction = se.handleRSVPReaction(event);
if (removeReaction) {
// attempt to remove the reaction
Consumer<Throwable> errorProcessor = e -> {
if (!(e instanceof PermissionException)) {
Logging.exception(this.getClass(), e);
}
};
event.getReaction().removeReaction(event.getUser()).queue(null, errorProcessor);
}
}
} catch (PermissionException ignored) {
} catch (Exception e) {
Logging.exception(this.getClass(), e);
}
}
use of ws.nmathe.saber.core.schedule.ScheduleEntry in project Saber-Bot by notem.
the class TestCommand 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++;
String format = Main.getScheduleManager().getStartAnnounceFormat(entry.getChannelId());
if (args.length == 2) {
switch(args[index].toLowerCase()) {
case "e":
case "end":
format = Main.getScheduleManager().getEndAnnounceFormat(entry.getChannelId());
break;
case "r":
case "remind":
format = Main.getScheduleManager().getReminderFormat(entry.getChannelId());
break;
}
}
String remindMsg = ParsingUtilities.parseMessageFormat(format, entry, true);
MessageUtilities.sendMsg(remindMsg, event.getChannel(), null);
}
use of ws.nmathe.saber.core.schedule.ScheduleEntry in project Saber-Bot by notem.
the class TestCommand method verify.
@Override
public String verify(String prefix, String[] args, MessageReceivedEvent event) {
String head = prefix + this.name();
int index = 0;
// length checks
if (args.length < 1) {
return "That's not enough arguments! Use ``" + head + " <ID>``";
}
if (args.length > 2) {
return "That's too many arguments! Use ``" + head + " <ID> [<type>]``";
}
// check for a valid entry ID
if (!VerifyUtilities.verifyEntryID(args[index])) {
return "``" + args[index] + "`` is not a valid entry ID!";
}
// check to see if event with the provided ID exists for the guild
Integer Id = ParsingUtilities.encodeIDToInt(args[index]);
ScheduleEntry entry = Main.getEntryManager().getEntryFromGuild(Id, event.getGuild().getId());
if (entry == null) {
return "I could not find an entry with that ID!";
}
// next argument is optional
index++;
if (args.length == 2) {
switch(args[index].toLowerCase()) {
case "start":
case "end":
case "remind":
case "reminder":
case "e":
case "r":
case "s":
break;
default:
return "*" + args[index] + "* is not an announcement type!\n" + "Please use either ``start``, ``end``, or ``remind`` for this argument!";
}
}
// return valid
return "";
}
Aggregations