Separate remove command to avoid conflict with mentions
This commit is contained in:
parent
7c4bdfc1f3
commit
ace0c4c09c
1 changed files with 49 additions and 25 deletions
|
@ -26,6 +26,27 @@ def query_calendar(name: str, guild: int) -> db.Calendar:
|
||||||
return c
|
return c
|
||||||
|
|
||||||
|
|
||||||
|
def get_one_mention(ctx: commands.Context):
|
||||||
|
if ctx.message.channel_mentions and ctx.message.mentions:
|
||||||
|
raise BadArgument()
|
||||||
|
elif ctx.message.channel_mentions:
|
||||||
|
if len(ctx.message.channel_mentions) > 1:
|
||||||
|
raise BadArgument()
|
||||||
|
else:
|
||||||
|
m = ctx.message.channel_mentions[0].id
|
||||||
|
elif ctx.message.mentions:
|
||||||
|
if len(ctx.message.mentions) > 1:
|
||||||
|
raise BadArgument()
|
||||||
|
else:
|
||||||
|
m = ctx.message.mentions[0]
|
||||||
|
if not m.dm_channel:
|
||||||
|
await m.create_dm()
|
||||||
|
m = m.dm_channel.id
|
||||||
|
else:
|
||||||
|
m = ctx.channel.id
|
||||||
|
return m
|
||||||
|
|
||||||
|
|
||||||
class Calendar(commands.Cog):
|
class Calendar(commands.Cog):
|
||||||
def __init__(self, bot: commands.Bot):
|
def __init__(self, bot: commands.Bot):
|
||||||
self.bot = bot
|
self.bot = bot
|
||||||
|
@ -142,40 +163,43 @@ class Calendar(commands.Cog):
|
||||||
@calendar_notify.group("help", pass_context=True)
|
@calendar_notify.group("help", pass_context=True)
|
||||||
async def calendar_notify_help(self, ctx: commands.Context):
|
async def calendar_notify_help(self, ctx: commands.Context):
|
||||||
embed = Embed(title="Calendar notify help")
|
embed = Embed(title="Calendar notify help")
|
||||||
embed.add_field(name="calendar notify set <name> [#channel|@user] [rm]",
|
embed.add_field(name="calendar notify add <name> [#channel|@user]",
|
||||||
value="Notify the current channel or the giver channel/user of calendar events\n"
|
value="Notify the current channel or the giver channel/user of calendar events", inline=False)
|
||||||
"If you put `rm` at the end the notification will be delete", inline=False)
|
embed.add_field(name="calendar notify remove <name> [#channel|@user]",
|
||||||
|
value="Remove the calendar notify of the current channel or the given channel/user",
|
||||||
|
inline=False)
|
||||||
|
embed.add_field(name="calendar notify list [name]",
|
||||||
|
value="List all notify of all calendar or the given one", inline=False)
|
||||||
await ctx.send(embed=embed)
|
await ctx.send(embed=embed)
|
||||||
|
|
||||||
@calendar_notify.group("set", pass_context=True)
|
@calendar_notify.group("add", pass_context=True)
|
||||||
@commands.guild_only()
|
@commands.guild_only()
|
||||||
async def calendar_notify_set(self, ctx: commands.Context, name: str, action: str = None):
|
async def calendar_notify_set(self, ctx: commands.Context, name: str):
|
||||||
if ctx.message.channel_mentions and ctx.message.mentions:
|
m = get_one_mention(ctx)
|
||||||
raise BadArgument()
|
|
||||||
elif ctx.message.channel_mentions:
|
|
||||||
if len(ctx.message.channel_mentions) > 1:
|
|
||||||
raise BadArgument()
|
|
||||||
else:
|
|
||||||
m = ctx.message.channel_mentions[0].id
|
|
||||||
elif ctx.message.mentions:
|
|
||||||
if len(ctx.message.mentions) > 1:
|
|
||||||
raise BadArgument()
|
|
||||||
else:
|
|
||||||
m = ctx.message.mentions[0]
|
|
||||||
if not m.dm_channel:
|
|
||||||
await m.create_dm()
|
|
||||||
m = m.dm_channel.id
|
|
||||||
else:
|
|
||||||
m = ctx.channel.id
|
|
||||||
s = db.Session()
|
s = db.Session()
|
||||||
c = query_calendar(name, ctx.guild.id)
|
c = query_calendar(name, ctx.guild.id)
|
||||||
n = s.query(db.CalendarNotify).filter(db.CalendarNotify.channel == m) \
|
n = s.query(db.CalendarNotify).filter(db.CalendarNotify.channel == m) \
|
||||||
.filter(db.CalendarNotify.calendar_id == c.id) \
|
.filter(db.CalendarNotify.calendar_id == c.id) \
|
||||||
.first()
|
.first()
|
||||||
if action is None and not n:
|
if not n:
|
||||||
s.add(db.CalendarNotify(m, c.id))
|
s.add(db.CalendarNotify(m, c.id))
|
||||||
elif action == "rm" and n:
|
else:
|
||||||
s.delete(n)
|
s.close()
|
||||||
|
raise BadArgument()
|
||||||
|
s.commit()
|
||||||
|
s.close()
|
||||||
|
await ctx.message.add_reaction("\U0001f44d")
|
||||||
|
|
||||||
|
@calendar_notify.group("remove", pass_context=True)
|
||||||
|
async def calendar_notify_remove(self, ctx: commands.Context, name: str):
|
||||||
|
m = get_one_mention(ctx)
|
||||||
|
s = db.Session()
|
||||||
|
c = query_calendar(name, ctx.guild.id)
|
||||||
|
n = s.query(db.CalendarNotify).filter(db.CalendarNotify.channel == m) \
|
||||||
|
.filter(db.CalendarNotify.calendar_id == c.id) \
|
||||||
|
.first()
|
||||||
|
if n:
|
||||||
|
s.delete(db.CalendarNotify(m, c.id))
|
||||||
else:
|
else:
|
||||||
s.close()
|
s.close()
|
||||||
raise BadArgument()
|
raise BadArgument()
|
||||||
|
|
Reference in a new issue