Search in sources :

Example 1 with DailyMessage

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

the class QueueHandler method reactionAdded.

public static void reactionAdded(CommandObject object, IReaction reaction) {
    ReactionEmoji thumbsUp = Utility.getReaction(Constants.EMOJI_APPROVE);
    ReactionEmoji thumbsDown = Utility.getReaction(Constants.EMOJI_DISAPPROVE);
    ReactionEmoji ok = Utility.getReaction("white_check_mark");
    ReactionEmoji no = Utility.getReaction(Constants.EMOJI_REMOVE_PIN);
    ArrayList<QueueObject> queuedMessages = Globals.getDailyMessages().getQueue();
    IMessage message = object.message.get();
    IUser owner = object.client.creator.get();
    // exit if not the queue channel
    if (object.channel.longID != Globals.queueChannelID) {
        return;
    }
    // exit if not the owner.
    if (reaction.getCount() == 0) {
        return;
    }
    if (!reaction.getUserReacted(owner)) {
        return;
    }
    // exit if no embeds
    if (message.getEmbeds().size() == 0) {
        return;
    }
    // exit if not enough reactions
    if (message.getReactions().size() <= 1) {
        return;
    }
    checkQueue();
    for (QueueObject q : queuedMessages) {
        if (q.getMessageId() == message.getLongID()) {
            // getToggles the embed
            IEmbed embed = message.getEmbeds().get(0);
            RequestBuffer.request(() -> message.removeAllReactions()).get();
            switch(q.getType()) {
                // do if daily request
                case Constants.QUEUE_DAILY:
                    try {
                        // getToggles the data
                        long userID = Long.parseLong(embed.getFooter().getText());
                        long uID = -1;
                        DayOfWeek day = null;
                        for (IEmbed.IEmbedField f : embed.getEmbedFields()) {
                            if (f.getName().equalsIgnoreCase(dowString)) {
                                day = DayOfWeek.valueOf(f.getValue());
                            } else if (f.getName().equalsIgnoreCase(uIDString)) {
                                uID = Long.parseLong(f.getValue());
                            }
                        }
                        UserObject user = new UserObject(object.client.getUserByID(userID), null);
                        // do if accepted
                        if (reaction.getEmoji().equals(thumbsUp)) {
                            user.sendDm("> A daily message you sent was approved. **[" + uID + "]**");
                            Globals.getDailyMessages().getMessages().add(new DailyMessage(embed.getDescription(), day, userID, uID));
                            RequestBuffer.request(() -> message.addReaction(ok)).get();
                            q.toggleMarkedForRemoval();
                            return;
                        // do if denied
                        } else if (reaction.getEmoji().equals(thumbsDown)) {
                            user.sendDm("> A daily message you sent was denied. **[" + uID + "]**");
                            q.toggleMarkedForRemoval();
                            RequestBuffer.request(() -> message.addReaction(no)).get();
                            return;
                        } else {
                            return;
                        }
                    } catch (NumberFormatException e) {
                        q.toggleMarkedForRemoval();
                    } catch (IllegalArgumentException e) {
                        q.toggleMarkedForRemoval();
                    }
                default:
                    return;
            }
        }
    }
}
Also used : QueueObject(com.github.vaerys.objects.QueueObject) DayOfWeek(java.time.DayOfWeek) UserObject(com.github.vaerys.masterobjects.UserObject) DailyMessage(com.github.vaerys.objects.DailyMessage) ReactionEmoji(sx.blah.discord.handle.impl.obj.ReactionEmoji)

Example 2 with DailyMessage

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

the class PatchHandler method fixMultipleDailies.

private static void fixMultipleDailies() {
    String path = Constants.DIRECTORY_STORAGE + DailyMessages.FILE_PATH;
    // check file
    if (!FileHandler.exists(path))
        return;
    JsonObject json = FileHandler.fileToJsonObject(path);
    if (checkPatch(1.2, null, "Fix_Multiple_Daily_Messages", json))
        return;
    DailyMessages messages = (DailyMessages) DailyMessages.create(DailyMessages.FILE_PATH, new DailyMessages());
    ArrayList<DailyMessage> dailyMessages = new ArrayList<>();
    for (DailyMessage message : messages.getMessages()) {
        boolean foundMessage = false;
        for (DailyMessage m : dailyMessages) {
            if (m.getUID() == message.getUID()) {
                foundMessage = true;
            }
        }
        if (!foundMessage) {
            dailyMessages.add(message);
        }
    }
    messages.setMessages(dailyMessages);
    messages.flushFile();
    json = FileHandler.fileToJsonObject(path);
    newPatchID(1.2, json);
    FileHandler.writeToJson(path, json);
}
Also used : ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) DailyMessage(com.github.vaerys.objects.DailyMessage)

Example 3 with DailyMessage

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

the class DailyMessages method newDailyMsgUID.

public long newDailyMsgUID() {
    long result;
    Random random = new Random();
    List<Long> uIDs = dailyMessages.stream().filter(dailyMessage -> dailyMessage.getUID() != -1).map(dailyMessage -> dailyMessage.getUID()).collect(Collectors.toList());
    uIDs.addAll(queuedRequests.stream().map(queueObject -> queueObject.getuID()).collect(Collectors.toList()));
    if (uIDs.size() >= 8999) {
        return -1;
    }
    // for (DailyMessage d : dailyMessages) {
    // if (d.getUID() != -1) {
    // uIDs.add(d.getUID());
    // }
    // }
    // for (QueueObject o : queuedRequests) {
    // uIDs.add(o.getuID());
    // }
    result = random.nextInt(9000) + 1000;
    while (uIDs.contains(result)) {
        result = random.nextInt(9000) + 1000;
    }
    return result;
}
Also used : DailyMessage(com.github.vaerys.objects.DailyMessage) QueueObject(com.github.vaerys.objects.QueueObject) List(java.util.List) CommandObject(com.github.vaerys.commands.CommandObject) Globals(com.github.vaerys.main.Globals) GlobalFile(com.github.vaerys.templates.GlobalFile) DayOfWeek(java.time.DayOfWeek) ZonedDateTime(java.time.ZonedDateTime) Random(java.util.Random) ZoneOffset(java.time.ZoneOffset) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) Random(java.util.Random)

Example 4 with DailyMessage

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

the class DailyMessages method getMessageByUID.

public DailyMessage getMessageByUID(long dailyMessageID, CommandObject command) {
    DayOfWeek today = ZonedDateTime.now(ZoneOffset.UTC).getDayOfWeek();
    DailyMessage configMessage = null;
    for (DailyMessage d : Globals.configDailyMessages) {
        if (d.getDay() == today) {
            configMessage = d;
        }
    }
    DailyMessage object = new DailyMessage(configMessage.getContents(command), today, Globals.client.getOurUser().getLongID(), 0000);
    for (DailyMessage d : dailyMessages) {
        if (d.getUID() == dailyMessageID) {
            return d;
        }
    }
    return object;
}
Also used : DayOfWeek(java.time.DayOfWeek) DailyMessage(com.github.vaerys.objects.DailyMessage)

Example 5 with DailyMessage

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

the class DailyMsg method execute.

@Override
public String execute(String args, CommandObject command) {
    SplitFirstObject uIDString = new SplitFirstObject(args);
    try {
        long uID = Long.parseLong(uIDString.getFirstWord());
        DailyMessage messageObject = null;
        for (DailyMessage u : Globals.getDailyMessages().getMessages()) {
            if (u.getUID() == uID) {
                messageObject = u;
            }
        }
        if (messageObject == null) {
            return "> Could not find daily message with that UID.";
        }
        SplitFirstObject mode = null;
        String totest;
        if (uIDString.getRest() == null) {
            totest = "empty";
        } else {
            mode = new SplitFirstObject(uIDString.getRest());
            totest = mode.getFirstWord();
        }
        switch(totest.toLowerCase()) {
            case "edit":
                if (mode.getRest() == null) {
                    return "> Missing args";
                }
                messageObject.setContents(mode.getRest());
                return "> Daily Message contents updated";
            case "delete":
                ListIterator iterator = Globals.getDailyMessages().getMessages().listIterator();
                while (iterator.hasNext()) {
                    DailyMessage object = (DailyMessage) iterator.next();
                    if (object.getUID() == uID) {
                        iterator.remove();
                    }
                }
                return "> Daily Message Deleted";
            case "moveday":
                if (mode.getRest() == null) {
                    return "> Missing args";
                }
                String day = mode.getRest().toUpperCase();
                try {
                    DayOfWeek dayOfWeek = DayOfWeek.valueOf(day);
                    messageObject.setDay(dayOfWeek);
                    return "> Moved message to " + dayOfWeek + ".";
                } catch (IllegalArgumentException e) {
                    return "> Not a valid day of the week.";
                }
            case "info":
                RequestHandler.sendEmbedMessage("", getInfo(messageObject, command), command.channel.get());
                return null;
            default:
                RequestHandler.sendEmbedMessage("", getInfo(messageObject, command), command.channel.get());
                return null;
        }
    } catch (NumberFormatException e) {
        return "> Invalid UID.";
    }
}
Also used : DayOfWeek(java.time.DayOfWeek) DailyMessage(com.github.vaerys.objects.DailyMessage) ListIterator(java.util.ListIterator) SplitFirstObject(com.github.vaerys.objects.SplitFirstObject)

Aggregations

DailyMessage (com.github.vaerys.objects.DailyMessage)7 DayOfWeek (java.time.DayOfWeek)5 ArrayList (java.util.ArrayList)3 QueueObject (com.github.vaerys.objects.QueueObject)2 SplitFirstObject (com.github.vaerys.objects.SplitFirstObject)2 CommandObject (com.github.vaerys.commands.CommandObject)1 Globals (com.github.vaerys.main.Globals)1 UserObject (com.github.vaerys.masterobjects.UserObject)1 GlobalFile (com.github.vaerys.templates.GlobalFile)1 JsonObject (com.google.gson.JsonObject)1 ZoneOffset (java.time.ZoneOffset)1 ZonedDateTime (java.time.ZonedDateTime)1 List (java.util.List)1 ListIterator (java.util.ListIterator)1 Random (java.util.Random)1 Collectors (java.util.stream.Collectors)1 ReactionEmoji (sx.blah.discord.handle.impl.obj.ReactionEmoji)1