private Optional<Message> sendMd(String message, MessageContext ctx, String... args) { return silent.sendMd(getLocalizedMessage(message, ctx.user().getLanguageCode(), args), ctx.chatId()); }
private Optional<Message> send(String message, MessageContext ctx, String... args) { return silent.send(getLocalizedMessage(message, ctx.user().getLanguageCode(), args), ctx.chatId()); }
.sorted() .reduce((a, b) -> format("%s%n%s", a, b)) .orElse(getLocalizedMessage(ABILITY_COMMANDS_NOT_FOUND, ctx.user().getLanguageCode()));
private Optional<Message> send(String message, Update upd) { Long chatId = upd.getMessage().getChatId(); return silent.send(getLocalizedMessage(message, AbilityUtils.getUser(upd).getLanguageCode()), chatId); }
commands = getLocalizedMessage(ABILITY_COMMANDS_NOT_FOUND, ctx.user().getLanguageCode());
/** * Gets the user with the specified username. If user was not found, the bot will send a message on Telegram. * * @param username the username of the required user * @param ctx the message context with the originating user * @return the id of the user */ protected int getUserIdSendError(String username, MessageContext ctx) { try { return getUser(username).getId(); } catch (IllegalStateException ex) { silent.send(getLocalizedMessage(USER_NOT_FOUND, ctx.user().getLanguageCode(), username), ctx.chatId()); throw propagate(ex); } }
boolean checkLocality(Trio<Update, Ability, String[]> trio) { Update update = trio.a(); Locality locality = isUserMessage(update) ? USER : GROUP; Locality abilityLocality = trio.b().locality(); boolean isOk = abilityLocality == ALL || locality == abilityLocality; if (!isOk) silent.send( getLocalizedMessage( CHECK_LOCALITY_FAIL, AbilityUtils.getUser(trio.a()).getLanguageCode(), abilityLocality.toString().toLowerCase()), getChatId(trio.a())); return isOk; }
boolean checkInput(Trio<Update, Ability, String[]> trio) { String[] tokens = trio.c(); int abilityTokens = trio.b().tokens(); boolean isOk = abilityTokens == 0 || (tokens.length > 0 && tokens.length == abilityTokens); if (!isOk) silent.send( getLocalizedMessage( CHECK_INPUT_FAIL, AbilityUtils.getUser(trio.a()).getLanguageCode(), abilityTokens, abilityTokens == 1 ? "input" : "inputs"), getChatId(trio.a())); return isOk; }
/** * Usage: <code>/unban @username</code> * * @return the ability to unban a user */ public Ability unbanUser() { return builder() .name(UNBAN) .locality(ALL) .privacy(ADMIN) .input(1) .action(ctx -> { String username = stripTag(ctx.firstArg()); Integer userId = getUserIdSendError(username, ctx); Set<Integer> blacklist = blacklist(); if (!blacklist.remove(userId)) silent.sendMd(getLocalizedMessage(ABILITY_UNBAN_FAIL, ctx.user().getLanguageCode(), escape(username)), ctx.chatId()); else { silent.sendMd(getLocalizedMessage(ABILITY_UNBAN_SUCCESS, ctx.user().getLanguageCode(), escape(username)), ctx.chatId()); } }) .post(commitTo(db)) .build(); }
.input(0) .action(ctx -> silent.forceReply( getLocalizedMessage(ABILITY_RECOVER_MESSAGE, ctx.user().getLanguageCode()), ctx.chatId())) .reply(update -> { String replyToMsg = update.getMessage().getReplyToMessage().getText(); String recoverMessage = getLocalizedMessage(ABILITY_RECOVER_MESSAGE, AbilityUtils.getUser(update).getLanguageCode()); if (!replyToMsg.equals(recoverMessage)) return;
boolean checkPrivacy(Trio<Update, Ability, String[]> trio) { Update update = trio.a(); User user = AbilityUtils.getUser(update); Privacy privacy; int id = user.getId(); privacy = getPrivacy(update, id); boolean isOk = privacy.compareTo(trio.b().privacy()) >= 0; if (!isOk) silent.send( getLocalizedMessage( CHECK_PRIVACY_FAIL, AbilityUtils.getUser(trio.a()).getLanguageCode()), getChatId(trio.a())); return isOk; }