Search in sources :

Example 31 with JDA

use of net.dv8tion.jda.core.JDA in project Saber-Bot by notem.

the class ScheduleManager method deleteSchedule.

/**
 * Removes a schedule and attempts to delete the schedule's channel
 * @param cId (String) ID of channel / schedule (synonymous)
 */
public void deleteSchedule(String cId) {
    // identify which shard is responsible for the schedule
    Document doc = Main.getDBDriver().getScheduleCollection().find(eq("_id", cId)).projection(fields(include("guildId"))).first();
    JDA jda = Main.getShardManager().getJDA(doc.getString("guildId"));
    try {
        jda.getTextChannelById(cId).delete().complete();
    } catch (PermissionException e) {
        String m = e.getMessage() + ": " + e.getPermission();
        Logging.warn(this.getClass(), m);
    } catch (Exception e) {
        Logging.exception(this.getClass(), e);
    }
    Main.getDBDriver().getEventCollection().deleteMany(eq("channelId", cId));
    Main.getDBDriver().getScheduleCollection().deleteOne(eq("_id", cId));
}
Also used : PermissionException(net.dv8tion.jda.core.exceptions.PermissionException) JDA(net.dv8tion.jda.core.JDA) Document(org.bson.Document) PermissionException(net.dv8tion.jda.core.exceptions.PermissionException)

Example 32 with JDA

use of net.dv8tion.jda.core.JDA in project Saber-Bot by notem.

the class ScheduleManager method createSchedule.

/**
 * Create a new schedule and it's associated schedule channel, if the bot cannot create the
 * new channel no schedule will be created
 * @param gId (String) guild ID
 * @param optional (String) optional name of schedule channel
 */
public void createSchedule(String gId, String optional) {
    JDA jda = Main.getShardManager().getJDA(gId);
    // bot self permissions
    Collection<Permission> channelPerms = Stream.of(Permission.MESSAGE_ADD_REACTION, Permission.MESSAGE_EMBED_LINKS, Permission.MESSAGE_MANAGE, Permission.MESSAGE_HISTORY, Permission.MESSAGE_READ, Permission.MESSAGE_WRITE, Permission.MESSAGE_ATTACH_FILES).collect(Collectors.toList());
    // create channel and get ID
    String cId;
    try {
        Guild guild = jda.getGuildById(gId);
        cId = guild.getController().createTextChannel(optional != null ? optional : "new_schedule").addPermissionOverride(// allow self permissions
        guild.getMember(jda.getSelfUser()), channelPerms, new ArrayList<>()).addPermissionOverride(// disable @everyone message write
        guild.getPublicRole(), // disable @everyone message write
        new ArrayList<>(), Collections.singletonList(Permission.MESSAGE_WRITE)).complete().getId();
    } catch (PermissionException e) {
        String m = e.getMessage() + ": Guild ID " + gId;
        Logging.warn(this.getClass(), m);
        return;
    } catch (Exception e) {
        Logging.exception(this.getClass(), e);
        return;
    }
    // set default reminders
    List<Integer> default_reminders = new ArrayList<>();
    default_reminders.add(10);
    // set default rsvp options
    Map<String, String> default_rsvp = new LinkedHashMap<>();
    default_rsvp.put(Main.getBotSettingsManager().getYesEmoji(), "Yes");
    default_rsvp.put(Main.getBotSettingsManager().getNoEmoji(), "No");
    default_rsvp.put(Main.getBotSettingsManager().getClearEmoji(), "Undecided");
    // create DB document for new schedule
    Document schedule = new Document("_id", cId).append("guildId", gId).append("announcement_channel", Main.getBotSettingsManager().getAnnounceChan()).append("announcement_format", Main.getBotSettingsManager().getAnnounceFormat()).append("clock_format", Main.getBotSettingsManager().getClockFormat()).append("timezone", Main.getBotSettingsManager().getTimeZone()).append("sync_time", Date.from(ZonedDateTime.of(LocalDate.now().plusDays(1), LocalTime.now().truncatedTo(ChronoUnit.HOURS), ZoneId.systemDefault()).toInstant())).append("default_reminders", default_reminders).append("rsvp_enabled", false).append("display_style", "full").append("sync_length", 7).append("sync_address", "off").append("rsvp_options", default_rsvp);
    Main.getDBDriver().getScheduleCollection().insertOne(schedule);
}
Also used : PermissionException(net.dv8tion.jda.core.exceptions.PermissionException) JDA(net.dv8tion.jda.core.JDA) Guild(net.dv8tion.jda.core.entities.Guild) Document(org.bson.Document) PermissionException(net.dv8tion.jda.core.exceptions.PermissionException) Permission(net.dv8tion.jda.core.Permission)

Example 33 with JDA

use of net.dv8tion.jda.core.JDA in project Saber-Bot by notem.

the class ScheduleManager method createSchedule.

/**
 * Convert a pre-existing discord channel to a new saber schedule channel
 * @param channel (TextChannel) a pre-existing channel to convert to a schedule
 */
public void createSchedule(TextChannel channel) {
    JDA jda = Main.getShardManager().getJDA(channel.getGuild().getId());
    // bot self permissions
    Collection<Permission> channelPerms = Stream.of(Permission.MESSAGE_ADD_REACTION, Permission.MESSAGE_EMBED_LINKS, Permission.MESSAGE_MANAGE, Permission.MESSAGE_HISTORY, Permission.MESSAGE_READ, Permission.MESSAGE_WRITE, Permission.MESSAGE_ATTACH_FILES).collect(Collectors.toList());
    // attempt to set the channel permissions
    try {
        // self perms
        channel.createPermissionOverride(channel.getGuild().getMember(jda.getSelfUser())).setAllow(channelPerms).queue();
        // @everyone perms
        channel.createPermissionOverride(channel.getGuild().getPublicRole()).setDeny(Collections.singleton(Permission.MESSAGE_WRITE)).queue();
    } catch (PermissionException e) {
        String m = e.getMessage() + ": Guild ID " + channel.getGuild().getId();
        Logging.warn(this.getClass(), m);
    } catch (Exception e) {
        Logging.exception(this.getClass(), e);
    }
    // default reminders
    List<Integer> default_reminders = new ArrayList<>();
    default_reminders.add(10);
    // default rsvp options
    Map<String, String> default_rsvp = new LinkedHashMap<>();
    default_rsvp.put(Main.getBotSettingsManager().getYesEmoji(), "Yes");
    default_rsvp.put(Main.getBotSettingsManager().getNoEmoji(), "No");
    default_rsvp.put(Main.getBotSettingsManager().getClearEmoji(), "Undecided");
    // create DB entry
    Document schedule = new Document("_id", channel.getId()).append("guildId", channel.getGuild().getId()).append("announcement_channel", Main.getBotSettingsManager().getAnnounceChan()).append("announcement_format", Main.getBotSettingsManager().getAnnounceFormat()).append("clock_format", Main.getBotSettingsManager().getClockFormat()).append("timezone", Main.getBotSettingsManager().getTimeZone()).append("sync_time", Date.from(ZonedDateTime.of(LocalDate.now().plusDays(1), LocalTime.now().truncatedTo(ChronoUnit.HOURS), ZoneId.systemDefault()).toInstant())).append("default_reminders", default_reminders).append("rsvp_enabled", false).append("display_style", "full").append("sync_length", 7).append("auto_sort", 0).append("sync_address", "off").append("rsvp_options", default_rsvp);
    Main.getDBDriver().getScheduleCollection().insertOne(schedule);
}
Also used : PermissionException(net.dv8tion.jda.core.exceptions.PermissionException) JDA(net.dv8tion.jda.core.JDA) Permission(net.dv8tion.jda.core.Permission) Document(org.bson.Document) PermissionException(net.dv8tion.jda.core.exceptions.PermissionException)

Example 34 with JDA

use of net.dv8tion.jda.core.JDA in project Saber-Bot by notem.

the class ScheduleEntry method spawnRole.

/**
 * generates a temporary RSVP group role for dynamic user mentioning
 * the role will last for some time before being removed
 * @param group the rsvp group
 * @return the newly created Role
 */
public Role spawnRole(String group) {
    List<String> members = this.rsvpMembers.get(group);
    JDA jda = Main.getShardManager().getJDA(this.guildId);
    Guild guild = jda.getGuildById(guildId);
    // create the event RSVP role
    Role role = guild.getController().createRole().setName(group).setMentionable(true).setColor(Color.ORANGE).complete();
    // add all RSVP'ed members to the role
    members.forEach(memberId -> {
        if (memberId.matches("\\d+")) {
            Member member = guild.getMemberById(memberId);
            guild.getController().addSingleRoleToMember(member, role).reason("dynamic RSVP role for event announcement").complete();
        }
    });
    // automatically remove the role after 5 minutes
    role.delete().queueAfter(60 * 5, TimeUnit.SECONDS);
    return role;
}
Also used : JDA(net.dv8tion.jda.core.JDA)

Example 35 with JDA

use of net.dv8tion.jda.core.JDA in project Saber-Bot by notem.

the class ScheduleEntry method announcementsToString.

/**
 * creates string display for event announcements
 */
public String announcementsToString() {
    StringBuilder body = new StringBuilder("// Event Announcements\n");
    JDA jda = Main.getShardManager().getJDA(this.guildId);
    for (String id : this.aTimes.keySet()) {
        TextChannel channel = jda.getTextChannelById(this.aTargets.get(id));
        body.append("(").append(Integer.parseInt(id) + 1).append(") \"").append(this.aMessages.get(id)).append("\"").append(" at \"").append(this.aTimes.get(id)).append("\"").append(" to \"#").append(channel == null ? "unknown_channel" : channel.getName()).append("\"\n");
    }
    return body.toString();
}
Also used : JDA(net.dv8tion.jda.core.JDA)

Aggregations

JDA (net.dv8tion.jda.core.JDA)42 Guild (net.dv8tion.jda.core.entities.Guild)10 TextChannel (net.dv8tion.jda.core.entities.TextChannel)9 Document (org.bson.Document)9 Permission (net.dv8tion.jda.core.Permission)7 Main (ws.nmathe.saber.Main)7 Consumer (java.util.function.Consumer)6 Message (net.dv8tion.jda.core.entities.Message)6 JSONObject (org.json.JSONObject)5 Logging (ws.nmathe.saber.utils.Logging)5 ArrayList (java.util.ArrayList)4 Collections (java.util.Collections)4 Date (java.util.Date)4 List (java.util.List)4 Collectors (java.util.stream.Collectors)4 Member (net.dv8tion.jda.core.entities.Member)4 Bson (org.bson.conversions.Bson)4 EmbedBuilder (net.dv8tion.jda.core.EmbedBuilder)3 User (net.dv8tion.jda.core.entities.User)3 PermissionException (net.dv8tion.jda.core.exceptions.PermissionException)3