Search in sources :

Example 1 with TextChannel

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

the class GuildReadyListener method initTempchannel.

private void initTempchannel(final Guild guild) {
    for (VoiceChannel voiceChannel : guild.getVoiceChannels()) {
        final 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) {
            bot.getTempchannels().put(voiceChannel.getId(), new Tempchannel());
        } else {
            Tempchannel tempchannel = new Tempchannel(textChannel);
            tempchannel.onLoad(textChannel, voiceChannel);
            bot.getTempchannels().put(voiceChannel.getId(), tempchannel);
        }
    }
}
Also used : TextChannel(net.dv8tion.jda.api.entities.TextChannel) VoiceChannel(net.dv8tion.jda.api.entities.VoiceChannel) Tempchannel(de.c0debase.bot.tempchannel.Tempchannel)

Example 2 with TextChannel

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

the class Tempchannel method onTempchannelJoin.

@Override
public void onTempchannelJoin(final VoiceChannel voiceChannel, final Member member) {
    if (voiceChannel.equals(voiceChannel.getGuild().getAfkChannel())) {
        return;
    }
    if (textChannel == null) {
        final Guild guild = voiceChannel.getGuild();
        voiceChannel.getParent().createTextChannel("temp-" + voiceChannel.getName().toLowerCase()).addPermissionOverride(guild.getSelfMember(), MEMBER_PERMISSIONS, null).addPermissionOverride(member, MEMBER_PERMISSIONS, null).addPermissionOverride(guild.getPublicRole(), null, MEMBER_PERMISSIONS).queue(channel -> setTextChannel((TextChannel) channel));
    } else {
        if (textChannel.getPermissionOverride(member) != null) {
            textChannel.getPermissionOverride(member).getManager().grant(MEMBER_PERMISSIONS).queue();
        } else {
            textChannel.createPermissionOverride(member).setAllow(MEMBER_PERMISSIONS).queue();
        }
        if (member.getUser().isBot()) {
            return;
        }
        final EmbedBuilder embedBuilder = new EmbedBuilder();
        embedBuilder.setColor(member.getColor());
        embedBuilder.setDescription(":arrow_right: " + member.getAsMention() + " ist beigetreten");
        textChannel.sendMessage(embedBuilder.build()).queue();
    }
}
Also used : TextChannel(net.dv8tion.jda.api.entities.TextChannel) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) Guild(net.dv8tion.jda.api.entities.Guild)

Example 3 with TextChannel

use of net.dv8tion.jda.api.entities.TextChannel in project Saber-Bot by notem.

the class EditCommand method verify.

@Override
public String verify(String prefix, String[] args, MessageReceivedEvent event) {
    String head = prefix + this.name();
    int index = 0;
    if (args.length < 1) {
        return "That's not enough arguments! Use ``" + head + " <ID> [<option> <arg>]``";
    }
    // check first arg
    if (!VerifyUtilities.verifyEntryID(args[index])) {
        return "``" + args[index] + "`` is not a valid entry ID!";
    }
    Integer Id = ParsingUtilities.encodeIDToInt(args[index]);
    ScheduleEntry entry = Main.getEntryManager().getEntryFromGuild(Id, event.getGuild().getId());
    if (entry == null) {
        return "I could not find an entry with that ID!";
    }
    if (Main.getScheduleManager().isLocked(entry.getChannelId())) {
        return "This schedule is locked. Please try again after the sort/sync operation finishes.";
    }
    TextChannel channel = event.getGuild().getTextChannelById(entry.getChannelId());
    if (!event.getGuild().getMember(event.getJDA().getSelfUser()).hasPermission(channel, Permission.MESSAGE_HISTORY)) {
        return "The bot must have the \"Read Message History\" permission on channel for which that event exists!";
    }
    // if only one argument, command is valid
    if (args.length == 1)
        return "";
    // 1
    index++;
    // check later args
    ZoneId zone = Main.getScheduleManager().getTimeZone(entry.getChannelId());
    String verify;
    while (index < args.length) {
        switch(args[index++].toLowerCase()) {
            case "c":
            case "comment":
            case "comments":
                if (args.length - index < 1) {
                    return "That's not enough arguments for *comment*!\n" + "Use ``" + head + " " + args[0] + " " + args[index - 1] + " [add|remove|swap] <arg(s)>``";
                }
                switch(args[index++].toLowerCase()) {
                    case "a":
                    case "add":
                        verify = VerifyUtilities.verifyCommentAdd(args, index, head);
                        if (!verify.isEmpty())
                            return verify;
                        index++;
                        break;
                    case "r":
                    case "remove":
                        verify = VerifyUtilities.verifyCommentRemove(args, index, head, entry);
                        if (!verify.isEmpty())
                            return verify;
                        index++;
                        break;
                    case "s":
                    case "swap":
                        verify = VerifyUtilities.verifyCommentSwap(args, index, head, entry);
                        if (!verify.isEmpty())
                            return verify;
                        index += 2;
                        break;
                    case "m":
                    case "modify":
                    case "replace":
                    case "set":
                        if (args.length - index < 2) {
                            return "That's not the right number of arguments for **" + args[index - 1] + "**!\n" + "Use ``" + head + " " + args[index - 2] + " " + args[index - 1] + " [num] [new_comment]``!";
                        }
                        if (!VerifyUtilities.verifyInteger(args[index])) {
                            return "The argument **" + args[index] + "** is not right!\n" + "This needs to be a number representing the comment number" + " you wish to replace!";
                        }
                        int num = Integer.parseInt(args[index]);
                        if (!(num <= entry.getComments().size() && num > 0)) {
                            return "The provided comment number must be between 1 and " + entry.getComments().size() + "!";
                        }
                        index += 2;
                        break;
                    default:
                        return "The only valid options for ``comment`` are **add**, **remove**, **modify**, or **swap**!";
                }
                break;
            case "desc":
            case "description":
                if (args.length - index < 1) {
                    return "That's not the right number of arguments for **" + args[index - 1] + "**!\n" + "Use ``" + head + " " + args[index - 2] + " " + args[index - 1] + " [description]``";
                }
                index++;
                break;
            case "s":
            case "starts":
            case "start":
                if (args.length - index < 1) {
                    return "That's not the right number of arguments for **" + args[index - 1] + "**!\n" + "Use ``" + head + " " + args[index - 2] + " " + args[index - 1] + " [start time]``";
                }
                if (!VerifyUtilities.verifyTime(args[index])) {
                    return "I could not understand **" + args[index] + "** as a time!" + "\nPlease use the format hh:mm[am|pm].";
                }
                if (ZonedDateTime.of(entry.getStart().toLocalDate(), ParsingUtilities.parseTime(args[index], entry.getStart().getZone()), entry.getStart().getZone()).isBefore(ZonedDateTime.now())) {
                    return "Today's time is already past *" + args[index] + "*!\n" + "Please use a different time, or change the date for the event!";
                }
                if (entry.hasStarted()) {
                    return "You cannot modify the start time after the event has already started.";
                }
                index++;
                break;
            case "e":
            case "ends":
            case "end":
                if (args.length - index < 1) {
                    return "That's not the right number of arguments for **" + args[index - 1] + "**! " + "Use ``" + head + " " + args[0] + " " + args[index - 1] + " [end time]``";
                }
                // if time is not "off" or an invalid time, fail
                if (!VerifyUtilities.verifyTime(args[index]) && !args[index].equalsIgnoreCase("off")) {
                    return "I could not understand **" + args[index] + "** as a time!\n" + "Please use the format hh:mm[am|pm].";
                }
                // if time is not "off" do additional check
                if (!args[index].equalsIgnoreCase("off")) {
                    ZonedDateTime end = ZonedDateTime.of(entry.getEnd().toLocalDate(), ParsingUtilities.parseTime(args[index], entry.getEnd().getZone()), entry.getEnd().getZone());
                    if (end.isBefore(ZonedDateTime.now())) {
                        return "Today's time is already past *" + args[index] + "*!\n" + "Please use a different time, or change the date for the event!";
                    }
                }
                index++;
                break;
            case "t":
            case "title":
                if (args.length - index < 1) {
                    return "That's not the right number of arguments for **" + args[index - 1] + "**! " + "Use ``" + head + " " + args[0] + " " + args[index - 1] + " [\"title\"]``";
                }
                if (args[index].length() > 255) {
                    return "Your title can be at most 255 characters!";
                }
                index++;
                break;
            case "d":
            case "date":
            case "sd":
            case "start-date":
                verify = VerifyUtilities.verifyDate(args, index, head, entry, zone, true);
                if (!verify.isEmpty())
                    return verify;
                index++;
                break;
            case "ed":
            case "end-date":
                verify = VerifyUtilities.verifyDate(args, index, head, entry, zone, false);
                if (!verify.isEmpty())
                    return verify;
                index++;
                break;
            case "r":
            case "repeats":
            case "repeat":
                verify = VerifyUtilities.verifyRepeat(args, index, head);
                if (!verify.isEmpty())
                    return verify;
                index++;
                break;
            case "im":
            case "image":
            case "th":
            case "thumbnail":
            case "u":
            case "url":
                verify = VerifyUtilities.verifyUrl(args, index, head);
                if (!verify.isEmpty())
                    return verify;
                index++;
                break;
            case "qs":
            case "quiet-start":
            case "qe":
            case "quiet-end":
            case "qr":
            case "quiet-remind":
            case "quiet-all":
            case "qa":
                break;
            case "ex":
            case "expire":
                verify = VerifyUtilities.verifyExpire(args, index, head, zone);
                if (!verify.isEmpty())
                    return verify;
                index++;
                break;
            case "limit":
            case "l":
                verify = VerifyUtilities.verifyLimit(args, index, head, entry);
                if (!verify.isEmpty())
                    return verify;
                index += 2;
                break;
            case "deadline":
            case "dl":
                verify = VerifyUtilities.verifyDeadline(args, index, head);
                if (!verify.isEmpty())
                    return verify;
                index++;
                if (index < args.length && VerifyUtilities.verifyTime(args[index]) && !args[index].contains("@"))
                    index++;
                break;
            case "co":
            case "count":
                verify = VerifyUtilities.verifyCount(args, index, head);
                if (!verify.isEmpty())
                    return verify;
                index++;
                break;
            case "lo":
            case "location":
                verify = VerifyUtilities.verifyLocation(args, index, head);
                if (!verify.isEmpty())
                    return verify;
                index++;
                break;
            case "an":
            case "announce":
            case "announcement":
            case "announcements":
                if (args.length - index < 1) {
                    return "That's not the right number of arguments for **" + args[index - 1] + "**!\n" + "Use ``" + head + " " + args[0] + " " + args[index - 1] + " [add|remove] [#target] [time] [message]``";
                }
                switch(args[index++].toLowerCase()) {
                    case "a":
                    case "add":
                        verify = VerifyUtilities.verifyAnnouncementAdd(args, index, head, event);
                        if (!verify.isEmpty())
                            return verify;
                        index += 3;
                        break;
                    case "r":
                    case "remove":
                        verify = VerifyUtilities.verifyAnnouncementRemove(args, index, head, entry);
                        if (!verify.isEmpty())
                            return verify;
                        index++;
                        break;
                    default:
                        return "**" + args[index - 1] + "** is not a valid option!\n" + "You should use either *add* or *remove*!";
                }
                break;
            case "a":
            case "add":
                if (args.length - index < 1) {
                    return "That's not the right number of arguments!\n" + "Use ``" + head + " " + args[0] + " " + args[index - 1] + " [announcement|comment] [args]";
                }
                switch(args[index++].toLowerCase()) {
                    case "a":
                    case "an":
                    case "announce":
                    case "announcement":
                    case "announcements":
                        verify = VerifyUtilities.verifyAnnouncementAdd(args, index, head, event);
                        if (!verify.isEmpty())
                            return verify;
                        index += 3;
                        break;
                    case "c":
                    case "comment":
                    case "comments":
                        verify = VerifyUtilities.verifyCommentAdd(args, index, head);
                        if (!verify.isEmpty())
                            return verify;
                        index++;
                        break;
                    default:
                        return "*" + args[index - 1] + "* is not a valid option!\n" + "Please use either *comment* or **announcement*!";
                }
                break;
            case "re":
            case "remove":
                if (args.length - index < 1) {
                    return "That's not the right number of arguments!\n" + "Use ``" + head + " " + args[0] + " " + args[index - 1] + " [announcement|comment] [args]";
                }
                switch(args[index++].toLowerCase()) {
                    case "a":
                    case "an":
                    case "announce":
                    case "announcement":
                    case "announcements":
                        verify = VerifyUtilities.verifyAnnouncementRemove(args, index, head, entry);
                        if (!verify.isEmpty())
                            return verify;
                        index++;
                        break;
                    case "c":
                    case "comment":
                    case "comments":
                        verify = VerifyUtilities.verifyCommentRemove(args, index, head, entry);
                        if (!verify.isEmpty())
                            return verify;
                        index++;
                        break;
                    default:
                        return "*" + args[index - 1] + "* is not a valid option!\n" + "Please use either *comment* or **announcement*!";
                }
                break;
            case "color":
                verify = VerifyUtilities.verifyColor(args, index, head);
                if (!verify.isEmpty())
                    return verify;
                index++;
                break;
            case "n":
            case "nonembed":
                if (args.length - index < 1)
                    return "That's not the right number of arguments!\n" + "Use ``" + head + " " + args[0] + " " + args[index - 1] + " [non_embeded_text]";
                if (args[index].length() > 1024)
                    return "The described text to appear outside the event message embed is too long!" + "\nThe text should be at most 1024 characters!";
                index++;
                break;
            default:
                return "**" + args[index - 1] + "** is not an option I know of!\n" + "Please use the ``help`` command to see available options!";
        }
    }
    // return valid
    return "";
}
Also used : TextChannel(net.dv8tion.jda.api.entities.TextChannel) ScheduleEntry(ws.nmathe.saber.core.schedule.ScheduleEntry) ZoneId(java.time.ZoneId) ZonedDateTime(java.time.ZonedDateTime)

Example 4 with TextChannel

use of net.dv8tion.jda.api.entities.TextChannel in project Saber-Bot by notem.

the class InitCommand method action.

@Override
public void action(String prefix, String[] args, MessageReceivedEvent event) {
    String body;
    if (args.length > 0) {
        boolean isAChannel = false;
        TextChannel chan = null;
        String chanId = null;
        try {
            chanId = args[0].replaceAll("[^\\d]", "");
            chan = event.getGuild().getTextChannelById(chanId);
            if (chan != null)
                isAChannel = true;
        } catch (Exception ignored) {
        }
        if (// use the argument as the new channel's name
        !isAChannel) {
            String chanTitle = args[0].replaceAll("[^A-Za-z0-9_ \\-]", "").replaceAll("[ ]", "_");
            Main.getScheduleManager().createSchedule(event.getGuild().getId(), chanTitle);
            body = "A new schedule channel named **" + chanTitle.toLowerCase() + "** has been created!\n" + "You can now use the create command to create events on that schedule, or the sync command to sync " + "that schedule to a Google Calendar.";
        } else // convert the channel to a schedule
        {
            if (Main.getScheduleManager().isSchedule(chan.getId())) {
                // clear the channel of events
                TextChannel finalChan = chan;
                Main.getDBDriver().getEventCollection().find(eq("channelId", chan.getId())).forEach((Consumer<? super Document>) document -> {
                    String msgId = document.getString("messageId");
                    finalChan.deleteMessageById(msgId).complete();
                    Main.getEntryManager().removeEntry(document.getInteger("_id"));
                });
                body = "The schedule <#" + chanId + "> has been cleared!";
            } else {
                // create a new schedule
                Main.getScheduleManager().createSchedule(chan);
                body = "The channel <#" + chanId + "> has been converted to a schedule channel!\n" + "You can now use the create command to create events on that schedule, or the sync " + "command to sync that schedule to a Google Calendar.";
            }
        }
    } else // create a new schedule using the default name
    {
        Main.getScheduleManager().createSchedule(event.getGuild().getId(), null);
        body = "A new schedule channel named **new_schedule** has been created!\n" + "You can now use the create command to create events on that schedule, or the sync command" + " to sync that schedule to a Google Calendar.";
    }
    MessageUtilities.sendMsg(body, event.getChannel(), null);
}
Also used : Document(org.bson.Document) Consumer(java.util.function.Consumer) Command(ws.nmathe.saber.commands.Command) CommandInfo(ws.nmathe.saber.commands.CommandInfo) Permission(net.dv8tion.jda.api.Permission) MessageUtilities(ws.nmathe.saber.utils.MessageUtilities) Filters.eq(com.mongodb.client.model.Filters.eq) Main(ws.nmathe.saber.Main) TextChannel(net.dv8tion.jda.api.entities.TextChannel) MessageReceivedEvent(net.dv8tion.jda.api.events.message.MessageReceivedEvent) TextChannel(net.dv8tion.jda.api.entities.TextChannel)

Example 5 with TextChannel

use of net.dv8tion.jda.api.entities.TextChannel in project Saber-Bot by notem.

the class PurgeCommand method action.

@Override
public void action(String prefix, String[] args, MessageReceivedEvent event) {
    TextChannel channel = event.getGuild().getJDA().getTextChannelById(args[0].replaceAll("[^\\d]", ""));
    // number of messages to remove
    Integer[] count = { 100 };
    // ID of bot to check messages against
    String botId = event.getJDA().getSelfUser().getId();
    processing.put(event.getGuild().getId(), channel.getId());
    channel.getIterableHistory().stream().filter(message -> message.getAuthor().getId().equals(botId) && (count[0]-- > 0)).forEach((message -> {
        message.getChannel().sendTyping().queue();
        // sleep for half a second before continuing
        try {
            Thread.sleep(500);
        } catch (InterruptedException ignored) {
        }
        Bson query = eq("messageId", message.getId());
        if (Main.getDBDriver().getEventCollection().count(query) == 0) {
            MessageUtilities.deleteMsg(message);
        }
    }));
    processing.remove(event.getGuild().getId(), channel.getId());
    // send success message
    String content = "Finished purging old message.";
    MessageUtilities.sendMsg(content, event.getTextChannel(), null);
}
Also used : Bson(org.bson.conversions.Bson) Command(ws.nmathe.saber.commands.Command) MessageChannel(net.dv8tion.jda.api.entities.MessageChannel) CommandInfo(ws.nmathe.saber.commands.CommandInfo) RateLimiter(ws.nmathe.saber.core.RateLimiter) Map(java.util.Map) MessageUtilities(ws.nmathe.saber.utils.MessageUtilities) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Filters.eq(com.mongodb.client.model.Filters.eq) Main(ws.nmathe.saber.Main) TextChannel(net.dv8tion.jda.api.entities.TextChannel) MessageReceivedEvent(net.dv8tion.jda.api.events.message.MessageReceivedEvent) TextChannel(net.dv8tion.jda.api.entities.TextChannel) Bson(org.bson.conversions.Bson)

Aggregations

TextChannel (net.dv8tion.jda.api.entities.TextChannel)12 Document (org.bson.Document)5 Main (ws.nmathe.saber.Main)5 Consumer (java.util.function.Consumer)4 JDA (net.dv8tion.jda.api.JDA)4 Bson (org.bson.conversions.Bson)4 MessageUtilities (ws.nmathe.saber.utils.MessageUtilities)4 Calendar (com.google.api.services.calendar.Calendar)3 Filters.eq (com.mongodb.client.model.Filters.eq)3 Permission (net.dv8tion.jda.api.Permission)3 Guild (net.dv8tion.jda.api.entities.Guild)3 MessageChannel (net.dv8tion.jda.api.entities.MessageChannel)3 Logging (ws.nmathe.saber.utils.Logging)3 Credential (com.google.api.client.auth.oauth2.Credential)2 Filters (com.mongodb.client.model.Filters)2 Projections.fields (com.mongodb.client.model.Projections.fields)2 Projections.include (com.mongodb.client.model.Projections.include)2 ZonedDateTime (java.time.ZonedDateTime)2 Message (net.dv8tion.jda.api.entities.Message)2 MessageReceivedEvent (net.dv8tion.jda.api.events.message.MessageReceivedEvent)2