Search in sources :

Example 1 with GuildMessageChannel

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

the class ServerCommand method request.

@Override
public void request(MessageCreateEvent event, Message message, Matcher m, Language lg) {
    Guild guild = Guild.getGuild(message.getGuild().block());
    MessageChannel channel = message.getChannel().block();
    if (m.group(1) != null) {
        String serverName = m.group(1).toLowerCase().trim();
        if (serverName.equals("-list")) {
            String sb = Translator.getLabel(lg, "server.list") + "\n" + getServersList(lg);
            message.getChannel().flatMap(chan -> chan.createMessage(sb)).subscribe();
            return;
        }
        if (isUserHasEnoughRights(message)) {
            if (!serverName.startsWith("-channel")) {
                ServerUtils.ServerQuery query = ServerUtils.getServerDofusFromName(serverName, lg);
                if (query.hasSucceed()) {
                    guild.setServer(query.getServer());
                    message.getChannel().flatMap(chan -> chan.createMessage(Translator.getLabel(lg, "server.request.1").replace("{game}", Constants.game.getName()) + " " + guild.getName() + " " + Translator.getLabel(lg, "server.request.2") + " " + query.getServer().getLabel(lg) + ".")).subscribe();
                } else
                    query.getExceptions().forEach(exception -> exception.throwException(message, this, lg, query.getServersFound()));
            } else {
                ServerUtils.ServerQuery query = ServerUtils.getServerDofusFromName(serverName.replace("-channel", "").trim(), lg);
                ChannelServer channelServer = ChannelServer.getChannelServers().get(channel.getId().asLong());
                if (query.hasSucceed()) {
                    if (channelServer != null) {
                        if (channelServer.getServer().getName().equals(query.getServer().getName())) {
                            channelServer.removeToDatabase();
                            channel.createMessage(((GuildMessageChannel) channel).getName() + " " + Translator.getLabel(lg, "server.request.4").replace("{server}", guild.getServerDofus().toString())).subscribe();
                        } else {
                            channelServer.setServer(query.getServer());
                            message.getChannel().flatMap(salon -> salon.createMessage(((GuildMessageChannel) channel).getName() + " " + Translator.getLabel(lg, "server.request.4").replace("{server}", query.getServer().toString()))).subscribe();
                        }
                    } else {
                        final ChannelServer CHAN = new ChannelServer(query.getServer(), channel.getId().asLong());
                        CHAN.addToDatabase();
                        message.getChannel().flatMap(salon -> salon.createMessage(((GuildMessageChannel) channel).getName() + " " + Translator.getLabel(lg, "server.request.4").replace("{server}", query.getServer().toString()))).subscribe();
                    }
                } else
                    query.getExceptions().forEach(exception -> exception.throwException(message, this, lg, query.getServersFound()));
            }
        } else
            BasicDiscordException.NO_ENOUGH_RIGHTS.throwException(message, this, lg);
    } else {
        String text = "**" + guild.getName() + "** " + (guild.getServerDofus() != null ? Translator.getLabel(lg, "server.request.4").replace("{server}", guild.getServerDofus().getLabel(lg)) : Translator.getLabel(lg, "server.request.5")).replace("{game}", Constants.game.getName());
        ChannelServer channelServer = ChannelServer.getChannelServers().get(channel.getId().asLong());
        if (channelServer != null)
            text += "\n" + Translator.getLabel(lg, "server.request.6").replace("{channel}", ((GuildMessageChannel) channel).getName()).replace("{server}", channelServer.getServer().getLabel(lg));
        channel.createMessage(text).subscribe();
    }
}
Also used : Language(enums.Language) MessageCreateEvent(discord4j.core.event.domain.message.MessageCreateEvent) AbstractCommand(commands.model.AbstractCommand) MessageChannel(discord4j.core.object.entity.channel.MessageChannel) data(data) ServerUtils(util.ServerUtils) Collectors(java.util.stream.Collectors) List(java.util.List) GuildMessageChannel(discord4j.core.object.entity.channel.GuildMessageChannel) Matcher(java.util.regex.Matcher) Message(discord4j.core.object.entity.Message) Game(enums.Game) BasicDiscordException(exceptions.BasicDiscordException) Translator(util.Translator) MessageChannel(discord4j.core.object.entity.channel.MessageChannel) GuildMessageChannel(discord4j.core.object.entity.channel.GuildMessageChannel) ServerUtils(util.ServerUtils) GuildMessageChannel(discord4j.core.object.entity.channel.GuildMessageChannel)

Example 2 with GuildMessageChannel

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

the class LanguageCommand method request.

@Override
public void request(MessageCreateEvent event, Message message, Matcher m, Language lg) {
    Optional<discord4j.core.object.entity.Guild> guild = message.getGuild().blockOptional();
    Optional<MessageChannel> channel = message.getChannel().blockOptional();
    if (guild.isPresent() && channel.isPresent())
        if (m.group(2) != null) {
            // Ajouts
            String channelName = ((GuildMessageChannel) channel.get()).getName();
            if (isUserHasEnoughRights(message)) {
                List<Language> langs = new ArrayList<>();
                for (Language lang : Language.values()) if (m.group(2).trim().toUpperCase().equals(lang.getAbrev()))
                    langs.add(lang);
                if (langs.size() == 1) {
                    if (m.group(1) == null) {
                        Guild.getGuild(guild.get()).setLanguage(langs.get(0));
                        final Language LG = langs.get(0);
                        message.getChannel().flatMap(chan -> chan.createMessage(guild.get().getName() + " " + Translator.getLabel(LG, "lang.request.1") + " " + langs.get(0))).subscribe();
                    } else {
                        ChannelLanguage chan = ChannelLanguage.getChannelLanguages().get(channel.get().getId().asLong());
                        if (chan != null) {
                            if (chan.getLang().equals(langs.get(0))) {
                                chan.removeToDatabase();
                                final Language LG = Translator.getLanguageFrom(channel.get());
                                message.getChannel().flatMap(salon -> salon.createMessage(channelName + " " + Translator.getLabel(LG, "lang.request.2") + " " + Guild.getGuild(guild.get()).getLanguage())).subscribe();
                            } else {
                                chan.setLanguage(langs.get(0));
                                final Language LG = langs.get(0);
                                message.getChannel().flatMap(salon -> salon.createMessage(channelName + " " + Translator.getLabel(LG, "lang.request.1") + " " + chan.getLang())).subscribe();
                            }
                        } else {
                            final Language LG = langs.get(0);
                            final ChannelLanguage CHAN = new ChannelLanguage(langs.get(0), channel.get().getId().asLong());
                            CHAN.addToDatabase();
                            message.getChannel().flatMap(salon -> salon.createMessage(channelName + " " + Translator.getLabel(LG, "lang.request.1") + " " + CHAN.getLang())).subscribe();
                        }
                    }
                } else if (langs.isEmpty())
                    notFoundLang.throwException(message, this, lg);
                else
                    tooMuchLangs.throwException(message, this, lg);
            } else
                BasicDiscordException.NO_ENOUGH_RIGHTS.throwException(message, this, lg);
        } else {
            // Consultation
            String text = "**" + guild.get().getName() + "** " + Translator.getLabel(lg, "lang.request.3") + " " + Guild.getGuild(guild.get()).getLanguage() + ".";
            ChannelLanguage chanLang = ChannelLanguage.getChannelLanguages().get(channel.get().getId().asLong());
            if (chanLang != null)
                text += "\nLe salon *" + ((GuildMessageChannel) channel.get()).getName() + "* " + Translator.getLabel(lg, "lang.request.3") + " " + chanLang.getLang() + ".";
            final String TEXT = text;
            message.getChannel().flatMap(salon -> salon.createMessage(TEXT)).subscribe();
        }
}
Also used : ChannelLanguage(data.ChannelLanguage) MessageChannel(discord4j.core.object.entity.channel.MessageChannel) GuildMessageChannel(discord4j.core.object.entity.channel.GuildMessageChannel) Language(enums.Language) ChannelLanguage(data.ChannelLanguage) ArrayList(java.util.ArrayList) List(java.util.List) Guild(data.Guild)

Example 3 with GuildMessageChannel

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

the class GuildCreateListener method onReady.

public Flux<MessageData> onReady(GuildCreateEvent event) {
    try {
        if (!Guild.getGuilds().containsKey(event.getGuild().getId().asString())) {
            return event.getGuild().getChannels().filter(chan -> chan instanceof GuildMessageChannel).map(chan -> (GuildMessageChannel) chan).take(1).flatMap(chan -> {
                Guild guild = new Guild(event.getGuild().getId().asString(), event.getGuild().getName(), Translator.detectLanguage(chan));
                guild.addToDatabase();
                Language lg = guild.getLanguage();
                LOG.info("La guilde " + guild.getId() + " - " + guild.getName() + " a ajouté " + Constants.name);
                return event.getGuild().getOwner().flatMap(owner -> {
                    String customMessage = Translator.getLabel(lg, "welcome.message").replaceAll("\\{name}", Constants.name).replaceAll("\\{game}", Constants.game.getName()).replaceAll("\\{prefix}", Constants.prefixCommand).replaceAll("\\{help}", HelpCommand.NAME).replaceAll("\\{server}", new ServerCommand().getName()).replaceAll("\\{lang}", new LanguageCommand().getName()).replaceAll("\\{twitter}", new TwitterCommand().getName()).replaceAll("\\{almanax-auto}", new AlmanaxAutoCommand().getName()).replaceAll("\\{rss}", new RSSCommand().getName()).replaceAll("\\{owner}", owner.getMention()).replaceAll("\\{guild}", event.getGuild().getName());
                    return chan.createMessage(customMessage).onErrorResume(ignored -> sendWelcomeMessageInPM(event, customMessage)).then(ClientConfig.DISCORD().getChannelById(Snowflake.of(Constants.chanReportID)).createMessage("[NEW] **" + event.getGuild().getName() + "** (" + guild.getLanguage().getAbrev() + "), +" + event.getGuild().getMemberCount() + " utilisateurs"));
                });
            });
        }
    } catch (Exception e) {
        Reporter.report(e, event.getGuild());
        LOG.error("onReady", e);
    }
    return Flux.empty();
}
Also used : commands.config(commands.config) Language(enums.Language) GuildCreateEvent(discord4j.core.event.domain.guild.GuildCreateEvent) Logger(org.slf4j.Logger) TextChannel(discord4j.core.object.entity.channel.TextChannel) ClientException(discord4j.rest.http.client.ClientException) LoggerFactory(org.slf4j.LoggerFactory) Mono(reactor.core.publisher.Mono) HelpCommand(commands.classic.HelpCommand) DiscordClient(discord4j.core.DiscordClient) Permission(discord4j.rest.util.Permission) Snowflake(discord4j.common.util.Snowflake) Constants(data.Constants) Guild(data.Guild) Flux(reactor.core.publisher.Flux) Reporter(util.Reporter) GuildMessageChannel(discord4j.core.object.entity.channel.GuildMessageChannel) Message(discord4j.core.object.entity.Message) Member(discord4j.core.object.entity.Member) MessageData(discord4j.discordjson.json.MessageData) Translator(util.Translator) ClientConfig(util.ClientConfig) Language(enums.Language) Guild(data.Guild) ClientException(discord4j.rest.http.client.ClientException) GuildMessageChannel(discord4j.core.object.entity.channel.GuildMessageChannel)

Example 4 with GuildMessageChannel

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

the class Translator method getLanguageFrom.

/**
 * Fournit la langue utilisée dans un salon textuel
 * @param channel Salon textuel
 * @return Langue de la guilde ou du salon si précisé
 */
public static Language getLanguageFrom(MessageChannel channel) {
    Language result = Constants.defaultLanguage;
    if (channel instanceof GuildMessageChannel) {
        Guild guild = Guild.getGuild(((GuildMessageChannel) channel).getGuild().block());
        result = guild.getLanguage();
        ChannelLanguage channelLanguage = ChannelLanguage.getChannelLanguages().get(channel.getId().asLong());
        if (channelLanguage != null)
            result = channelLanguage.getLang();
    }
    return result;
}
Also used : ChannelLanguage(data.ChannelLanguage) Language(enums.Language) ChannelLanguage(data.ChannelLanguage) Guild(data.Guild) GuildMessageChannel(discord4j.core.object.entity.channel.GuildMessageChannel)

Aggregations

GuildMessageChannel (discord4j.core.object.entity.channel.GuildMessageChannel)4 Language (enums.Language)4 Guild (data.Guild)3 ChannelLanguage (data.ChannelLanguage)2 Message (discord4j.core.object.entity.Message)2 MessageChannel (discord4j.core.object.entity.channel.MessageChannel)2 List (java.util.List)2 Translator (util.Translator)2 HelpCommand (commands.classic.HelpCommand)1 commands.config (commands.config)1 AbstractCommand (commands.model.AbstractCommand)1 data (data)1 Constants (data.Constants)1 Snowflake (discord4j.common.util.Snowflake)1 DiscordClient (discord4j.core.DiscordClient)1 GuildCreateEvent (discord4j.core.event.domain.guild.GuildCreateEvent)1 MessageCreateEvent (discord4j.core.event.domain.message.MessageCreateEvent)1 Member (discord4j.core.object.entity.Member)1 TextChannel (discord4j.core.object.entity.channel.TextChannel)1 MessageData (discord4j.discordjson.json.MessageData)1