Skip to content

Commit 261cc6f

Browse files
authored
Merge pull request CloudBotIRC#166 from linuxdaemon/gonzobot+herald-cache
Add caching to herald.py
2 parents 4c97b45 + bcd09ec commit 261cc6f

1 file changed

Lines changed: 34 additions & 22 deletions

File tree

plugins/herald.py

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import random
22
import re
33
import time
4+
from collections import defaultdict
45

56
from sqlalchemy import Table, Column, String, PrimaryKeyConstraint, select
67

@@ -19,28 +20,37 @@
1920
PrimaryKeyConstraint('name', 'chan')
2021
)
2122

23+
herald_cache = defaultdict(dict)
24+
25+
26+
@hook.on_start
27+
def load_cache(db):
28+
herald_cache.clear()
29+
for row in db.execute(table.select()):
30+
herald_cache[row["chan"]][row["name"]] = row["quote"]
31+
2232

2333
@hook.command()
24-
def herald(text, nick, chan, db):
34+
def herald(text, nick, chan, db, reply):
2535
"""{<message>|show|delete|remove} - adds a greeting for your nick that will be announced everytime you join the channel. Using .herald show will show your current herald and .herald delete will remove your greeting."""
2636
if text.lower() == "show":
27-
query = select([table.c.quote]).where(table.c.name == nick.lower()).where(table.c.chan == chan.lower())
28-
greeting = db.execute(query).fetchone()
29-
if greeting:
30-
return greeting[0]
31-
else:
37+
greeting = herald_cache[chan.casefold()].get(nick.casefold())
38+
if greeting is None:
3239
return "you don't have a herald set try .herald <message> to set your greeting."
40+
41+
return greeting
3342
elif text.lower() in ["delete", "remove"]:
34-
greeting = db.execute(
35-
select([table.c.quote]).where(table.c.name == nick.lower()).where(table.c.chan == chan.lower())
36-
).fetchone()
43+
greeting = herald_cache[chan.casefold()].get(nick.casefold())
44+
if greeting is None:
45+
return "no herald set, unable to delete."
46+
3747
query = table.delete().where(table.c.name == nick.lower()).where(table.c.chan == chan.lower())
38-
res = db.execute(query)
48+
db.execute(query)
3949
db.commit()
40-
if res.rowcount > 0:
41-
return "greeting \'{}\' for {} has been removed".format(greeting[0], nick)
42-
else:
43-
return "no herald set, unable to delete."
50+
51+
reply("greeting \'{}\' for {} has been removed".format(greeting[0], nick))
52+
53+
load_cache(db)
4454
else:
4555
res = db.execute(
4656
table.update().where(table.c.name == nick.lower()).where(table.c.chan == chan.lower()).values(quote=text)
@@ -49,11 +59,13 @@ def herald(text, nick, chan, db):
4959
db.execute(table.insert().values(name=nick.lower(), chan=chan.lower(), quote=text))
5060

5161
db.commit()
52-
return "greeting successfully added"
62+
reply("greeting successfully added")
63+
64+
load_cache(db)
5365

5466

5567
@hook.command(permissions=["botcontrol", "snoonetstaff"])
56-
def deleteherald(text, chan, db):
68+
def deleteherald(text, chan, db, reply):
5769
"""<nickname> - Delete [nickname]'s herald."""
5870

5971
nick = text.strip()
@@ -65,13 +77,15 @@ def deleteherald(text, chan, db):
6577
db.commit()
6678

6779
if res.rowcount > 0:
68-
return "greeting for {} has been removed".format(text.lower())
80+
reply("greeting for {} has been removed".format(text.lower()))
6981
else:
70-
return "{} does not have a herald".format(text.lower())
82+
reply("{} does not have a herald".format(text.lower()))
83+
84+
load_cache(db)
7185

7286

7387
@hook.irc_raw("JOIN", singlethread=True)
74-
def welcome(nick, message, db, bot, chan):
88+
def welcome(nick, message, bot, chan):
7589
decoy = re.compile('[Òo○O0öøóȯôőŏᴏōο][<><]')
7690
colors_re = re.compile("\x02|\x03(?:\d{1,2}(?:,\d{1,2})?)?", re.UNICODE)
7791
bino_re = re.compile('b+i+n+o+', re.IGNORECASE)
@@ -85,9 +99,7 @@ def welcome(nick, message, db, bot, chan):
8599
else:
86100
floodcheck[chan] = time.time()
87101

88-
welcome = db.execute(
89-
select([table.c.quote]).where(table.c.name == nick.lower()).where(table.c.chan == chan.lower())
90-
).fetchone()
102+
welcome = herald_cache[chan.casefold()].get(nick.casefold())
91103
if welcome:
92104
greet = welcome[0]
93105
stripped = greet.translate(dict.fromkeys(["\u200b", " ", "\u202f", "\x02"]))

0 commit comments

Comments
 (0)