goscreenshot/main.go

110 lines
1.7 KiB
Go
Raw Normal View History

2024-06-10 21:51:37 +02:00
package main
import (
"encoding/json"
"image/png"
"os"
"time"
"github.com/bwmarrin/discordgo"
"github.com/kbinani/screenshot"
)
const screenshot_path = "screenshot.png"
const config_path = "config.json"
2024-06-10 22:36:35 +02:00
func GetConfig() (string, string, string, error) {
2024-06-10 21:51:37 +02:00
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)
2024-06-10 22:36:35 +02:00
interval := config["interval"].(string)
2024-06-10 21:51:37 +02:00
2024-06-10 22:36:35 +02:00
return token, channelId, interval, nil
2024-06-10 21:51:37 +02:00
}
func SendScreenshots(discord *discordgo.Session, channelId string, sleepDuration time.Duration) {
for {
new_image, err := screenshot.CaptureDisplay(0)
if err != nil {
panic(err)
}
file, err := os.Create(screenshot_path)
if err != nil {
panic(err)
}
err = png.Encode(file, new_image)
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()
if err != nil {
panic(err)
}
time.Sleep(sleepDuration)
}
}
func main() {
2024-06-10 22:36:35 +02:00
token, channelId, interval, err := GetConfig()
2024-06-10 21:51:37 +02:00
if err != nil {
panic(err)
}
discord, err := discordgo.New("Bot " + token)
if err != nil {
panic(err)
}
2024-06-10 22:36:35 +02:00
sleepDuration, err := time.ParseDuration(interval)
2024-06-10 21:51:37 +02:00
if err != nil {
panic(err)
}
SendScreenshots(discord, channelId, sleepDuration)
}