Search in sources :

Example 1 with PrivateChannel

use of discord4j.core.object.entity.channel.PrivateChannel in project KaellyBot by Kaysoro.

the class HelpCommand method request.

@Override
public void request(MessageCreateEvent event, Message message, Matcher m, Language lg) {
    String prefix = getPrefixMdEscaped(message);
    StringBuilder st = new StringBuilder();
    List<String> messages = new ArrayList<>();
    boolean argumentFound = m.group(1) != null && m.group(1).replaceAll("^\\s+", "").length() > 0;
    for (Command command : CommandManager.getCommands()) if (command.isPublic() && !command.isAdmin() && (!command.isHidden() || argumentFound) && (message.getChannel().block() instanceof PrivateChannel || !command.isForbidden(Guild.getGuild(message.getGuild().block())))) {
        if (!argumentFound) {
            String helpCmd = command.help(lg, prefix) + "\n";
            if (st.length() + helpCmd.length() > Message.MAX_CONTENT_LENGTH) {
                messages.add(st.toString());
                st.setLength(0);
            }
            st.append(helpCmd);
        } else if (command.getName().equals(m.group(1).trim())) {
            st.append(command.helpDetailed(lg, prefix));
            break;
        }
    }
    if (st.length() > 0)
        messages.add(st.toString());
    if (argumentFound && messages.isEmpty())
        notFoundCmd.throwException(message, this, lg);
    else
        for (String msg : messages) message.getChannel().flatMap(chan -> chan.createMessage(msg)).subscribe();
}
Also used : PrivateChannel(discord4j.core.object.entity.channel.PrivateChannel) Language(enums.Language) MessageCreateEvent(discord4j.core.event.domain.message.MessageCreateEvent) AbstractCommand(commands.model.AbstractCommand) NotFoundDiscordException(exceptions.NotFoundDiscordException) Constants(data.Constants) Guild(data.Guild) ArrayList(java.util.ArrayList) List(java.util.List) Matcher(java.util.regex.Matcher) DiscordException(exceptions.DiscordException) Message(discord4j.core.object.entity.Message) PrivateChannel(discord4j.core.object.entity.channel.PrivateChannel) CommandManager(commands.CommandManager) Command(commands.model.Command) Translator(util.Translator) AbstractCommand(commands.model.AbstractCommand) Command(commands.model.Command) ArrayList(java.util.ArrayList)

Example 2 with PrivateChannel

use of discord4j.core.object.entity.channel.PrivateChannel in project KaellyBot by Kaysoro.

the class AbstractCommand method request.

@Override
public final void request(MessageCreateEvent event, Message message) {
    Language lg = Translator.getLanguageFrom(message.getChannel().block());
    try {
        Matcher m = getMatcher(message);
        boolean isFound = m.find();
        // Caché si la fonction est désactivée/réservée aux admin et que l'auteur n'est pas super-admin
        if ((!isPublic() || isAdmin()) && message.getAuthor().map(user -> user.getId().asLong() != Constants.authorId).orElse(false))
            return;
        // S'il s'agit d'une demande d'aide...
        if (message.getContent().matches(Pattern.quote(getPrefix(message)) + getName() + "\\s+help")) {
            message.getChannel().flatMap(chan -> chan.createMessage(helpDetailed(lg, getPrefix(message)))).subscribe();
            return;
        }
        // La commande est trouvée
        if (isFound) {
            // Mais n'est pas utilisable en MP
            MessageChannel channel = message.getChannel().block();
            if (!isUsableInMP() && channel instanceof PrivateChannel) {
                BasicDiscordException.NOT_USABLE_IN_MP.throwException(message, this, lg);
                return;
            } else // Mais est désactivée par la guilde
            if (!(channel instanceof PrivateChannel) && message.getAuthor().map(user -> user.getId().asLong() != Constants.authorId).orElse(false) && isForbidden(Guild.getGuild(message.getGuild().block()))) {
                BasicDiscordException.COMMAND_FORBIDDEN.throwException(message, this, lg);
                return;
            }
        } else // Mais est mal utilisée
        if (message.getContent().startsWith(getPrefix(message) + getName())) {
            badUse.throwException(message, this, lg);
            return;
        }
        if (isFound)
            request(event, message, m, lg);
    } catch (Exception e) {
        BasicDiscordException.UNKNOWN_ERROR.throwException(message, this, lg);
        Reporter.report(e);
        LOG.error("request", e);
    }
}
Also used : Language(enums.Language) MessageCreateEvent(discord4j.core.event.domain.message.MessageCreateEvent) MessageChannel(discord4j.core.object.entity.channel.MessageChannel) Logger(org.slf4j.Logger) TextChannel(discord4j.core.object.entity.channel.TextChannel) LoggerFactory(org.slf4j.LoggerFactory) Permission(discord4j.rest.util.Permission) Constants(data.Constants) Snowflake(discord4j.common.util.Snowflake) Guild(data.Guild) PermissionSet(discord4j.rest.util.PermissionSet) Reporter(util.Reporter) GuildMessageChannel(discord4j.core.object.entity.channel.GuildMessageChannel) Matcher(java.util.regex.Matcher) BadUseCommandDiscordException(exceptions.BadUseCommandDiscordException) DiscordException(exceptions.DiscordException) PrivateChannel(discord4j.core.object.entity.channel.PrivateChannel) BasicDiscordException(exceptions.BasicDiscordException) Translator(util.Translator) Pattern(java.util.regex.Pattern) discord4j.core.object.entity(discord4j.core.object.entity) PrivateChannel(discord4j.core.object.entity.channel.PrivateChannel) Language(enums.Language) MessageChannel(discord4j.core.object.entity.channel.MessageChannel) GuildMessageChannel(discord4j.core.object.entity.channel.GuildMessageChannel) Matcher(java.util.regex.Matcher) BadUseCommandDiscordException(exceptions.BadUseCommandDiscordException) DiscordException(exceptions.DiscordException) BasicDiscordException(exceptions.BasicDiscordException)

Example 3 with PrivateChannel

use of discord4j.core.object.entity.channel.PrivateChannel in project KaellyBot by Kaysoro.

the class SendNudeCommand method request.

@Override
public void request(MessageCreateEvent event, Message message, Matcher m, Language lg) {
    if (message.getChannel().blockOptional().map(chan -> chan instanceof PrivateChannel).orElse(false) || message.getChannel().blockOptional().map(chan -> ((TextChannel) chan).isNsfw()).orElse(false)) {
        int position = RANDOM.nextInt(Nude.values().length);
        Nude nude = Nude.values()[position];
        message.getChannel().flatMap(chan -> chan.createEmbed(spec -> spec.setTitle(Translator.getLabel(lg, "sendnude.title")).setDescription(Translator.getLabel(lg, "sendnude.author").replace("{author}", nude.getAuthor()).replace("{url}", nude.getUrl())).setColor(discord4j.rest.util.Color.PINK).setFooter(Translator.getLabel(lg, "sendnude.footer").replace("{position}", String.valueOf(position + 1)).replace("{number}", String.valueOf(Nude.values().length)), null).setImage(nude.getImage()))).subscribe();
    } else
        // Exception NSFW
        BasicDiscordException.NO_NSFW_CHANNEL.throwException(message, this, lg);
}
Also used : PrivateChannel(discord4j.core.object.entity.channel.PrivateChannel) Language(enums.Language) java.awt(java.awt) MessageCreateEvent(discord4j.core.event.domain.message.MessageCreateEvent) AbstractCommand(commands.model.AbstractCommand) Matcher(java.util.regex.Matcher) Message(discord4j.core.object.entity.Message) TextChannel(discord4j.core.object.entity.channel.TextChannel) PrivateChannel(discord4j.core.object.entity.channel.PrivateChannel) Nude(enums.Nude) BasicDiscordException(exceptions.BasicDiscordException) Random(java.util.Random) Translator(util.Translator) Nude(enums.Nude)

Aggregations

MessageCreateEvent (discord4j.core.event.domain.message.MessageCreateEvent)3 PrivateChannel (discord4j.core.object.entity.channel.PrivateChannel)3 Language (enums.Language)3 Matcher (java.util.regex.Matcher)3 Translator (util.Translator)3 AbstractCommand (commands.model.AbstractCommand)2 Constants (data.Constants)2 Guild (data.Guild)2 Message (discord4j.core.object.entity.Message)2 TextChannel (discord4j.core.object.entity.channel.TextChannel)2 BasicDiscordException (exceptions.BasicDiscordException)2 DiscordException (exceptions.DiscordException)2 CommandManager (commands.CommandManager)1 Command (commands.model.Command)1 Snowflake (discord4j.common.util.Snowflake)1 discord4j.core.object.entity (discord4j.core.object.entity)1 GuildMessageChannel (discord4j.core.object.entity.channel.GuildMessageChannel)1 MessageChannel (discord4j.core.object.entity.channel.MessageChannel)1 Permission (discord4j.rest.util.Permission)1 PermissionSet (discord4j.rest.util.PermissionSet)1