Search in sources :

Example 1 with TextChannel

use of net.dv8tion.jda.core.entities.TextChannel in project MantaroBot by Mantaro.

the class QuoteCmd method quote.

@Command
public static void quote(CommandRegistry cr) {
    cr.register("quote", new SimpleCommand(Category.MISC) {

        @Override
        protected void call(GuildMessageReceivedEvent event, String content, String[] args) {
            if (content.isEmpty()) {
                onHelp(event);
                return;
            }
            String action = args[0];
            String phrase = content.replace(action + " ", "");
            Guild guild = event.getGuild();
            ManagedDatabase db = MantaroData.db();
            EmbedBuilder builder = new EmbedBuilder();
            SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
            List<Message> messageHistory;
            try {
                messageHistory = event.getChannel().getHistory().retrievePast(100).complete();
            } catch (Exception e) {
                if (e instanceof PermissionException) {
                    event.getChannel().sendMessage(EmoteReference.CRYING + "I don't have permission to do this :<").queue();
                    return;
                }
                event.getChannel().sendMessage(EmoteReference.ERROR + "It seems like discord is on fire, as my" + " " + "request to retrieve message history was denied" + "with the error `" + e.getClass().getSimpleName() + "`").queue();
                log.warn("Shit exploded on Discord's backend. <@155867458203287552>", e);
                return;
            }
            if (action.equals("addfrom")) {
                Message message = messageHistory.stream().filter(msg -> msg.getContent().toLowerCase().contains(phrase.toLowerCase()) && !msg.getContent().startsWith(db.getGuild(guild).getData().getGuildCustomPrefix() == null ? MantaroData.config().get().getPrefix() : db.getGuild(guild).getData().getGuildCustomPrefix()) && !msg.getContent().startsWith(MantaroData.config().get().getPrefix())).findFirst().orElse(null);
                if (message == null) {
                    event.getChannel().sendMessage(EmoteReference.ERROR + "I couldn't find a message matching the specified search" + " criteria. Please try again with a more specific query.").queue();
                    return;
                }
                TextChannel channel = guild.getTextChannelById(message.getChannel().getId());
                Quote quote = Quote.of(guild.getMember(message.getAuthor()), channel, message);
                db.getQuotes(guild).add(quote);
                event.getChannel().sendMessage(buildQuoteEmbed(dateFormat, builder, quote)).queue();
                quote.saveAsync();
                return;
            }
            if (action.equals("random")) {
                try {
                    Quote quote = CollectionUtils.random(db.getQuotes(event.getGuild()));
                    event.getChannel().sendMessage(buildQuoteEmbed(dateFormat, builder, quote)).queue();
                } catch (Exception e) {
                    event.getChannel().sendMessage(EmoteReference.ERROR + "This server has no set quotes!").queue();
                }
                return;
            }
            if (action.equals("readfrom")) {
                try {
                    List<Quote> quotes = db.getQuotes(guild);
                    for (int i2 = 0; i2 < quotes.size(); i2++) {
                        if (quotes.get(i2).getContent().contains(phrase)) {
                            Quote quote = quotes.get(i2);
                            event.getChannel().sendMessage(buildQuoteEmbed(dateFormat, builder, quote)).queue();
                            break;
                        }
                    }
                } catch (Exception e) {
                    event.getChannel().sendMessage(EmoteReference.ERROR + "I didn't find any quotes! (no quotes match the criteria).").queue();
                }
                return;
            }
            if (action.equals("removefrom")) {
                try {
                    List<Quote> quotes = db.getQuotes(guild);
                    for (int i2 = 0; i2 < quotes.size(); i2++) {
                        if (quotes.get(i2).getContent().contains(phrase)) {
                            Quote quote = quotes.get(i2);
                            db.getQuotes(guild).remove(i2);
                            quote.saveAsync();
                            event.getChannel().sendMessage(EmoteReference.CORRECT + "Removed quote with content: " + quote.getContent()).queue();
                            break;
                        }
                    }
                } catch (Exception e) {
                    event.getChannel().sendMessage(EmoteReference.ERROR + "No quotes match the criteria.").queue();
                }
            }
        }

        @Override
        public MessageEmbed help(GuildMessageReceivedEvent event) {
            return helpEmbed(event, "Quote command").setDescription("**Quotes a message by search term.**").addField("Usage", "`~>quote addfrom <phrase>`- **Add a quote with the content defined by the specified number. For example, providing 1 will quote " + "the last message.**\n" + "`~>quote removefrom <phrase>` - **Remove a quote based on your text query.**\n" + "`~>quote readfrom <phrase>` - **Search for the first quote which matches your search criteria and prints " + "it.**\n" + "`~>quote random` - **Get a random quote.** \n", false).addField("Parameters", "`phrase` - A part of the quote phrase.", false).setColor(Color.DARK_GRAY).build();
        }
    });
}
Also used : PermissionException(net.dv8tion.jda.core.exceptions.PermissionException) SimpleCommand(net.kodehawa.mantarobot.modules.commands.SimpleCommand) Color(java.awt.Color) TextChannel(net.dv8tion.jda.core.entities.TextChannel) Date(java.util.Date) Module(net.kodehawa.mantarobot.modules.Module) SimpleDateFormat(java.text.SimpleDateFormat) Category(net.kodehawa.mantarobot.modules.commands.base.Category) Quote(net.kodehawa.mantarobot.data.entities.Quote) Message(net.dv8tion.jda.core.entities.Message) EmbedBuilder(net.dv8tion.jda.core.EmbedBuilder) Guild(net.dv8tion.jda.core.entities.Guild) PermissionException(net.dv8tion.jda.core.exceptions.PermissionException) Slf4j(lombok.extern.slf4j.Slf4j) GuildMessageReceivedEvent(net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent) List(java.util.List) CollectionUtils(br.com.brjdevs.java.utils.collections.CollectionUtils) EmoteReference(net.kodehawa.mantarobot.utils.commands.EmoteReference) MantaroData(net.kodehawa.mantarobot.data.MantaroData) ManagedDatabase(net.kodehawa.mantarobot.data.db.ManagedDatabase) CommandRegistry(net.kodehawa.mantarobot.modules.CommandRegistry) Command(net.kodehawa.mantarobot.modules.Command) MessageEmbed(net.dv8tion.jda.core.entities.MessageEmbed) MessageEmbed(net.dv8tion.jda.core.entities.MessageEmbed) Message(net.dv8tion.jda.core.entities.Message) Guild(net.dv8tion.jda.core.entities.Guild) PermissionException(net.dv8tion.jda.core.exceptions.PermissionException) Quote(net.kodehawa.mantarobot.data.entities.Quote) EmbedBuilder(net.dv8tion.jda.core.EmbedBuilder) TextChannel(net.dv8tion.jda.core.entities.TextChannel) SimpleCommand(net.kodehawa.mantarobot.modules.commands.SimpleCommand) List(java.util.List) ManagedDatabase(net.kodehawa.mantarobot.data.db.ManagedDatabase) SimpleDateFormat(java.text.SimpleDateFormat) GuildMessageReceivedEvent(net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent) SimpleCommand(net.kodehawa.mantarobot.modules.commands.SimpleCommand) Command(net.kodehawa.mantarobot.modules.Command)

Example 2 with TextChannel

use of net.dv8tion.jda.core.entities.TextChannel in project c0debaseBot by Biospheere.

the class ReadyListener method onReady.

@Override
public void onReady(ReadyEvent event) {
    super.onReady(event);
    event.getJDA().getGuilds().get(0).getMembers().forEach(member -> {
        if (!member.getUser().isBot()) {
            CodebaseBot.getInstance().getLevelManager().load(member.getUser().getId());
            if (CodebaseBot.getInstance().getLevelManager().getLevelUser(member.getUser().getId()).getLevel() >= 3 && !member.getGuild().getRolesByName("Projekt", true).isEmpty()) {
                Role role = member.getGuild().getRolesByName("Projekt", true).get(0);
                if (PermissionUtil.canInteract(member.getGuild().getSelfMember(), role)) {
                    member.getGuild().getController().addRolesToMember(member, role).queue();
                }
            }
        }
    });
    Set<Class<? extends ListenerAdapter>> classes = new Reflections("de.c0debase.bot.listener").getSubTypesOf(ListenerAdapter.class);
    classes.forEach(listenerClass -> {
        if (!listenerClass.getName().equals(this.getClass().getName())) {
            try {
                event.getJDA().addEventListener(listenerClass.newInstance());
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    });
    for (VoiceChannel voiceChannel : event.getJDA().getGuilds().get(0).getVoiceChannels()) {
        String name = ("temp-" + voiceChannel.getName().toLowerCase()).replaceAll("\\s+", "-");
        final TextChannel textChannel = voiceChannel.getGuild().getTextChannelsByName(name, true).isEmpty() ? null : voiceChannel.getGuild().getTextChannelsByName(name, true).get(0);
        if (textChannel == null) {
            CodebaseBot.getInstance().getTempchannels().put(voiceChannel.getId(), new Tempchannel());
        } else {
            Tempchannel tempchannel = new Tempchannel(textChannel);
            tempchannel.onLoad(textChannel, voiceChannel);
            CodebaseBot.getInstance().getTempchannels().put(voiceChannel.getId(), tempchannel);
        }
    }
    CodebaseBot.getInstance().getLevelManager().startInviteChecker();
}
Also used : Role(net.dv8tion.jda.core.entities.Role) ListenerAdapter(net.dv8tion.jda.core.hooks.ListenerAdapter) TextChannel(net.dv8tion.jda.core.entities.TextChannel) VoiceChannel(net.dv8tion.jda.core.entities.VoiceChannel) Tempchannel(de.c0debase.bot.tempchannel.Tempchannel) Reflections(org.reflections.Reflections)

Example 3 with TextChannel

use of net.dv8tion.jda.core.entities.TextChannel in project MantaroBot by Mantaro.

the class VoiceChannelListener method onLeave.

private void onLeave(VoiceChannel vc) {
    GuildVoiceState vs = vc.getGuild().getSelfMember().getVoiceState();
    if (validate(vs))
        return;
    if (isAlone(vc)) {
        GuildMusicManager gmm = MantaroBot.getInstance().getAudioManager().getMusicManager(vc.getGuild());
        if (gmm != null) {
            TrackScheduler scheduler = gmm.getTrackScheduler();
            if (scheduler != null && scheduler.getCurrentTrack() != null && scheduler.getRequestedChannelParsed() != null) {
                TextChannel tc = scheduler.getRequestedChannelParsed();
                if (tc.canTalk()) {
                    tc.sendMessage(EmoteReference.THINKING + "I'll leave **" + vc.getName() + "** in 2 minutes because I was left all alone :<").queue(m -> m.delete().queueAfter(30, TimeUnit.SECONDS));
                }
            }
            gmm.setAwaitingDeath(true);
            gmm.scheduleLeave();
            gmm.getAudioPlayer().setPaused(true);
        }
    }
}
Also used : TextChannel(net.dv8tion.jda.core.entities.TextChannel) GuildMusicManager(net.kodehawa.mantarobot.commands.music.GuildMusicManager) GuildVoiceState(net.dv8tion.jda.core.entities.GuildVoiceState) TrackScheduler(net.kodehawa.mantarobot.commands.music.requester.TrackScheduler)

Example 4 with TextChannel

use of net.dv8tion.jda.core.entities.TextChannel in project MantaroBot by Mantaro.

the class VoiceChannelListener method onJoin.

private void onJoin(VoiceChannel vc) {
    GuildVoiceState vs = vc.getGuild().getSelfMember().getVoiceState();
    if (validate(vs))
        return;
    if (!isAlone(vc)) {
        GuildMusicManager gmm = MantaroBot.getInstance().getAudioManager().getMusicManager(vc.getGuild());
        if (gmm != null) {
            TrackScheduler scheduler = gmm.getTrackScheduler();
            if (scheduler.getCurrentTrack() != null) {
                if (gmm.isAwaitingDeath()) {
                    TextChannel tc = scheduler.getRequestedChannelParsed();
                    if (tc.canTalk()) {
                        tc.sendMessage(EmoteReference.POPPER + "Resuming playback because someone joined!").queue();
                    }
                }
            }
            gmm.cancelLeave();
            gmm.setAwaitingDeath(false);
            gmm.getAudioPlayer().setPaused(false);
        }
    }
}
Also used : TextChannel(net.dv8tion.jda.core.entities.TextChannel) GuildMusicManager(net.kodehawa.mantarobot.commands.music.GuildMusicManager) GuildVoiceState(net.dv8tion.jda.core.entities.GuildVoiceState) TrackScheduler(net.kodehawa.mantarobot.commands.music.requester.TrackScheduler)

Example 5 with TextChannel

use of net.dv8tion.jda.core.entities.TextChannel in project MantaroBot by Mantaro.

the class VoiceChannelListener method onGuildVoiceMute.

private void onGuildVoiceMute(GuildVoiceMuteEvent event) {
    if (event.getMember().getUser().getIdLong() != event.getJDA().getSelfUser().getIdLong())
        return;
    GuildVoiceState vs = event.getVoiceState();
    if (validate(vs))
        return;
    GuildMusicManager gmm = MantaroBot.getInstance().getAudioManager().getMusicManager(event.getGuild());
    if (gmm != null) {
        if (event.isMuted()) {
            TrackScheduler scheduler = gmm.getTrackScheduler();
            if (scheduler.getCurrentTrack() != null && scheduler.getRequestedChannelParsed() != null) {
                TextChannel tc = scheduler.getRequestedChannelParsed();
                if (tc.canTalk()) {
                    tc.sendMessage(EmoteReference.SAD + "Pausing player because I got muted :(").queue();
                }
                gmm.getAudioPlayer().setPaused(true);
            }
        } else {
            if (!isAlone(vs.getChannel())) {
                if (gmm.getTrackScheduler().getCurrentTrack() != null) {
                    gmm.getAudioPlayer().setPaused(false);
                }
            }
        }
    }
}
Also used : TextChannel(net.dv8tion.jda.core.entities.TextChannel) GuildMusicManager(net.kodehawa.mantarobot.commands.music.GuildMusicManager) GuildVoiceState(net.dv8tion.jda.core.entities.GuildVoiceState) TrackScheduler(net.kodehawa.mantarobot.commands.music.requester.TrackScheduler)

Aggregations

TextChannel (net.dv8tion.jda.core.entities.TextChannel)95 Guild (net.dv8tion.jda.core.entities.Guild)23 EmbedBuilder (net.dv8tion.jda.core.EmbedBuilder)22 List (java.util.List)18 Message (net.dv8tion.jda.core.entities.Message)18 User (net.dv8tion.jda.core.entities.User)18 Member (net.dv8tion.jda.core.entities.Member)16 ArrayList (java.util.ArrayList)14 VoiceChannel (net.dv8tion.jda.core.entities.VoiceChannel)12 Collectors (java.util.stream.Collectors)11 GuildWrapper (stream.flarebot.flarebot.objects.GuildWrapper)11 MessageUtils (stream.flarebot.flarebot.util.MessageUtils)11 Permission (net.dv8tion.jda.core.Permission)7 CommandType (stream.flarebot.flarebot.commands.CommandType)7 IOException (java.io.IOException)6 Consumer (java.util.function.Consumer)6 Role (net.dv8tion.jda.core.entities.Role)6 MantaroData (net.kodehawa.mantarobot.data.MantaroData)6 DBGuild (net.kodehawa.mantarobot.db.entities.DBGuild)6 EmoteReference (net.kodehawa.mantarobot.utils.commands.EmoteReference)6