mirror of
https://onedev.fprog.nl/goscreenshot
synced 2026-02-14 21:33:43 +01:00
92 lines
1.5 KiB
Go
92 lines
1.5 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"image/png"
|
|
"io"
|
|
"os"
|
|
"time"
|
|
|
|
"github.com/bwmarrin/discordgo"
|
|
"github.com/kbinani/screenshot"
|
|
)
|
|
|
|
const screenshot_path = "screenshot.png"
|
|
const config_path = "config.json"
|
|
|
|
func GetConfig() (string, string, string, error) {
|
|
configFile, err := os.ReadFile(config_path)
|
|
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
isValid := json.Valid(configFile)
|
|
|
|
if !isValid {
|
|
println("Configuration in config.json is not valid JSON.")
|
|
panic(isValid)
|
|
}
|
|
|
|
var config map[string]interface{}
|
|
|
|
json.Unmarshal(configFile, &config)
|
|
|
|
token := config["token"].(string)
|
|
channelId := config["channelId"].(string)
|
|
interval := config["interval"].(string)
|
|
|
|
return token, channelId, interval, nil
|
|
}
|
|
|
|
func SendScreenshots(discord *discordgo.Session, channelId string, sleepDuration time.Duration) {
|
|
for {
|
|
new_image, err := screenshot.CaptureDisplay(0)
|
|
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
imageReader, imageWriter := io.Pipe()
|
|
go func() {
|
|
err = png.Encode(imageWriter, new_image)
|
|
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
imageWriter.Close()
|
|
}()
|
|
|
|
_, err = discord.ChannelFileSend(channelId, screenshot_path, imageReader)
|
|
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
time.Sleep(sleepDuration)
|
|
}
|
|
}
|
|
|
|
func main() {
|
|
token, channelId, interval, err := GetConfig()
|
|
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
discord, err := discordgo.New("Bot " + token)
|
|
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
sleepDuration, err := time.ParseDuration(interval)
|
|
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
SendScreenshots(discord, channelId, sleepDuration)
|
|
}
|