summaryrefslogtreecommitdiff
path: root/mentio.py
blob: fa46d7133fade7ab9e1a9b03b59951b4cd8bd5a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
from helpers import *
from re import compile as reg_compile
from traceback import format_exc as print_traceback


mentions   = open_json_file("mentio", {}) # contains a list of keywords for each player (uuid)
max_amount = 1000
arrow      = colorify(u"&r&7\u2192&r")
colors_reg = reg_compile(u"\u00A7[\\da-fk-or]") # finds color codes


def saveMentions():
    save_json_file("mentio", mentions)


@hook.event("player.AsyncPlayerChatEvent", "monitor")
def onChat(event):
    if not event.isCancelled():
        sender     = event.getPlayer()
        words      = event.getMessage().split(" ")
        recipients = event.getRecipients() # set of <Player>, may be a lazy or unmodifiable collection

        for recipient in list(recipients):
            recuid = uid(recipient)

            if recuid in mentions:
                keywords = mentions[uid(recipient)]
            else:
                # player
                keywords = [recipient.getName().lower(), stripcolors(recipient.getDisplayName()).lower()]

            rec_words = words[:] # copy
            for index, word in enumerate(rec_words):
                isMentioned = False

                for keyword in keywords:
                    if keyword in word.lower(): # is the keyword mentioned
                        isMentioned = True

                if isMentioned:
                    # join all color codes used upto this word
                    colors = "".join(colors_reg.findall("".join(words[:index+1])))
                    # highlight the word containing mention, then apply all previous color codes
                    rec_words[index] = colorify("&r&a&o") + stripcolors(word) + colorify("&r") + colors
                    # No need to
                    break

            # player was mentioned
            if rec_words != words:
                try:
                    recipients.remove(recipient) # don't send original message
                except:
                    # list might not be mutable, ignoring. Receiver will get the message twice
                    pass
                message = " "+" ".join([sender.getDisplayName(), arrow] + rec_words)
                msg(recipient, message, usecolor = False)
                recipient.playSound(recipient.getLocation(), "mob.chicken.plop", 1, 2)


def get_keywords(player):
    sender_id = uid(player)
    if sender_id in mentions.keys():
        keywords = mentions[sender_id]
    else:
        keywords = []
    return keywords


def add_keyword(sender, args):
    keywords = get_keywords(sender)
    new_word = stripcolors(args[1].lower())

    if (len(keywords) >= max_amount) and (max_amount >= 0):
        msg(sender, "&cYou are already listening for %s words! Try &6/mentio del <word>" % max_amount)
        return True

    if len(new_word) > 20:
        msg(sender, "&cThis word is longer than 20 characters. Pick a shorter one!")
        return True

    if new_word in keywords:
        msg(sender, "&cYou are already listening for this word! Try &6/mentio list")
        return True

    keywords.append(new_word)
    if keywords:
        mentions[uid(sender)] = keywords

    msg(sender, "&aYou are now listening for '&2%s&2'!" % new_word)
    saveMentions()
    return True


def del_keyword(sender, args):
    keywords = get_keywords(sender)
    del_word = stripcolors(args[1].lower())

    if not keywords:
        msg(sender, "&cYou are currently listening for no words! Try &6/mentio add <word>")
        return

    if del_word in keywords:
        keywords.remove(del_word)
        sender_id = uid(sender)
        if keywords:
            mentions[sender_id] = keywords
        elif sender_id in mentions:
            del mentions[sender_id]
        saveMentions()
        msg(sender, "&aYou are no longer listening for '&2%s&2'!" % del_word)
    else:
        msg(sender, "&cWe can't remove something that doesn't exist! Try &6/mentio list")


def show_help(player):
    msg(player, "&a/mentio add <word>")
    msg(player, "&a/mentio del <word>")
    msg(player, "&a/mentio list")


@hook.command("mentio")
def onListenCommand(sender, command, label, args):
    plugin_header(sender, "Mentio")

    argnum = len(args)
    if argnum:
        cmd = args[0].lower()

        # /mentio add <word>
        if argnum == 2 and cmd == "add":
            add_keyword(sender, args)

        # /mentio del <word>
        elif argnum == 2 and cmd == "del":
            del_keyword(sender, args)

        # /mentio list
        elif argnum == 1 and cmd == "list":
            msg(sender, "&aWords you're listening for:")
            keywords = get_keywords(sender)
            for word in keywords:
                msg(sender, "&c- &3%s" % word)
            if not keywords:
                msg(sender, "&cYou are currently listening for no words! Try &6/mentio add <word>")
        else:
            show_help(sender)
    else:
        show_help(sender)
    return True