@Override public boolean canProcess(IncomeMessage message) { return !message.hasCommand() && patterns.stream() .anyMatch(pattern -> pattern.matcher(message.content()).find()); }
@Override public void process(IncomeMessage message) { message.command() .on("new", userId -> { userManager.create(userId, message.location()); message.reply("User created"); }) .on("link", LinkRequest.class, request -> { userManager.link(message, request.userId(), request.channel(), request.target()); message.reply("Link requested and will be effective after confirmation."); }) .on("unlink", UnlinkRequest.class, request -> { user.removeDestination(request.channel()); this.repository.update(user); message.reply("The destination was removed from your user."); }) .on("default", channel -> { User user = message.user(); if (user == null) { throw new MessageProcessingException("You're not registered."); channel = message.channel().id(); message.reply("Your new primary destination was saved!"); }) .execute();
@Override public void process(IncomeMessage message) { message.command() .on("enabled", String.class, (name) -> { boolean active = activator.isEnabled(name, message.location()); if (active) { message.reply("[p]Yes, the plugin is enabled for this channel[/p]"); } else { message.reply("[n]No, the plugin is disabled for this channel[/n]"); } }) .on("disable", List.class, (plugins) -> { plugins.forEach(name -> activator.disable(name.toString(), message.location())); message.reply("Plugin(s) disabled successfully!"); }) .on("enable", List.class, (plugins) -> { plugins.forEach(name -> activator.enable(name.toString(), message.location())); message.reply("Plugin(s) enabled successfully!"); }).execute(); }
/** * Formats the message using the given format and arguments and then * uses the produced content as the {@link #reply(String) reply}. * * @param format the format of the message * @param args the arguments to format the message */ default void reply(String format, Object... args) { reply(String.format(format, args)); }
/** * Returns a predicate that allows commands that have the given name. * * @param commandName the allowed name * @return a predicate that allows commands that have the given name. */ public static Predicate<IncomeMessage> command(String commandName) { return message -> hasCommand() .and(new AcceptedValuePredicate<>(commandName.toLowerCase(), m -> m.command().name().toLowerCase())) .test(message); }
private <T> T convert(String string, Class<T> type) { try { return new ParameterBinder<>(type) .context(context -> { OsgiServiceRegistry registry = new OsgiServiceRegistry(); OsgiParameterResolver osgiResolver = new OsgiParameterResolver(registry); context.use(this.incomeMessage) .when(type(IncomeMessage.class)) .use(this.incomeMessage.channel()) .when(type(Channel.class)) .use(this.incomeMessage.sender()) .when(type(Sender.class)) .use(this.incomeMessage.location()) .when(type(MessageLocation.class)) .use(registry) .when(type(ServiceRegistry.class)) // try to lookup implementations by default using the osgi registry .use(osgiResolver) .byDefault(); if (this.incomeMessage.user() != null) { context.use(parameter -> this.incomeMessage.user()) .when(type(User.class)); } }).apply(string); } catch (Exception e) { throw new MessageProcessingException("Invalid command parameters."); } }
@Override public void process(IncomeMessage message) { Matcher matcher = pattern.matcher(message.content()); StringBuilder replyMessage = new StringBuilder(); Set<String> evaluated = new HashSet<>(); while (matcher.find()) { String term = matcher.group("term"); String operation = matcher.group("operation"); if (!evaluated.contains(term.toLowerCase())) { int value = update(term, operation); replyMessage.append(buildReplyMessage(term, value)).append("\n"); evaluated.add(term.toLowerCase()); } } if (replyMessage.length() > 0) { // remove the last \n replyMessage.deleteCharAt(replyMessage.length() - 1); message.sendBack(replyMessage.toString()); } }
@Override public boolean isCommand(IncomeMessage message) { return pattern.matcher(message.content()).find(); }
@Override public boolean accept(MessageProcessor messageProcessor, IncomeMessage message) { return messageProcessor.getClass().isAnnotationPresent(AlwaysActive.class) || activator.isEnabled(messageProcessor.name(), message.location()); }
@Override public boolean accept(IncomeMessage message) { return !ignored.contains(message.sender().id()); }
@Override public boolean canProcess(IncomeMessage message) { return acceptedChannels.contains(message.channel().id()); }
/** * Formats the message using the given format and arguments and then * uses the produced content as the {@link #sendBack(String) reply to sender}. * * @param format the format of the message * @param args the arguments to format the message */ default void sendBack(String format, Object... args) { sendBack(String.format(format, args)); }
public ExtractedCommand(IncomeMessage incomeMessage, String name, String rawArguments) { this.incomeMessage = incomeMessage; this.name = name; this.rawArguments = rawArguments; this.actions = new HashMap<>(); this.defaultAction = string -> this.incomeMessage.reply("Invalid action, possible actions: \n" + actions.keySet().stream().collect(Collectors.joining("\n"))); }
@Override public void process(IncomeMessage message) { message.command().as(LookupCommand.class) .lookup() .and(message::reply) .orElseThrow(() -> new MessageProcessingException("Definition not found")); }
public MessageCommand extract(IncomeMessage message) { Matcher matcher = pattern.matcher(message.content()); if (matcher.find()) { String command = matcher.group("command"); String args = matcher.group("arguments"); return new ExtractedCommand(message, command, args != null ? args.trim() : ""); } else { throw new BotException(message.content() + " is not a command"); } }
/** * Returns a predicate that allows channels that have the given id. * * @param channelId the channel id * @return a predicate that allows channels that have the given id. */ public static Predicate<IncomeMessage> channel(String channelId) { return new AcceptedValuePredicate<>(channelId, m -> m.channel().id()); }