Skip to content

Commit 9e9fb74

Browse files
authored
Merge pull request CloudBotIRC#139 from linuxdaemon/gonzobot+chainallow-exist-check
Refactor logic for adding and deleting allowed chain commands
2 parents 957d9ce + 8450781 commit 9e9fb74

1 file changed

Lines changed: 15 additions & 4 deletions

File tree

plugins/chain.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from cloudbot import hook
88
from cloudbot.event import CommandEvent
99
from cloudbot.util import database
10-
from cloudbot.util.formatting import chunk_str
10+
from cloudbot.util.formatting import chunk_str, pluralize
1111

1212
commands = Table(
1313
'chain_commands',
@@ -84,15 +84,26 @@ def chainallow(text, db, notice_doc, bot):
8484

8585
values['allowed'] = allow
8686

87-
db.execute(commands.insert().values(**values))
87+
updated = True
88+
res = db.execute(commands.update().values(**values).where(commands.c.hook == hook_name))
89+
if res.rowcount == 0:
90+
updated = False
91+
db.execute(commands.insert().values(**values))
92+
8893
db.commit()
8994
load_cache(db)
90-
return "Add '{}' as an allowed command".format(hook_name)
95+
if updated:
96+
return "Updated state of '{}' in chainallow to allowed={}".format(hook_name, allow_cache.get(hook_name))
97+
98+
if allow_cache.get(hook_name):
99+
return "Added '{}' as an allowed command".format(hook_name)
100+
101+
return "Added '{}' as a denied command".format(hook_name)
91102
elif subcmd == "del":
92103
res = db.execute(commands.delete().where(commands.c.hook == hook_name))
93104
db.commit()
94105
load_cache(db)
95-
return "Deleted {} rows.".format(res.rowcount)
106+
return "Deleted {}.".format(pluralize(res.rowcount, "row"))
96107
else:
97108
return notice_doc()
98109

0 commit comments

Comments
 (0)