From 9034af447f74cb0c23190403e760fdff4d0dbfa3 Mon Sep 17 00:00:00 2001 From: Filip Strajnar Date: Mon, 10 Jun 2024 23:03:17 +0200 Subject: [PATCH] Now using pipe instead of file. --- main.go | 38 ++++++++++---------------------------- 1 file changed, 10 insertions(+), 28 deletions(-) diff --git a/main.go b/main.go index 59fa43b..b0512e5 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,7 @@ package main import ( "encoding/json" "image/png" + "io" "os" "time" @@ -46,37 +47,18 @@ func SendScreenshots(discord *discordgo.Session, channelId string, sleepDuration panic(err) } - file, err := os.Create(screenshot_path) + imageReader, imageWriter := io.Pipe() + go func() { + err = png.Encode(imageWriter, new_image) - if err != nil { - panic(err) - } + if err != nil { + panic(err) + } - err = png.Encode(file, new_image) + imageWriter.Close() + }() - if err != nil { - panic(err) - } - - err = file.Close() - - if err != nil { - panic(err) - } - - file, err = os.Open(screenshot_path) - - if err != nil { - panic(err) - } - - _, err = discord.ChannelFileSend(channelId, screenshot_path, file) - - if err != nil { - panic(err) - } - - err = file.Close() + _, err = discord.ChannelFileSend(channelId, screenshot_path, imageReader) if err != nil { panic(err)