Now using pipe instead of file.

This commit is contained in:
Filip Strajnar 2024-06-10 23:03:17 +02:00
parent b04d2625f8
commit 9034af447f

38
main.go
View file

@ -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)