예제 #1
0
        def btn_edit():
            message_content = self.ui.send_content_ph.toPlainText()
            message_file = self.ui.send_attachment_ph.text()
            embed_title = self.ui.send_title_ph.text()
            embed_url = self.ui.send_url_ph.text()
            embed_author = self.ui.send_author_ph.text()
            embed_author_icon = self.ui.send_author_icon_ph.text()
            embed_author_url = self.ui.send_author_url_ph.text()
            embed_image = self.ui.send_image_ph.text()
            embed_thumbnail = self.ui.send_thumbnail_ph.text()
            embed_footer = self.ui.send_footer_ph.text()
            embed_footer_icon = self.ui.send_footer_icon_ph.text()
            embed_timestamp = self.ui.send_timestamp_cb.isChecked()
            embed_color = self.ui.send_color_ph.text()
            if embed_color != "":
                e_color = int(embed_color, 16)
            else:
                e_color = 000000
            embed_desc = self.ui.send_desc_ph.toPlainText()
            wh_u = self.ui.wh_url_ph.text()
            wh_n = self.ui.wh_un_ph.text()
            wh_a = self.ui.wh_avatar_ph.text()

            webhook = DiscordWebhook(url=wh_u,
                                     username=wh_n,
                                     avatar_url=wh_a,
                                     content=message_content)
            embed = DiscordEmbed(title=embed_title,
                                 description=embed_desc,
                                 color=e_color,
                                 url=embed_url)
            embed.set_author(name=embed_author,
                             url=embed_author_url,
                             icon_url=embed_author_icon)
            embed.set_image(url=embed_image)
            embed.set_thumbnail(url=embed_thumbnail)
            embed.set_footer(text=embed_footer, icon_url=embed_footer_icon)
            if embed_timestamp == True:
                embed.set_timestamp()
            if embed_title != "" or embed_url != "" or embed_author != "" or embed_author_icon != "" or embed_author_url != "" or embed_image != "" or embed_thumbnail != "" or embed_footer != "" or embed_footer_icon != "" or embed_timestamp == True or embed_color != "" or embed_desc != "":
                webhook.add_embed(embed)
            if message_file != "":
                with open(message_file, "rb") as f:
                    webhook.add_file(file=f.read(),
                                     filename=attachment_file_name)

            global sent_webhook
            webhook.edit(sent_webhook)
예제 #2
0
 def edit_weebhook():
     webhook = DiscordWebhook(url='your webhook url',
                              content='Webhook content before edit')
     sent_webhook = webhook.execute()
     webhook.content = 'After Edit'
     sleep(10)
     sent_webhook = webhook.edit(sent_webhook)
예제 #3
0
def main():
    """Do the main lifting!"""

    # Configuration and config stuff
    args = get_args()
    if args.config == 'resources/discordnotify.yml':
        config_file = dirname(realpath(__file__)) + \
                '/resources/discordnotify.yml'
    config = get_config(config_file)
    hook_name, hook, user = check_config(args, config)
    bcolors.info("Executing job: {}".format(bcolors.bold_format(args.command)),
                 strong=True)

    #Create main webhook for proc completion
    wh = DiscordWebhook(url=hook, \
            content="You've just started a job. Brace for updates...",\
            username=user['username'])
    embed = DiscordEmbed(title="Process running",\
            description="Full command line: {}".format(args.command), color=242424)
    if user['image']:
        embed.set_author(name="Job: {}".format(args.command.split(' ')[0]),
                         icon_url=user['image'])
    else:
        embed.set_author(name="Job: {}".format(args.command.split(' ')[0]))
    embed.set_footer(
        text="Sent with DiscordNotify by @blackf3ll - [email protected]")
    wh.add_embed(embed)
    sent_wh = wh.execute()

    #Pre-ampble and process start
    t1 = time()
    t2 = time()
    worker = False
    start_time = ctime()  #Human friendly :)
    output = ''
    p = subprocess.Popen(
            args.command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,\
            shell=True , encoding='utf-8', errors='replace')

    # Manage beat updates
    if args.beat:
        worker = True
        embed.add_embed_field(name="Job ongoing",
                              value="No updates yet...",
                              inline=False)
        stdout_q = multiprocessing.Queue()
        stdout_worker = multiprocessing.Process(target=stdout_reader,
                                                args=(p, stdout_q))
        stdout_worker.start()
        while p.poll() == None:
            try:
                while not stdout_q.empty():
                    output += stdout_q.get()
                t3 = time()
                if round(t3 - t2) >= args.beat:
                    t2 = int(t3)
                    # update embed summary data with stdout summary and time info
                    embed.fields[0]['value'] = \
                            "Started: {} Elapsed : {}s Output summary:\n{}".format(
                            start_time, round(t3-t1),trim_output(
                            output, config['instance_info']['max_embed_lines']))
                    wh.edit(sent_wh)  # and send it
            except Exception as e:
                bcolors.err("Error sending update:\n{}".format(e))

    # Mop up any un-read stdout and print to console
    output += p.stdout.read()  #.decode()
    duration = round(time() - t1)
    print(output)

    #Configure final report & embed in webhook
    was_error = "with errors" if p.returncode != 0 else "without errors"
    embed.add_embed_field(name="Process Complete",
                          value="Completed {} \
            (exit code {}) in {} Seconds".format(was_error, p.returncode,
                                                 duration),
                          inline=False)
    if output:
        out_summary = trim_output(output,
                                  config['instance_info']['max_embed_lines'])
    else:
        out_summary = "No data on STDOUT or STDERR."
    embed.add_embed_field(name="Stdout Summary",
                          value=out_summary,
                          inline=False)

    # Add any requested image - TODO - fix this so it works - add to file attachment?
    if args.image:
        embed.set_thumbnail(url='attachment://{}'.format(args.image))
    # Now send
    try:
        wh.edit(sent_wh)
    except Exception as e:
        bcolors.err("Failed to send update to Discord:\n{}".format(e))

    # Send any file on afterwards, to avoid conflictings file space/embed requirements
    if args.file:
        # Use a fresh webhook to avoid API limits and issues with large embeds
        filewh = DiscordWebhook(url=hook, \
                content="Here's the associated file for your job: {}.".format(
                args.command), username=user['username'])
        try:
            with open(args.file, "rb") as f:
                filewh.add_file(file=f.read(), filename=args.file)
                filewh.execute()
        except Exception as e:
            bcolors.err("Couldn't attach file : {} :\n{}".format(args.file, e))

    # Worker process cleanup
    if worker:
        stdout_worker.terminate()
        stdout_worker.join(5)