Search in sources :

Example 1 with Subscriber

use of irc.Subscriber in project Botnak by Gocnak.

the class ChatPane method onMessage.

/**
     * This is the main message method when somebody sends a message to the channel.
     *
     * @param m The message from the chat.
     */
public void onMessage(MessageWrapper m, boolean showChannel) {
    if (textPane == null)
        return;
    Message message = m.getLocal();
    String sender = message.getSender().toLowerCase();
    String channel = message.getChannel();
    String mess = message.getContent();
    boolean isMe = (message.getType() == Message.MessageType.ACTION_MESSAGE);
    try {
        print(m, "\n" + getTime(), GUIMain.norm);
        User u = Settings.channelManager.getUser(sender, true);
        SimpleAttributeSet user = getUserSet(u);
        if (channel.substring(1).equals(sender)) {
            insertIcon(m, IconEnum.BROADCASTER, null);
        }
        if (u.isOp(channel)) {
            if (!channel.substring(1).equals(sender) && !u.isStaff() && !u.isAdmin() && !u.isGlobalMod()) {
                //not the broadcaster again
                insertIcon(m, IconEnum.MOD, null);
            }
        }
        if (u.isGlobalMod()) {
            insertIcon(m, IconEnum.GLOBAL_MOD, null);
        }
        if (u.isStaff()) {
            insertIcon(m, IconEnum.STAFF, null);
        }
        if (u.isAdmin()) {
            insertIcon(m, IconEnum.ADMIN, null);
        }
        boolean isSubscriber = u.isSubscriber(channel);
        if (isSubscriber) {
            insertIcon(m, IconEnum.SUBSCRIBER, channel);
        } else {
            if (Utils.isMainChannel(channel)) {
                Optional<Subscriber> sub = Settings.subscriberManager.getSubscriber(sender);
                if (sub.isPresent() && !sub.get().isActive()) {
                    insertIcon(m, IconEnum.EX_SUBSCRIBER, channel);
                }
            }
        }
        if (u.isTurbo()) {
            insertIcon(m, IconEnum.TURBO, null);
        }
        if (u.isPrime())
            insertIcon(m, IconEnum.PRIME, null);
        //Cheering
        int cheerTotal = u.getCheer(channel);
        if (cheerTotal > 0) {
            insertIcon(m, Donor.getCheerStatus(cheerTotal), null);
        }
        // Third party donor
        if (Settings.showDonorIcons.getValue()) {
            if (u.isDonor()) {
                insertIcon(m, u.getDonationStatus(), null);
            }
        }
        //name stuff
        print(m, " ", GUIMain.norm);
        SimpleAttributeSet userColor = new SimpleAttributeSet(user);
        FaceManager.handleNameFaces(sender, user);
        if (showChannel) {
            print(m, determineName(u), user);
            print(m, " (" + channel.substring(1) + ")" + (isMe ? " " : ": "), GUIMain.norm);
        } else {
            print(m, determineName(u), user);
            print(m, (!isMe ? ": " : " "), userColor);
        }
        //keyword?
        SimpleAttributeSet set;
        if (Utils.mentionsKeyword(mess)) {
            set = Utils.getSetForKeyword(mess);
        } else {
            set = (isMe ? userColor : GUIMain.norm);
        }
        //URL, Faces, rest of message
        printMessage(m, mess, set, u);
        if (BotnakTrayIcon.shouldDisplayMentions() && !Utils.isTabSelected(index)) {
            if (mess.toLowerCase().contains(Settings.accountManager.getUserAccount().getName().toLowerCase())) {
                GUIMain.getSystemTrayIcon().displayMention(m.getLocal());
            }
        }
        if (Utils.isMainChannel(channel))
            //check status of the sub, has it been a month?
            Settings.subscriberManager.updateSubscriber(u, channel, isSubscriber);
        if (shouldPulse())
            GUIMain.instance.pulseTab(this);
    } catch (Exception e) {
        GUIMain.log(e);
    }
}
Also used : SimpleAttributeSet(javax.swing.text.SimpleAttributeSet) User(lib.pircbot.User) Message(irc.message.Message) Subscriber(irc.Subscriber)

Example 2 with Subscriber

use of irc.Subscriber in project Botnak by Gocnak.

the class SubscriberManager method addNewSubscriber.

public boolean addNewSubscriber(String name, String channel) {
    Optional<Subscriber> subscriber = getSubscriber(name);
    if (!subscriber.isPresent()) {
        //brand spanking new sub, live as botnak caught it
        addSub(new Subscriber(name, LocalDateTime.now(), true, 0));
        notifyTrayIcon(name + " has just subscribed!", false);
        playSubscriberSound();
    //we're going to return false (end of method) so that Botnak generates the message
    //like it did before this manager was created and implemented (and because less of the same code is better eh?)
    } else if (subscriber.get().isActive()) {
        //this may have been twitchnotify telling us twice, discard without messing up sounds
        return true;
    } else {
        //re-sub!
        //if we got the message, this means a month has passed, and they cancelled
        //question: should the streak still matter, if they're quick enough to resub?
        //answer: Botnak automatically acknowledges them for their continued support anyways, and
        // if they decide to cancel just to get the notification again, they deserve their streak to be reset
        String content = name + " has just RE-subscribed!";
        MessageQueue.addMessage(new Message().setContent(content).setChannel(channel).setType(Message.MessageType.SUB_NOTIFY));
        notifyTrayIcon(content, false);
        playSubscriberSound();
        subscriber.get().resetStreak();
        subscriber.get().setStarted(LocalDateTime.now());
        subscriber.get().setActive(true);
        setLastSubscriber(subscriber.get());
        return true;
    }
    return false;
}
Also used : Message(irc.message.Message) Subscriber(irc.Subscriber)

Example 3 with Subscriber

use of irc.Subscriber in project Botnak by Gocnak.

the class SubscriberManager method updateSubscriber.

/**
     * How's our little friend doing?
     * <p>
     * Called from the Channel class of Pircbot, this method
     * updates the sub's status (donation-wise) based on how
     * many months it's been since they first subbed, and checks
     * to see if the person subscribed while offline (new or not).
     *
     * @param u               The user object of the potential subscriber.
     * @param channel         Your channel name, for the messages.
     * @param currentlyActive Boolean used to determine current sub status of the user.
     */
public void updateSubscriber(User u, String channel, boolean currentlyActive) {
    if (u.getNick().equalsIgnoreCase(Settings.accountManager.getUserAccount().getName()))
        return;
    //you will always be your own sub, silly
    Optional<Subscriber> s = getSubscriber(u.getNick());
    if (s.isPresent()) {
        if (s.get().isActive()) {
            int streak = s.get().getStreak();
            int monthsSince = (int) (s.get().getStarted().until(LocalDateTime.now(), ChronoUnit.DAYS) / 32);
            if (monthsSince > streak) {
                if (currentlyActive) {
                    String content = s.get().getName() + " has continued their subscription for over " + (monthsSince) + ((monthsSince) > 1 ? " months!" : " month!");
                    MessageQueue.addMessage(new Message().setChannel(channel).setType(Message.MessageType.SUB_NOTIFY).setContent(content));
                    //this will most likely be 1
                    s.get().incrementStreak(monthsSince - streak);
                    playSubscriberSound();
                } else {
                    //we're offering a month to re-sub
                    s.get().setActive(false);
                    s.get().resetStreak();
                }
            }
        } else {
            if (currentlyActive) {
                // this has the potential to be an offline re-sub:
                // botnak will know that the sub is currently alive if it catches it live, (see the other use of SUB_NOTIFY)
                // however if the person subscribes offline, botnak has no way of telling, and
                // the next time they talk is the only time Botnak (and perhaps you as well) knows for sure that they did
                // so, we need to update the date the user subbed to now, ensure their streak is reset, and
                // make botnak send a "thanks for subbing offline" message
                //or twitchnotify could have been a douchenozzle and did not send the message
                String content = s.get().getName() + " has RE-subscribed offline!";
                if (Settings.botAnnounceSubscribers.getValue()) {
                    Settings.accountManager.getBot().sendMessage(channel, ".me " + u.getNick() + " has just RE-subscribed!");
                }
                MessageQueue.addMessage(new Message().setContent(content).setType(Message.MessageType.SUB_NOTIFY).setChannel(channel));
                s.get().resetStreak();
                s.get().setStarted(LocalDateTime.now());
                s.get().setActive(true);
                playSubscriberSound();
                setLastSubscriber(s.get());
                notifyTrayIcon(content, false);
            }
        }
    } else {
        if (currentlyActive) {
            //or twitchnotify could have been a douchenozzle and did not send the message
            if (Settings.botAnnounceSubscribers.getValue()) {
                Settings.accountManager.getBot().sendMessage(channel, ".me " + u.getNick() + " has just subscribed!");
            }
            String content = u.getNick().toLowerCase() + " has subscribed offline!";
            MessageQueue.addMessage(new Message().setContent(content).setType(Message.MessageType.SUB_NOTIFY).setChannel(channel));
            addSub(new Subscriber(u.getNick().toLowerCase(), LocalDateTime.now(), true, 0));
            playSubscriberSound();
            notifyTrayIcon(content, false);
        }
    }
}
Also used : Message(irc.message.Message) Subscriber(irc.Subscriber)

Example 4 with Subscriber

use of irc.Subscriber in project Botnak by Gocnak.

the class SubscriberManager method scanInitialSubscribers.

public void scanInitialSubscribers(String channel, Oauth key, int passesCompleted, HashSet<Subscriber> set) {
    String oauth = key.getKey().split(":")[1];
    String urlString = "https://api.twitch.tv/kraken/channels/" + channel + "/subscriptions?oauth_token=" + oauth + "&limit=100";
    String offset = "&offset=" + String.valueOf(100 * passesCompleted);
    urlString += offset;
    try {
        String line = Utils.createAndParseBufferedReader(new URL(urlString).openStream());
        if (!line.isEmpty()) {
            JSONObject entire = new JSONObject(line);
            if (entire.has("error")) {
                GUIMain.log("Error scanning for initial subs, does your OAuth key allow for this?");
            } else {
                int total = entire.getInt("_total");
                int passes = (total > 100 ? (int) Math.ceil((double) total / 100.0) : 1);
                if (passes == passesCompleted) {
                    fillSubscribers(set);
                    GUIMain.log("Successfully scanned " + set.size() + " subscriber(s)!");
                    Settings.scannedInitialSubscribers.setValue(true);
                } else {
                    JSONArray subs = entire.getJSONArray("subscriptions");
                    for (int subIndex = 0; subIndex < subs.length(); subIndex++) {
                        JSONObject outer = subs.getJSONObject(subIndex);
                        JSONObject user = outer.getJSONObject("user");
                        String name = user.getString("name");
                        //don't want to add yourself
                        if (name.equalsIgnoreCase(channel))
                            continue;
                        LocalDateTime started = LocalDateTime.parse(outer.getString("created_at"), DateTimeFormatter.ISO_DATE_TIME);
                        int streak = (int) started.until(LocalDateTime.now(), ChronoUnit.MONTHS);
                        Subscriber s = new Subscriber(name, started, true, streak);
                        set.add(s);
                    }
                    scanInitialSubscribers(channel, key, passesCompleted + 1, set);
                }
            }
        }
    } catch (Exception e) {
        if (e.getMessage().contains("422")) {
            //the user does not have a sub button
            key.setCanReadSubscribers(false);
            GUIMain.log("Failed to parse subscribers; your channel is not partnered!");
        } else
            GUIMain.log(e);
    }
}
Also used : LocalDateTime(java.time.LocalDateTime) JSONObject(lib.JSON.JSONObject) Subscriber(irc.Subscriber) JSONArray(lib.JSON.JSONArray) URL(java.net.URL)

Aggregations

Subscriber (irc.Subscriber)4 Message (irc.message.Message)3 URL (java.net.URL)1 LocalDateTime (java.time.LocalDateTime)1 SimpleAttributeSet (javax.swing.text.SimpleAttributeSet)1 JSONArray (lib.JSON.JSONArray)1 JSONObject (lib.JSON.JSONObject)1 User (lib.pircbot.User)1