def announcement_succeeded(future, recipients, sender, time_started, embed): time_spent = round(time.time() - time_started, 2) results = future.result() failed_list = [] errors = [] try: for i, res in enumerate(results): if isinstance(res, Exception): tb = traceback.format_tb(res.__traceback__) tb = '\n'.join(tb) errors.append(f'```py\n{str(res)}\n{tb}```') failed_list.append(recipients[i]) except IndexError: pass sch = [] if not failed_list: msg = f"Your announcement has been successfully sent to all {len(recipients)} members in {time_spent} seconds" embed.title = msg sch.append(sender.send(embed=embed)) else: msg = f"Your announcement has been successfully sent to {len(recipients) - len(failed_list)}/{len(recipients)} members in {time_spent} seconds" embed.title = msg sch.append(sender.send(embed=embed)) sch.append( split_send_message( sender, 'Failed for:\n' + '\n'.join(m.nick or m.name for m in failed_list))) sch.append(split_send_message(sender, 'Errors:' + '\n'.join(errors))) asyncio.ensure_future(asyncio.gather(*sch))
async def run_shell(command: list, channel): timeout = 15 if command[0] in ('aria2c', 'curl', 'wget', 'git', 'http'): timeout = 120 def kill(proc): if proc.returncode is None: proc.kill() proc.killed_by_bot = True command = ' '.join(command) await channel.send("Executing shell command `%s`" % command) async with channel.typing(): PIPE = asyncio.subprocess.PIPE DEVNULL = asyncio.subprocess.DEVNULL proc = await asyncio.subprocess.create_subprocess_shell( command, stdin=DEVNULL, stderr=PIPE, stdout=PIPE) proc.killed_by_bot = False bot.loop.call_later(timeout, kill, proc) buffer = '' timer = time.time() while proc.returncode is None: if not proc.stdout.at_eof(): buffer += (await proc.stdout.readline()).decode() if not proc.stderr.at_eof(): buffer += (await proc.stderr.readline()).decode() if time.time() - timer > 1 or len(buffer) > 1500: asyncio.ensure_future( split_send_message(channel, buffer, '```')) buffer = '' timer = time.time() await asyncio.sleep(0.1) buffer += (await proc.stdout.read()).decode() buffer += (await proc.stderr.read()).decode() if buffer: await split_send_message(channel, buffer, '```') if proc.killed_by_bot: await channel.send( "Operation exceeded the %d seconds timeout, so I had to kill it:sweat_smile:" % timeout) await channel.send("Process terminated with exit code %d" % proc.returncode)