Search in sources :

Example 1 with TrackLikes

use of com.github.vaerys.objects.TrackLikes in project DiscordSailv2 by Vaerys-Dawn.

the class ArtHandler method pinLiked.

/**
 * Handles the granting of xp to user's when their art has a :heart: reaction applied to their
 * message that was pinned with the artPinning module.
 *
 * @param command Used to parse in the variables needed to access the guild, channel, message,
 *                and user objects. these objects allows access to the api.
 * @param reacted the user that added a reaction.
 * @param owner   the user that owns the message.
 */
public static void pinLiked(CommandObject command, UserObject reacted, UserObject owner) {
    List<IChannel> channelIDS = command.guild.getChannelsByType(ChannelSetting.ART);
    // exit if not pinning art
    if (!command.guild.config.artPinning)
        return;
    // exit if pixels is off
    if (!command.guild.config.modulePixels)
        return;
    // exit if xp gain is off
    if (!command.guild.config.xpGain)
        return;
    // exit if liking art is off
    if (!command.guild.config.likeArt)
        return;
    // exit if message owner is a bot
    if (owner.get().isBot())
        return;
    // exit if there is no art channel
    if (channelIDS.size() == 0)
        return;
    // exit if this is not the art channel
    if (channelIDS.get(0).getLongID() != command.channel.longID)
        return;
    // you cant give yourself pixels via your own art
    if (owner.longID == reacted.longID)
        return;
    // exit if not pinned art
    if (!command.guild.channelData.getPinnedMessages().contains(command.message.longID))
        return;
    TrackLikes messageLikes = command.guild.channelData.getLiked(command.message.longID);
    // some thing weird happened if this is null unless its a legacy pin
    if (messageLikes == null)
        return;
    // cant like the same thing more than once
    if (messageLikes.getUsers().contains(reacted.longID))
        return;
    ProfileObject profile = owner.getProfile(command.guild);
    // exit if profile doesn't exist
    if (profile == null)
        return;
    // exit if the user should not gain pixels
    if (profile.getSettings().contains(UserSetting.NO_XP_GAIN) || profile.getSettings().contains(UserSetting.DENIED_XP))
        return;
    logger.trace(reacted.displayName + " just gave " + owner.displayName + " some pixels for liking their art.");
    // grant user xp for their nice art.
    profile.addXP(5, command.guild.config);
    messageLikes.getUsers().add(reacted.longID);
}
Also used : IChannel(sx.blah.discord.handle.obj.IChannel) TrackLikes(com.github.vaerys.objects.TrackLikes) ProfileObject(com.github.vaerys.objects.ProfileObject)

Example 2 with TrackLikes

use of com.github.vaerys.objects.TrackLikes 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 3 with TrackLikes

use of com.github.vaerys.objects.TrackLikes 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)

Aggregations

ProfileObject (com.github.vaerys.objects.ProfileObject)3 TrackLikes (com.github.vaerys.objects.TrackLikes)3 IChannel (sx.blah.discord.handle.obj.IChannel)3 IMessage (sx.blah.discord.handle.obj.IMessage)2 IReaction (sx.blah.discord.handle.obj.IReaction)2 DiscordException (sx.blah.discord.util.DiscordException)2 CommandObject (com.github.vaerys.commands.CommandObject)1 ChannelSetting (com.github.vaerys.enums.ChannelSetting)1 UserSetting (com.github.vaerys.enums.UserSetting)1 Constants (com.github.vaerys.main.Constants)1 Utility (com.github.vaerys.main.Utility)1 UserObject (com.github.vaerys.masterobjects.UserObject)1 java.util (java.util)1 Collectors (java.util.stream.Collectors)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1 IUser (sx.blah.discord.handle.obj.IUser)1 RequestBuffer (sx.blah.discord.util.RequestBuffer)1