To achieve the task of creating a script that converts an uploaded image to a negative image with different levels of intensity, you need to take several steps. The existing Go code interacts with Discord using the bwmarrin/discordgo library to listen to messages with specific prefixes. Here, I'll guide you to extend this functionality to process images:
-
Setup Image Processing: Use an image processing library in Go, such as gocv or github.com/disintegration/imaging, to manipulate the image colors.
-
Handle Image Commands: Modify the messageCreate function to parse the command for intensity and apply the negative effect.
-
Send the Processed Image: Use DiscordGo to upload and send the processed image back to the channel.
Here's a detailed script to help you achieve this:
package main
import (
"bytes"
"fmt"
"image"
"image/color"
"io/ioutil"
"net/http"
"os"
"os/signal"
"strconv"
"strings"
"syscall"
"github.com/bwmarrin/discordgo"
"github.com/disintegration/imaging"
)
const (
prefix = "+"
prefix1 = "-"
prefix2 = "!"
)
var (
whiteList = map[string]bool{
"1184449624950460488": true,
"292953664492929025": true,
}
allowedChannels = map[string]bool{
"1087629024139943936": true,
}
)
func main() {
token := "YOUR_DISCORD_TOKEN"
dg, err := discordgo.New("Bot " + token)
if err != nil {
fmt.Println("Error creating Discord session:", err)
return
}
dg.AddHandler(messageCreate)
err = dg.Open()
if err != nil {
fmt.Println("Error opening Discord session:", err)
return
}
defer dg.Close()
fmt.Println("Bot is now running. Press Ctrl+C to exit.")
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
<-sc
}
func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
if !allowedChannels[m.ChannelID] && !whiteList[m.Author.ID] {
return
}
if strings.HasPrefix(m.Content, prefix) || strings.HasPrefix(m.Content, prefix1) || strings.HasPrefix(m.Content, prefix2) {
parts := strings.Split(m.Content, " ")
command := parts[0]
switch command {
case prefix2 + "ss":
if len(m.Message.Attachments) <= 0 {
return
}
intensity := 100 // Default intensity
if len(parts) > 1 {
if value, err := strconv.Atoi(parts[1]); err == nil {
intensity = value
}
}
IMGurl := m.Message.Attachments[0].URL
processAndSendImage(s, m.ChannelID, IMGurl, intensity)
}
}
}
func processAndSendImage(s *discordgo.Session, channelID string, IMGurl string, intensity int) {
resp, err := http.Get(IMGurl)
if err != nil {
fmt.Println("Error downloading image:", err)
return
}
defer resp.Body.Close()
imgData, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("Error reading image data:", err)
return
}
img, _, err := image.Decode(bytes.NewReader(imgData))
if err != nil {
fmt.Println("Error decoding image:", err)
return
}
negativeImg := createNegative(img, intensity)
var imgBuffer bytes.Buffer
err = imaging.Encode(&imgBuffer, negativeImg, imaging.PNG)
if err != nil {
fmt.Println("Error encoding image:", err)
return
}
s.ChannelFileSend(channelID, "negative.png", &imgBuffer)
}
func createNegative(img image.Image, intensity int) image.Image {
bounds := img.Bounds()
negativeImg := image.NewRGBA(bounds)
invFactor := (255 * intensity) / 100
for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
for x := bounds.Min.X; x < bounds.Max.X; x++ {
r, g, b, a := img.At(x, y).RGBA()
negativeImg.Set(x, y, color.RGBA{
R: uint8(invFactor - r/256),
G: uint8(invFactor - g/256),
B: uint8(invFactor - b/256),
A: uint8(a / 256),
})
}
}
return negativeImg
}
Key Points:
- Image Processing: The
createNegative function inverts the colors by subtracting the existing color values from an adjusted intensity factor.
- Intensity Handling: The
intensity value scales the intensity of the negative effect.
- Image Retrieval and Sending: The code retrieves the image from a URL, processes it, and sends it back to Discord.
Replace "YOUR_DISCORD_TOKEN" with your actual Discord bot token and remember to handle this token securely. Also, ensure that the bot has proper permissions to read messages and send files in the targeted channels.