Skip to content

Commit 8450781

Browse files
committed
Refactor logic for adding and deleting allowed chain commands
1 parent 9bfdbca commit 8450781

1 file changed

Lines changed: 16 additions & 5 deletions

File tree

plugins/chain.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import asyncio
2+
import itertools
23
from operator import attrgetter
34

4-
import itertools
55
from sqlalchemy import Table, Column, String, Boolean, PrimaryKeyConstraint
66

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)