goscreenshot/main.go

109 lines
1.6 KiB
Go

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"
func GetConfig() (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)
return token, channelId, nil
}
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() {
token, channelId, err := GetConfig()
if err != nil {
panic(err)
}
discord, err := discordgo.New("Bot " + token)
if err != nil {
panic(err)
}
sleepDuration, err := time.ParseDuration("3s")
if err != nil {
panic(err)
}
SendScreenshots(discord, channelId, sleepDuration)
}