def create(self, ctx, name: TagName, *, content: commands.clean_content):
"""Creates a new tag owned by you.
This tag is server-specific and cannot be used in other servers.
For global tags that others can use, consider using the tag box.
Note that server moderators can delete your tag.
"""
# due to our denormalized design, I need to insert the tag in two different
# tables, make sure it's in a transaction so if one of the inserts fail I
# can act upon it
query = """WITH tag_insert AS (
INSERT INTO tags (name, content, owner_id, location_id)
VALUES ($1, $2, $3, $4)
RETURNING id
)
INSERT INTO tag_lookup (name, owner_id, location_id, tag_id)
VALUES ($1, $3, $4, (SELECT id FROM tag_insert));
"""
# since I'm checking for the exception type and acting on it, I need
# to use the manual transaction blocks
tr = ctx.db.transaction()
await tr.start()
try:
await ctx.db.execute(query, name, content, ctx.author.id, ctx.guild.id)
except asyncpg.UniqueViolationError:
await tr.rollback()
await ctx.send('This tag already exists.')
except:
await tr.rollback()
await ctx.send('Could not create tag.')
else:
await tr.commit()
await ctx.send(f'Tag {name} successfully created.')
评论列表
文章目录