Search in sources :

Example 36 with JDA

use of net.dv8tion.jda.core.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).getMessageById(this.msgId).complete();
    } catch (Exception e) {
        msg = null;
    }
    return msg;
}
Also used : JDA(net.dv8tion.jda.core.JDA)

Example 37 with JDA

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

the class EventListener method onGuildJoin.

@Override
public void onGuildJoin(GuildJoinEvent event) {
    // leave guild if guild is blacklisted
    if (Main.getBotSettingsManager().getBlackList().contains(event.getGuild().getId())) {
        event.getGuild().leave().queue();
        return;
    }
    // identify which shard is responsible for the guild
    String guildId = event.getGuild().getId();
    JDA jda = Main.getShardManager().isSharding() ? Main.getShardManager().getShard(guildId) : Main.getShardManager().getJDA();
    // send welcome message to the server owner
    String welcomeMessage = "```diff\n- Joined```\n" + "**" + jda.getSelfUser().getName() + "**, a calendar bot, has been added to the guild you own, '" + event.getGuild().getName() + "'." + "\n\n" + "If this is your first time using the bot, you will need to create a new channel in your guild named" + " **" + Main.getBotSettingsManager().getControlChan() + "** to control the bot.\n" + "The bot will not listen to commands in any other channel!" + "\n\n" + "If you have not yet reviewed the **Quickstart** guide (as seen on the bots.discord.pw listing), " + "it may be found here: https://bots.discord.pw/bots/250801603630596100";
    MessageUtilities.sendPrivateMsg(welcomeMessage, event.getGuild().getOwner().getUser(), null);
    // update web stats
    JDA.ShardInfo info = event.getJDA().getShardInfo();
    HttpUtilities.updateStats(info == null ? null : info.getShardId());
}
Also used : JDA(net.dv8tion.jda.core.JDA)

Example 38 with JDA

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

the class Pruner method run.

@Override
public void run() {
    Logging.info(this.getClass(), "Running database pruner. . .");
    // purge guild setting entries for any guild not connected to the bot
    Bson query = new Document();
    Main.getDBDriver().getGuildCollection().find(query).projection(fields(include("_id"))).forEach((Consumer<? super Document>) document -> {
        try {
            String guildId = document.getString("_id");
            JDA jda = Main.getShardManager().getShard(guildId);
            if (jda == null)
                return;
            if (JDA.Status.valueOf("CONNECTED") != jda.getStatus())
                return;
            Guild guild = jda.getGuildById(guildId);
            if (guild == null) {
                Main.getDBDriver().getGuildCollection().deleteOne(eq("_id", guildId));
                Main.getDBDriver().getEventCollection().deleteMany(eq("guildId", guildId));
                Main.getDBDriver().getScheduleCollection().deleteMany(eq("guildId", guildId));
                Logging.info(this.getClass(), "Pruned guild with ID: " + guildId);
            }
        } catch (Exception e) {
            Logging.exception(this.getClass(), e);
        }
    });
    // purge schedule entries that the bot cannot connect to
    query = new Document();
    Main.getDBDriver().getScheduleCollection().find(query).projection(fields(include("_id", "guildId"))).forEach((Consumer<? super Document>) document -> {
        try {
            String guildId = document.getString("guildId");
            JDA jda = Main.getShardManager().getShard(guildId);
            if (jda == null)
                return;
            if (JDA.Status.valueOf("CONNECTED") != jda.getStatus())
                return;
            String chanId = document.getString("_id");
            MessageChannel channel = jda.getTextChannelById(chanId);
            if (channel == null) {
                Main.getDBDriver().getEventCollection().deleteMany(eq("channeldId", chanId));
                Main.getDBDriver().getScheduleCollection().deleteMany(eq("_id", chanId));
                Logging.info(this.getClass(), "Pruned schedule with channel ID: " + chanId);
            }
        } catch (Exception e) {
            Logging.exception(this.getClass(), e);
        }
    });
    // purge events for which the bot cannot access the message
    query = new Document();
    Main.getDBDriver().getEventCollection().find(query).projection(fields(include("_id", "messageId", "channelId", "guildId"))).forEach((Consumer<? super Document>) document -> {
        try {
            String guildId = document.getString("guildId");
            JDA jda = Main.getShardManager().getShard(guildId);
            if (jda == null)
                return;
            if (JDA.Status.valueOf("CONNECTED") != jda.getStatus())
                return;
            Integer eventId = document.getInteger("_id");
            String messageId = document.getString("messageId");
            if (messageId == null) {
                Main.getDBDriver().getEventCollection().deleteOne(eq("_id", eventId));
                Logging.info(this.getClass(), "Pruned event with ID: " + eventId);
                return;
            }
            String channelId = document.getString("channelId");
            MessageChannel channel = jda.getTextChannelById(channelId);
            if (channel == null) {
                Main.getDBDriver().getEventCollection().deleteOne(eq("_id", eventId));
                Logging.info(this.getClass(), "Pruned event with ID: " + eventId);
                return;
            }
            channel.getMessageById(messageId).queue(message -> {
                if (message == null) {
                    Main.getDBDriver().getEventCollection().deleteOne(eq("_id", eventId));
                    Logging.info(this.getClass(), "Pruned event with ID: " + eventId + " on channel with ID: " + channelId);
                }
            }, throwable -> {
                Main.getDBDriver().getEventCollection().deleteOne(eq("_id", eventId));
                Logging.info(this.getClass(), "Pruned event with ID: " + eventId + " on channel with ID: " + channelId);
            });
        } catch (Exception e) {
            Logging.exception(this.getClass(), e);
        }
    });
}
Also used : Document(org.bson.Document) Projections.fields(com.mongodb.client.model.Projections.fields) Logging(ws.nmathe.saber.utils.Logging) MessageChannel(net.dv8tion.jda.core.entities.MessageChannel) Projections.include(com.mongodb.client.model.Projections.include) Bson(org.bson.conversions.Bson) Consumer(java.util.function.Consumer) Guild(net.dv8tion.jda.core.entities.Guild) Filters.and(com.mongodb.client.model.Filters.and) JDA(net.dv8tion.jda.core.JDA) Filters.eq(com.mongodb.client.model.Filters.eq) Main(ws.nmathe.saber.Main) Filters.where(com.mongodb.client.model.Filters.where) MessageChannel(net.dv8tion.jda.core.entities.MessageChannel) JDA(net.dv8tion.jda.core.JDA) Document(org.bson.Document) Guild(net.dv8tion.jda.core.entities.Guild) Bson(org.bson.conversions.Bson)

Example 39 with JDA

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

the class ParsingUtilities method compileUserList.

/**
 * generates a list of user IDs for a given RSVP category of an event
 * @param entry ScheduleEntry object
 * @param name name of RSVP category
 * @return List of Stings or null if category is invalid
 */
private static List<String> compileUserList(ScheduleEntry entry, String name) {
    List<String> users = null;
    if (name.toLowerCase().equals("no-input")) {
        List<String> rsvped = new ArrayList<>();
        Set<String> keys = entry.getRsvpMembers().keySet();
        for (String key : keys) {
            rsvped.addAll(entry.getRsvpMembersOfType(key));
        }
        JDA shard = Main.getShardManager().getJDA(entry.getGuildId());
        Guild guild = shard.getGuildById(entry.getGuildId());
        Channel channel = shard.getTextChannelById(entry.getChannelId());
        users = guild.getMembers().stream().filter(member -> member.getPermissions(channel).contains(Permission.MESSAGE_READ)).map(member -> member.getUser().getId()).filter(memberId -> !rsvped.contains(memberId)).collect(Collectors.toList());
    } else {
        List<String> members = entry.getRsvpMembers().get(name);
        if (members != null)
            users = entry.getRsvpMembersOfType(name);
    }
    return users;
}
Also used : java.util(java.util) net.dv8tion.jda.core.entities(net.dv8tion.jda.core.entities) ZonedDateTime(java.time.ZonedDateTime) StringUtils(org.apache.commons.lang3.StringUtils) ScheduleEntry(ws.nmathe.saber.core.schedule.ScheduleEntry) Collectors(java.util.stream.Collectors) ByteBuffer(java.nio.ByteBuffer) ZoneId(java.time.ZoneId) ChronoUnit(java.time.temporal.ChronoUnit) Matcher(java.util.regex.Matcher) Permission(net.dv8tion.jda.core.Permission) LocalDate(java.time.LocalDate) DateTimeFormatter(java.time.format.DateTimeFormatter) LocalTime(java.time.LocalTime) JDA(net.dv8tion.jda.core.JDA) Main(ws.nmathe.saber.Main) Pattern(java.util.regex.Pattern) JDA(net.dv8tion.jda.core.JDA)

Example 40 with JDA

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

the class VerifyUtilities method verifyEmoji.

/**
 * verify that a supplied emoji string is a valid discord emoji
 * @param emoji emoji string (either raw emoji char or discord emoji ID)
 * @return true if valid
 */
public static boolean verifyEmoji(String emoji) {
    if (!EmojiManager.isEmoji(emoji)) {
        String emoteId = emoji.replaceAll("[^\\d]", "");
        Emote emote = null;
        try {
            for (JDA jda : Main.getShardManager().getShards()) {
                emote = jda.getEmoteById(emoteId);
                if (emote != null)
                    break;
            }
        } catch (Exception e) {
            return false;
        }
        if (emote == null) {
            return false;
        }
    }
    return true;
}
Also used : JDA(net.dv8tion.jda.core.JDA) Emote(net.dv8tion.jda.core.entities.Emote)

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