1) `list` auto-update was deleting the old message every time, now it (and `summary`) edits the existing message(s) when auto-updating *unless* the auto-update message isn't the most recent. If there are newer messages then the auto-update is deleted and reposted at the bottom
2) depending on the timing with which a user presses the "cancel" reaction to stop auto-update, it could be missed. Added a final check based on the count of the cancel reaction. This has the drawback that any user can cancel the auto-update, but I could check to make sure the message author is one of the people that pressed it.
This commit is contained in:
Tim Wilson 2020-08-25 14:53:55 -06:00 committed by GitHub
parent 2217e3688b
commit 0c6fd04444
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

65
bot.py
View File

@ -482,7 +482,10 @@ async def add(context, *, content):
if content == "": if content == "":
await context.message.channel.send("Invalid string") await context.message.channel.send("Invalid string")
else: else:
await context.message.delete() try:
await context.message.delete()
except:
pass
torStr = None torStr = None
for t in content.strip().split(" "): for t in content.strip().split(" "):
await context.message.channel.send('Adding torrent {}\n Please wait...'.format(t)) await context.message.channel.send('Adding torrent {}\n Please wait...'.format(t))
@ -602,7 +605,10 @@ async def summary(context, *, content="", repeat=False):
global REPEAT_COMMAND, REPEAT_MSG_LIST global REPEAT_COMMAND, REPEAT_MSG_LIST
if await CommandPrecheck(context): if await CommandPrecheck(context):
if not repeat: if not repeat:
await context.message.delete() try:
await context.message.delete()
except:
pass
stateEmoji = ('📜','' if repeat else '🔄','↕️') + torStateEmoji stateEmoji = ('📜','' if repeat else '🔄','↕️') + torStateEmoji
ignoreEmoji = ('') ignoreEmoji = ('')
@ -656,6 +662,13 @@ async def summary(context, *, content="", repeat=False):
else: else:
await msg.clear_reaction('🔄') await msg.clear_reaction('🔄')
await repeat_command(summary, context=context, content=content, msg_list=[msg]) await repeat_command(summary, context=context, content=content, msg_list=[msg])
if repeat: # a final check to see if the user has cancelled the repeat by checking the count of the cancel reaction
cache_msg = await context.message.channel.fetch_message(msg.id)
for r in cache_msg.reactions:
if str(r.emoji) == '' and r.count > 1:
REPEAT_COMMAND = False
REPEAT_MSG_LIST = []
await context.message.channel.send("❎ Auto-update cancelled...")
def strListToList(strList): def strListToList(strList):
if not re.match('^[0-9\,\-]+$', strList): if not re.match('^[0-9\,\-]+$', strList):
@ -779,6 +792,7 @@ async def repeat_command(command, context, content="", msg_list=[]):
await context.message.channel.send("❎ Auto-update timed out...") await context.message.channel.send("❎ Auto-update timed out...")
REPEAT_COMMAND = False REPEAT_COMMAND = False
REPEAT_MSG_LIST = [] REPEAT_MSG_LIST = []
start_time = 0
return return
# for msg in REPEAT_MSG_LIST: # for msg in REPEAT_MSG_LIST:
# await msg.delete() # await msg.delete()
@ -798,7 +812,10 @@ async def list_transfers(context, *, content="", repeat=False):
return return
if not repeat: if not repeat:
await context.message.delete() try:
await context.message.delete()
except:
pass
torrents = TSCLIENT.get_torrents_by(sort_by=sort_by, filter_by=filter_by, filter_regex=filter_regex) torrents = TSCLIENT.get_torrents_by(sort_by=sort_by, filter_by=filter_by, filter_regex=filter_regex)
@ -811,12 +828,13 @@ async def list_transfers(context, *, content="", repeat=False):
if context.message.channel.last_message_id != msgs[-1].id: if context.message.channel.last_message_id != msgs[-1].id:
for m in msgs: for m in msgs:
await m.delete() await m.delete()
msgs = [] msgs = []
for i,e in enumerate(embeds): for i,e in enumerate(embeds):
if i < len(msgs): if i < len(msgs):
await msgs[i].edit(embed=e) await msgs[i].edit(embed=e)
if i < len(embeds) and len(msgs[i].reactions) > 0: cache_msg = await context.message.channel.fetch_message(msgs[i].id)
await msgs[i].clear_reactions() if i < len(embeds) - 1 and len(cache_msg.reactions) > 0:
await cache_msg.clear_reactions()
else: else:
msgs.append(await context.message.channel.send(embed=e)) msgs.append(await context.message.channel.send(embed=e))
if len(msgs) > len(embeds): if len(msgs) > len(embeds):
@ -831,12 +849,10 @@ async def list_transfers(context, *, content="", repeat=False):
msg = msgs[-1] msg = msgs[-1]
msgRxns = msg.reactions # to get actual list of reactions, need to re-fetch the message from the server
for r in msgRxns: cache_msg = await context.message.channel.fetch_message(msg.id)
if str(r.emoji) not in rxnEmoji: msgRxns = [str(r.emoji) for r in cache_msg.reactions]
await msg.clear_reaction(r)
msgRxns = [str(r.emoji) for r in msgRxns]
for e in rxnEmoji: for e in rxnEmoji:
if e not in msgRxns: if e not in msgRxns:
await msg.add_reaction(e) await msg.add_reaction(e)
@ -859,6 +875,13 @@ async def list_transfers(context, *, content="", repeat=False):
else: else:
await msg.clear_reaction('🔄') await msg.clear_reaction('🔄')
await repeat_command(list_transfers, context=context, content=content, msg_list=msgs) await repeat_command(list_transfers, context=context, content=content, msg_list=msgs)
if repeat: # a final check to see if the user has cancelled the repeat by checking the count of the cancel reaction
cache_msg = await context.message.channel.fetch_message(msg.id)
for r in cache_msg.reactions:
if str(r.emoji) == '' and r.count > 1:
REPEAT_COMMAND = False
REPEAT_MSG_LIST = []
await context.message.channel.send("❎ Auto-update cancelled...")
@client.command(name='modify', aliases=['m'], pass_context=True) @client.command(name='modify', aliases=['m'], pass_context=True)
async def modify(context, *, content=""): async def modify(context, *, content=""):
@ -879,7 +902,10 @@ async def modify(context, *, content=""):
await context.message.channel.send("Invalid sort specified. Choose one of {}".format(str(sort_names))) await context.message.channel.send("Invalid sort specified. Choose one of {}".format(str(sort_names)))
return return
await context.message.delete() try:
await context.message.delete()
except:
pass
torrents = TSCLIENT.get_torrents_by(filter_by=filter_by, sort_by=sort_by, filter_regex=filter_regex, id_list=id_list) torrents = TSCLIENT.get_torrents_by(filter_by=filter_by, sort_by=sort_by, filter_regex=filter_regex, id_list=id_list)
@ -894,7 +920,10 @@ async def modify(context, *, content=""):
embed.set_author(name="No matching transfers found!", icon_url=LOGO_URL) embed.set_author(name="No matching transfers found!", icon_url=LOGO_URL)
embeds = [embed] embeds = [embed]
else: else:
await context.message.delete() try:
await context.message.delete()
except:
pass
ops = ["pauseall","resumeall"] ops = ["pauseall","resumeall"]
opNames = ["pause all","resume all"] opNames = ["pause all","resume all"]
opEmoji = ['','▶️'] opEmoji = ['','▶️']
@ -1025,13 +1054,19 @@ async def help(context, *, content=""):
@client.event @client.event
async def on_command_error(context, error): async def on_command_error(context, error):
if isinstance(error, commands.CommandOnCooldown): if isinstance(error, commands.CommandOnCooldown):
await context.message.delete() try:
await context.message.delete()
except:
pass
embed = discord.Embed(title="Error!", description='This command is on a {:.2f}s cooldown'.format(error.retry_after), color=0xb51a00) embed = discord.Embed(title="Error!", description='This command is on a {:.2f}s cooldown'.format(error.retry_after), color=0xb51a00)
message = await context.message.channel.send(embed=embed) message = await context.message.channel.send(embed=embed)
await asyncio.sleep(5) await asyncio.sleep(5)
await message.delete() await message.delete()
elif isinstance(error, commands.CommandNotFound): elif isinstance(error, commands.CommandNotFound):
await context.message.delete() try:
await context.message.delete()
except:
pass
embed = discord.Embed(title="Error!", description="I don't know that command!", color=0xb51a00) embed = discord.Embed(title="Error!", description="I don't know that command!", color=0xb51a00)
message = await context.message.channel.send(embed=embed) message = await context.message.channel.send(embed=embed)
await asyncio.sleep(2) await asyncio.sleep(2)