Search in sources :

Example 1 with SelectType

use of com.sx4.bot.paged.PagedResult.SelectType in project Sx4 by sx4-discord-bot.

the class PagedHandler method handleMessage.

public void handleMessage(Message message) {
    MessageChannel channel = message.getChannel();
    User author = message.getAuthor();
    PagedResult<?> pagedResult = this.bot.getPagedManager().getPagedResult(channel.getIdLong(), author.getIdLong());
    if (pagedResult == null) {
        return;
    }
    if (author.getIdLong() != pagedResult.getOwnerId()) {
        return;
    }
    String content = message.getContentRaw();
    String contentLower = content.toLowerCase();
    EnumSet<SelectType> selectTypes = pagedResult.getSelect();
    if (this.next.contains(contentLower)) {
        if (pagedResult.getNextPage() != pagedResult.getPage()) {
            pagedResult.nextPage().ensure(channel);
            this.attemptDelete(message);
        }
    } else if (this.previous.contains(contentLower)) {
        if (pagedResult.getPreviousPage() != pagedResult.getPage()) {
            pagedResult.previousPage().ensure(channel);
            this.attemptDelete(message);
        }
    } else if (this.cancel.contains(contentLower)) {
        this.attemptDelete(message);
        pagedResult.delete();
    } else if (selectTypes.contains(SelectType.INDEX) && NumberUtility.isNumberUnsigned(content)) {
        try {
            int index = Integer.parseInt(content);
            if (index > pagedResult.getLowerPageBound() && index <= pagedResult.getHigherPageBound()) {
                pagedResult.select(pagedResult.isIncreasedIndex() ? index - 1 : (pagedResult.getPage() * pagedResult.getPerPage() - pagedResult.getPerPage()) + (index - 1));
                this.attemptDelete(message);
            }
        } catch (NumberFormatException ignored) {
        }
    } else {
        int index = contentLower.lastIndexOf(' ');
        if (index != -1 && this.skip.contains(contentLower.substring(0, index))) {
            try {
                int page = Integer.parseInt(contentLower.substring(index + 1));
                if (page > 0 && page <= pagedResult.getMaxPage() && page != pagedResult.getPage()) {
                    pagedResult.setPage(page).ensure(channel);
                    this.attemptDelete(message);
                    return;
                }
            } catch (NumberFormatException ignored) {
            }
        }
        if (selectTypes.contains(SelectType.OBJECT)) {
            for (int i = pagedResult.getPage() * pagedResult.getPerPage() - pagedResult.getPerPage(); i < (pagedResult.getPage() == pagedResult.getMaxPage() ? pagedResult.getList().size() : pagedResult.getPage() * pagedResult.getPerPage()); i++) {
                if (pagedResult.runSelectablePredicate(content, i)) {
                    pagedResult.select(i);
                    this.attemptDelete(message);
                }
            }
        }
    }
}
Also used : User(net.dv8tion.jda.api.entities.User) MessageChannel(net.dv8tion.jda.api.entities.MessageChannel) SelectType(com.sx4.bot.paged.PagedResult.SelectType)

Aggregations

SelectType (com.sx4.bot.paged.PagedResult.SelectType)1 MessageChannel (net.dv8tion.jda.api.entities.MessageChannel)1 User (net.dv8tion.jda.api.entities.User)1