Skip to content

Commit e9ec901

Browse files
committed
Clean up and fix errors in badwords.py
1 parent 9bfdbca commit e9ec901

1 file changed

Lines changed: 57 additions & 74 deletions

File tree

plugins/badwords.py

Lines changed: 57 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import re
2-
import random
2+
from collections import defaultdict
33

4-
from sqlalchemy import Table, Column, String, PrimaryKeyConstraint
4+
from sqlalchemy import Table, Column, String, PrimaryKeyConstraint, select
55

6-
from cloudbot.event import EventType
76
from cloudbot import hook
7+
from cloudbot.event import EventType
88
from cloudbot.util import database
99

1010
table = Table(
@@ -16,104 +16,87 @@
1616
PrimaryKeyConstraint('word', 'chan')
1717
)
1818

19+
badcache = defaultdict(list)
20+
1921

2022
@hook.on_start()
2123
@hook.command("loadbad", permissions=["badwords"], autohelp=False)
2224
def load_bad(db):
2325
"""- Should run on start of bot to load the existing words into the regex"""
24-
global badword_re, blacklist, black_re
25-
words = db.execute("select word from badwords").fetchall()
26-
out = ""
27-
for word in words:
28-
out = out + "{}|".format(word[0])
29-
blacklist = out[:-1]
30-
black_re = '(\s|^|[^\w\s])({0})(\s|$|[^\w\s])'.format(blacklist)
31-
badwords_re = re.compile(black_re, re.IGNORECASE)
26+
global badword_re
27+
badcache.clear()
28+
words = []
29+
for chan, word in db.execute(select([table.c.chan, table.c.word])):
30+
badcache[chan.casefold()].append(word)
31+
words.append(word)
32+
33+
badword_re = re.compile(
34+
r'(\s|^|[^\w\s])({0})(\s|$|[^\w\s])'.format('|'.join(words)), re.IGNORECASE
35+
)
3236

3337

3438
@hook.command("addbad", permissions=["badwords"])
35-
def add_bad(text, nick, db, conn):
39+
def add_bad(text, nick, db):
3640
"""<word> <channel> - adds a bad word to the auto kick list must specify a channel with each word"""
37-
global blacklist, black_re, blacklist
38-
word = text.split(' ')[0].lower()
39-
channel = text.split(' ')[1].lower()
41+
splt = text.lower().split(None, 1)
42+
word, channel = splt
4043
if not channel.startswith('#'):
4144
return "Please specify a valid channel name after the bad word."
45+
4246
word = re.escape(word)
43-
wordlist = list_bad(channel, db, conn)
47+
wordlist = list_bad(channel)
4448
if word in wordlist:
4549
return "{} is already added to the bad word list for {}".format(
46-
word,
47-
channel)
48-
else:
49-
if len(
50-
db.execute(
51-
"select word from badwords where chan = :chan", {
52-
"chan": channel}).fetchall()) < 10:
53-
db.execute(
54-
"insert into badwords ( word, nick, chan ) values ( :word, :nick, :chan)", {
55-
"word": word, "nick": nick, "chan": channel})
56-
db.commit()
57-
load_bad(db, conn)
58-
wordlist = list_bad(channel, db, conn)
59-
return "Current badwords: {}".format(wordlist)
60-
else:
61-
return "There are too many words listed for channel {}. Please remove a word using .rmbad before adding anymore. For a list of bad words use .listbad".format(
62-
channel)
50+
word, channel
51+
)
52+
53+
if len(badcache[channel]) >= 10:
54+
return "There are too many words listed for channel {}. Please remove a word using .rmbad before adding anymore. For a list of bad words use .listbad".format(
55+
channel
56+
)
57+
58+
db.execute(table.insert().values(word=word, nick=nick, chan=channel))
59+
db.commit()
60+
load_bad(db)
61+
wordlist = list_bad(channel)
62+
return "Current badwords: {}".format(wordlist)
6363

6464

6565
@hook.command("rmbad", "delbad", permissions=["badwords"])
66-
def del_bad(text, db, conn):
66+
def del_bad(text, db):
6767
"""<word> <channel> - removes the specified word from the specified channels bad word list"""
68-
global blacklist, black_re, blacklist
69-
word = text.split(' ')[0].lower()
70-
if not (text.split(' ')[1] or text.split(' ')[1]('#')):
71-
return "please specify a valid channel name"
72-
channel = text.split(' ')[1].lower()
73-
db.execute(
74-
"delete from badwords where word = :word and chan = :chan", {
75-
"word": word, "chan": channel})
68+
splt = text.lower().split(None, 1)
69+
word, channel = splt
70+
if not channel.startswith('#'):
71+
return "Please specify a valid channel name after the bad word."
72+
73+
db.execute(table.delete().where(table.c.word == word).where(table.c.chan == channel))
7674
db.commit()
77-
newlist = list_bad(channel, db, conn)
78-
load_bad(db, conn)
75+
newlist = list_bad(channel)
76+
load_bad(db)
7977
return "Removing {} new bad word list for {} is: {}".format(
80-
word,
81-
channel,
82-
newlist)
78+
word, channel, newlist
79+
)
8380

8481

8582
@hook.command("listbad", permissions=["badwords"])
86-
def list_bad(text, db):
83+
def list_bad(text):
8784
"""<channel> - Returns a list of bad words specify a channel to see words for a particular channel"""
8885
text = text.split(' ')[0].lower()
89-
out = ""
9086
if not text.startswith('#'):
9187
return "Please specify a valid channel name"
92-
words = db.execute(
93-
"select word from badwords where chan = :chan", {
94-
"chan": text}).fetchall()
95-
for word in words:
96-
out = out + "{}|".format(word[0])
97-
return out[:-1]
88+
89+
return '|'.join(badcache[text])
9890

9991

10092
@hook.event([EventType.message, EventType.action], singlethread=True)
101-
def test_badwords(event, db, conn, message):
102-
match = re.search(black_re, event.content, re.IGNORECASE)
103-
if match:
104-
# Check to see if the match is for this channel
105-
word = match.group().lower().strip()
106-
check = db.execute(
107-
"select word, nick from badwords where word = :word and chan = :chan", {
108-
"word": word, "chan": event.chan}).fetchone()
109-
if (check) or ((word == "fap") and (event.chan == "#conversations")):
110-
out = "KICK {} {} :that fucking word is so damn offensive".format(
111-
event.chan,
112-
event.nick)
113-
message(
114-
"{}, congratulations you've won!".format(
115-
event.nick),
116-
event.chan)
117-
conn.send(out)
118-
else:
119-
pass
93+
def test_badwords(conn, message, chan, content, nick):
94+
match = badword_re.match(content)
95+
if not match:
96+
return
97+
98+
# Check to see if the match is for this channel
99+
word = match.group().lower().strip()
100+
if word in badcache[chan]:
101+
conn.cmd("KICK", chan, nick, "that fucking word is so damn offensive")
102+
message("{}, congratulations you've won!".format(nick))

0 commit comments

Comments
 (0)