Search in sources :

Example 36 with IMessage

use of sx.blah.discord.handle.obj.IMessage in project DiscordSailv2 by Vaerys-Dawn.

the class TopUserForRole method execute.

@Override
public String execute(String args, CommandObject command) {
    // init index value
    int index = 1;
    // try to get role.
    IRole role = GuildHandler.getRoleFromName(args, command.guild.get());
    if (role == null) {
        try {
            // if role get fails, try again, but this time assume the first "word" is the rank the user wants to get.
            index = Integer.parseInt(new SplitFirstObject(args).getFirstWord());
        } catch (NumberFormatException e) {
            // not a valid number, can't find role, bork.
            return "> Invalid Role";
        }
        // remove index from string, try to get role again.
        args = new SplitFirstObject(args).getRest();
        role = GuildHandler.getRoleFromName(args, command.guild.get());
        if (role == null)
            return "> Invalid Role.";
    }
    IMessage working = RequestHandler.sendMessage("`Working...`", command.channel.get()).get();
    // populate list with users with role defined.
    List<Long> userIDs = command.guild.get().getUsersByRole(role).stream().map(IUser::getLongID).collect(Collectors.toList());
    if (userIDs.isEmpty()) {
        RequestHandler.deleteMessage(working);
        return "> Could not find any users with that role!";
    }
    userIDs.removeIf(f -> PixelHandler.rank(command.guild.users, command.guild.get(), f) == -1);
    userIDs.sort((o1, o2) -> {
        long rank1 = PixelHandler.rank(command.guild.users, command.guild.get(), o1);
        long rank2 = PixelHandler.rank(command.guild.users, command.guild.get(), o2);
        return Long.compare(rank1, rank2);
    });
    if (userIDs.size() == 0)
        return "> Could not find any ranked users with that role.";
    if (index > userIDs.size()) {
        RequestHandler.deleteMessage(working);
        return "> There's only " + userIDs.size() + (userIDs.size() == 1 ? " user" : " users") + " with that role.";
    }
    if (index == 1) {
        // show an embed with the top (at most) 5 users instead.
        RequestHandler.deleteMessage(working);
        getEmbed(command, role, userIDs);
        return null;
    }
    ProfileObject topUserProfile = command.guild.users.getUserByID(userIDs.get(index - 1));
    UserObject topUser = topUserProfile.getUser(command.guild);
    NumberFormat nf = NumberFormat.getInstance();
    RequestHandler.deleteMessage(working);
    return "> @" + topUser.username + ", **Pixels:** " + nf.format(topUserProfile.getXP()) + ", **Level:** " + topUserProfile.getCurrentLevel() + ", **User ID:** " + topUser.longID;
}
Also used : IRole(sx.blah.discord.handle.obj.IRole) IMessage(sx.blah.discord.handle.obj.IMessage) UserObject(com.github.vaerys.masterobjects.UserObject) SplitFirstObject(com.github.vaerys.objects.SplitFirstObject) ProfileObject(com.github.vaerys.objects.ProfileObject) NumberFormat(java.text.NumberFormat)

Example 37 with IMessage

use of sx.blah.discord.handle.obj.IMessage in project DiscordSailv2 by Vaerys-Dawn.

the class TransferLevels method execute.

@Override
public String execute(String args, CommandObject command) {
    if (command.guild.config.xpGain) {
        return "> Cannot transfer levels, xp gain enabled.";
    }
    if (command.guild.config.getRewardRoles().size() == 0) {
        return "> No rewards available to grant. cannot transfer levels";
    }
    IMessage message = RequestHandler.sendMessage("`Working...`", command.channel.get()).get();
    Utility.sortRewards(command.guild.config.getRewardRoles());
    for (IUser user : command.guild.getUsers()) {
        if (!user.isBot()) {
            ProfileObject uObject = command.guild.users.getUserByID(user.getLongID());
            if (uObject == null) {
                uObject = command.guild.users.addUser(user.getLongID());
            }
            uObject.lastTalked = ZonedDateTime.now(ZoneOffset.UTC).toEpochSecond();
            uObject.setXp(0);
            uObject.setCurrentLevel(-1);
            for (RewardRoleObject r : command.guild.config.getRewardRoles()) {
                if (user.getRolesForGuild(command.guild.get()).contains(r.get(command.guild))) {
                    uObject.setXp(r.getXp());
                    uObject.setCurrentLevel(r.getLevel());
                }
            }
            GuildHandler.checkUsersRoles(uObject.getUserID(), command.guild);
        }
    }
    RequestHandler.deleteMessage(message);
    command.guild.config.xpGain = true;
    return "> Transfer Complete.";
}
Also used : RewardRoleObject(com.github.vaerys.objects.RewardRoleObject) IMessage(sx.blah.discord.handle.obj.IMessage) IUser(sx.blah.discord.handle.obj.IUser) ProfileObject(com.github.vaerys.objects.ProfileObject)

Example 38 with IMessage

use of sx.blah.discord.handle.obj.IMessage in project DiscordSailv2 by Vaerys-Dawn.

the class ArtHandler method pinMessage.

public static void pinMessage(CommandObject command, UserObject reacted, UserObject owner) {
    IChannel channelIDS = command.guild.getChannelByType(ChannelSetting.ART);
    List<TrackLikes> likes = command.guild.channelData.getLikes();
    List<Long> pins = command.guild.channelData.getPinnedMessages();
    // exit if not pinning art
    if (!command.guild.config.artPinning)
        return;
    // exit if the art is already pinned
    if (command.message.get().isPinned())
        return;
    // exit if message owner is a bot
    if (owner.get().isBot())
        return;
    // exit if message has already been unpinned.
    IReaction reaction = command.message.getReationByName(Constants.EMOJI_REMOVE_PIN);
    if (reaction != null && reaction.getUserReacted(command.client.bot.get())) {
        RequestBuffer.request(() -> command.message.get().removeReaction(reacted.get(), Utility.getReaction(Constants.EMOJI_ADD_PIN))).get();
        return;
    }
    // exit if user has art pinning denied.
    ProfileObject profile = reacted.getProfile(command.guild);
    if (profile != null && profile.getSettings().contains(UserSetting.DENY_ART_PINNING))
        return;
    // exit if there is no art channel
    if (channelIDS == null)
        return;
    // exit if this is not the art channel
    if (channelIDS.getLongID() != command.channel.longID)
        return;
    // exit if there is no art to be found
    if (!checkAttachments(command) && !checkMessage(command))
        return;
    try {
        // pin message
        RequestBuffer.request(() -> command.channel.get().pin(command.message.get())).get();
        // debug builder
        String name;
        if (checkAttachments(command)) {
            name = "ATTACHMENT_PIN";
        } else {
            name = "MESSAGE_PIN";
        }
        String args;
        args = command.message.getContent();
        for (IMessage.Attachment a : command.message.getAttachments()) {
            args += " <" + a.getUrl() + ">";
        }
        if (command.message.getContent() == null || command.message.getContent().isEmpty()) {
            args = args.replace("  ", "");
        }
        command.guild.sendDebugLog(command, "ART_PINNED", name, args);
        // end debug
        // add to ping
        pins.add(command.message.longID);
        // add pin response
        RequestBuffer.request(() -> command.message.get().addReaction(Utility.getReaction(Constants.EMOJI_ADD_PIN)));
        if (command.guild.config.likeArt && command.guild.config.modulePixels) {
            // add heart
            RequestBuffer.request(() -> command.message.get().addReaction(Utility.getReaction(Constants.EMOJI_LIKE_PIN)));
            // add to list
            likes.add(new TrackLikes(command.message.longID));
        }
        String response;
        if (!command.guild.config.autoArtPinning) {
            if (owner.longID == reacted.longID) {
                response = "> **" + reacted.displayName + "** Has pinned their";
            } else {
                response = "> **" + reacted.displayName + "** Has pinned **" + owner.displayName + "'s**";
            }
            response += " art by reacting with the \uD83D\uDCCC emoji.";
        } else {
            response = "> I have pinned **" + owner.displayName + "'s** art.";
        }
        if (command.guild.config.likeArt && command.guild.config.modulePixels) {
            response += "\nYou can now react with a \u2764 emoji to give the user some pixels.";
        }
        IMessage pinResponse = RequestHandler.sendMessage(response, command.channel).get();
        Thread thread = new Thread(() -> {
            try {
                logger.trace("Deleting in 2 minutes.");
                Thread.sleep(2 * 60 * 1000);
                RequestHandler.deleteMessage(pinResponse);
            } catch (InterruptedException e) {
            // do nothing
            }
        });
        checkList(command);
        thread.start();
        return;
    } catch (DiscordException e) {
        if (e.getErrorMessage().contains("already pinned")) {
            return;
        } else {
            Utility.sendStack(e);
        }
    }
}
Also used : IChannel(sx.blah.discord.handle.obj.IChannel) IMessage(sx.blah.discord.handle.obj.IMessage) ProfileObject(com.github.vaerys.objects.ProfileObject) TrackLikes(com.github.vaerys.objects.TrackLikes) DiscordException(sx.blah.discord.util.DiscordException) IReaction(sx.blah.discord.handle.obj.IReaction)

Example 39 with IMessage

use of sx.blah.discord.handle.obj.IMessage in project DiscordSailv2 by Vaerys-Dawn.

the class ArtHandler method checkList.

private static void checkList(CommandObject command) {
    List<Long> pinnedMessages = command.guild.channelData.getPinnedMessages();
    List<TrackLikes> likes = command.guild.channelData.getLikes();
    List<IMessage> channelpins = RequestBuffer.request(() -> command.channel.get().getPinnedMessages()).get();
    List<IMessage> markedForUnpin = new LinkedList<>();
    int pinLimit = command.guild.config.pinLimit;
    int tries = 0;
    ListIterator iterator = pinnedMessages.listIterator();
    List<Long> channelPinsLong = channelpins.stream().map(iMessage -> iMessage.getLongID()).collect(Collectors.toList());
    try {
        while (iterator.hasNext()) {
            long item = (long) iterator.next();
            if (!channelPinsLong.contains(item)) {
                iterator.remove();
            }
        }
    } catch (ConcurrentModificationException e) {
        return;
    }
    while (pinnedMessages.size() > pinLimit && tries < 50) {
        for (IMessage p : channelpins) {
            if (pinnedMessages.contains(p.getLongID()) && pinnedMessages.get(0) == p.getLongID()) {
                // adds the pin to the messages to be unpinned
                markedForUnpin.add(p);
                removePin(p, pinnedMessages);
            }
        }
        tries++;
    }
    tries = 0;
    for (IMessage message : markedForUnpin) {
        try {
            if (message.isPinned()) {
                RequestBuffer.request(() -> command.channel.get().unpin(message)).get();
                command.guild.sendDebugLog(command.setMessage(message), "ART_PINNED", "UNPIN", "PIN TOTAL = " + command.channel.getPinCount() + "/" + command.guild.config.pinLimit);
            }
        } catch (DiscordException e) {
            if (!e.getMessage().contains("Message is not pinned!")) {
                throw (e);
            }
        }
    }
    tries++;
    iterator = likes.listIterator();
    while (iterator.hasNext()) {
        TrackLikes like = (TrackLikes) iterator.next();
        if (!pinnedMessages.contains(like.getMessageID())) {
            iterator.remove();
        }
    }
}
Also used : RequestBuffer(sx.blah.discord.util.RequestBuffer) java.util(java.util) CommandObject(com.github.vaerys.commands.CommandObject) Logger(org.slf4j.Logger) TrackLikes(com.github.vaerys.objects.TrackLikes) ChannelSetting(com.github.vaerys.enums.ChannelSetting) LoggerFactory(org.slf4j.LoggerFactory) UserSetting(com.github.vaerys.enums.UserSetting) Constants(com.github.vaerys.main.Constants) Collectors(java.util.stream.Collectors) IMessage(sx.blah.discord.handle.obj.IMessage) IUser(sx.blah.discord.handle.obj.IUser) IChannel(sx.blah.discord.handle.obj.IChannel) UserObject(com.github.vaerys.masterobjects.UserObject) ProfileObject(com.github.vaerys.objects.ProfileObject) DiscordException(sx.blah.discord.util.DiscordException) IReaction(sx.blah.discord.handle.obj.IReaction) Utility(com.github.vaerys.main.Utility) IMessage(sx.blah.discord.handle.obj.IMessage) TrackLikes(com.github.vaerys.objects.TrackLikes) DiscordException(sx.blah.discord.util.DiscordException)

Example 40 with IMessage

use of sx.blah.discord.handle.obj.IMessage in project DiscordSailv2 by Vaerys-Dawn.

the class NewDailyMessage method execute.

@Override
public String execute(String args, CommandObject command) {
    try {
        SplitFirstObject day = new SplitFirstObject(args);
        DayOfWeek dayOfWeek = DayOfWeek.valueOf(day.getFirstWord().toUpperCase());
        if (day.getRest() != null) {
            IMessage working = RequestHandler.sendMessage("`Working...`", command.channel.get()).get();
            QueueHandler.addToQueue(command, day.getRest(), dayOfWeek, Constants.QUEUE_DAILY);
            RequestHandler.deleteMessage(working);
            return "> Request Sent.";
        } else {
            return Utility.getCommandInfo(this, command);
        }
    } catch (IllegalArgumentException e) {
        return "> Not a valid Day of the week.";
    }
}
Also used : DayOfWeek(java.time.DayOfWeek) IMessage(sx.blah.discord.handle.obj.IMessage) SplitFirstObject(com.github.vaerys.objects.SplitFirstObject)

Aggregations

IMessage (sx.blah.discord.handle.obj.IMessage)46 IUser (sx.blah.discord.handle.obj.IUser)10 EmbedBuilder (sx.blah.discord.util.EmbedBuilder)9 EventSubscriber (sx.blah.discord.api.events.EventSubscriber)6 IChannel (sx.blah.discord.handle.obj.IChannel)6 ProfileObject (com.github.vaerys.objects.ProfileObject)5 UserObject (com.github.vaerys.masterobjects.UserObject)4 ArrayList (java.util.ArrayList)4 DiscordException (sx.blah.discord.util.DiscordException)4 Announcement (com.cloudcraftgaming.discal.api.object.announcement.Announcement)3 PreEvent (com.cloudcraftgaming.discal.api.object.event.PreEvent)3 CommandObject (com.github.vaerys.commands.CommandObject)3 SplitFirstObject (com.github.vaerys.objects.SplitFirstObject)3 GitHubImage (io.github.spugn.Sargo.Utilities.GitHubImage)3 File (java.io.File)3 FileNotFoundException (java.io.FileNotFoundException)3 IOException (java.io.IOException)3 RateLimitException (sx.blah.discord.util.RateLimitException)3 PreCalendar (com.cloudcraftgaming.discal.api.object.calendar.PreCalendar)2 TrackLikes (com.github.vaerys.objects.TrackLikes)2