Search in sources :

Example 16 with JDA

use of net.dv8tion.jda.api.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.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;
    }
    // create the schedule database entry
    createNewSchedule(cId, gId);
}
Also used : PermissionException(net.dv8tion.jda.api.exceptions.PermissionException) JDA(net.dv8tion.jda.api.JDA) Permission(net.dv8tion.jda.api.Permission) Guild(net.dv8tion.jda.api.entities.Guild) PermissionException(net.dv8tion.jda.api.exceptions.PermissionException)

Example 17 with JDA

use of net.dv8tion.jda.api.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);
    }
    // create the schedule database entry
    createNewSchedule(channel.getId(), channel.getGuild().getId());
}
Also used : PermissionException(net.dv8tion.jda.api.exceptions.PermissionException) JDA(net.dv8tion.jda.api.JDA) Permission(net.dv8tion.jda.api.Permission) PermissionException(net.dv8tion.jda.api.exceptions.PermissionException)

Example 18 with JDA

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

the class ScheduleEntry method getMessageObject.

/**
 * Attempts to retrieve the discord Message, if the message does not exist
 * (or the bot can for any other reason cannot retrieve it) the method returns null
 * @return (Message) if exists, otherwise null
 */
public Message getMessageObject() {
    Message msg;
    try {
        JDA jda = Main.getShardManager().isSharding() ? Main.getShardManager().getShard(guildId) : Main.getShardManager().getJDA();
        msg = jda.getTextChannelById(this.chanId).retrieveMessageById(this.msgId).complete();
    } catch (Exception e) {
        msg = null;
    }
    return msg;
}
Also used : JDA(net.dv8tion.jda.api.JDA) PermissionException(net.dv8tion.jda.api.exceptions.PermissionException)

Example 19 with JDA

use of net.dv8tion.jda.api.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.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.addRoleToMember(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.api.JDA)

Example 20 with JDA

use of net.dv8tion.jda.api.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.api.JDA)

Aggregations

JDA (net.dv8tion.jda.api.JDA)24 Document (org.bson.Document)7 Main (ws.nmathe.saber.Main)7 Consumer (java.util.function.Consumer)6 Permission (net.dv8tion.jda.api.Permission)6 PermissionException (net.dv8tion.jda.api.exceptions.PermissionException)6 Logging (ws.nmathe.saber.utils.Logging)5 TextChannel (net.dv8tion.jda.api.entities.TextChannel)4 Bson (org.bson.conversions.Bson)4 Filters (com.mongodb.client.model.Filters)3 Projections.fields (com.mongodb.client.model.Projections.fields)3 Projections.include (com.mongodb.client.model.Projections.include)3 ZonedDateTime (java.time.ZonedDateTime)3 ChronoUnit (java.time.temporal.ChronoUnit)3 java.util (java.util)3 Collectors (java.util.stream.Collectors)3 Guild (net.dv8tion.jda.api.entities.Guild)3 Message (net.dv8tion.jda.api.entities.Message)3 MessageUtilities (ws.nmathe.saber.utils.MessageUtilities)3 Calendar (com.google.api.services.calendar.Calendar)2