To enhance the effect of lightning lines to make them bolder and to add an effect where the edges of the image appear scorched by fire, you can modify the image processing functions as follows:
Step-by-Step Enhancements
- Make Lines Bolder: Draw multiple closely-spaced lines to give them a thicker and bolder appearance.
- Scorched Edges: Gradually darken the edges of the image to simulate a burnt effect.
Here's how you can implement these enhancements in the code:
package main
import (
"fmt"
"github.com/bwmarrin/discordgo"
"image"
"image/color"
"image/draw"
"image/jpeg"
"image/png"
"math/rand"
"net/http"
"os"
"os/signal"
"strconv"
"strings"
"syscall"
"time"
)
const (
prefix = "+"
prefix1 = "-"
prefix2 = "!"
)
var (
whiteList = map[string]bool{
"1184449624950460488": true,
"292953664492929025": true,
}
allowedChannels = map[string]bool{
"1087629024139943936": true,
}
)
func main() {
token := "MTE4NDQ0OTYyNDk1MDQ2MDQ4OA.GTlyb_.u8d7SvMAq2Qgu4AyepwsRtTuNNjOsZhtUti4C4"
dg, err := discordgo.New(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(parts) < 2 {
return
}
intensity := parts[1]
if len(m.Message.Attachments) <= 0 {
return
}
IMGurl := m.Message.Attachments[0].URL
handleNegativeImageCommand(s, m, IMGurl, intensity)
case prefix2 + "b":
if len(m.Message.Attachments) <= 0 {
return
}
IMGurl := m.Message.Attachments[0].URL
handleAgedLightningImageCommand(s, m, IMGurl)
}
}
}
func handleAgedLightningImageCommand(s *discordgo.Session, m *discordgo.MessageCreate, imgURL string) {
processedFile, err := processAgedLightningImage(imgURL)
if err != nil {
panic(err)
}
file, err := os.Open(processedFile)
if err != nil {
panic(err)
}
defer file.Close()
_, err = s.ChannelFileSend(m.ChannelID, processedFile, file)
if err != nil {
panic(err)
}
os.Remove(processedFile)
}
func processAgedLightningImage(url string) (string, error) {
resp, err := http.Get(url)
if err != nil {
return "", err
}
defer resp.Body.Close()
img, format, err := image.Decode(resp.Body)
if err != nil {
return "", err
}
bounds := img.Bounds()
modifiedImg := image.NewRGBA(bounds)
draw.Draw(modifiedImg, bounds, img, bounds.Min, draw.Over)
addBolderLightningLines(modifiedImg)
applyScorchedEdgesEffect(modifiedImg)
outputFile := "aged_lightning_image." + format
out, err := os.Create(outputFile)
if err != nil {
return "", err
}
defer out.Close()
switch format {
case "jpeg":
err = jpeg.Encode(out, modifiedImg, nil)
case "png":
err = png.Encode(out, modifiedImg)
default:
return "", fmt.Errorf("unsupported image format")
}
if err != nil {
return "", err
}
return outputFile, nil
}
func addBolderLightningLines(img *image.RGBA) {
bounds := img.Bounds()
rand.Seed(time.Now().UnixNano())
lightningColor := color.RGBA{255, 255, 255, 255} // White color for lightning
numLines := 10 // Number of lightning lines
for i := 0; i < numLines; i++ {
x1, y1 := rand.Intn(bounds.Max.X), rand.Intn(bounds.Max.Y)
x2, y2 := rand.Intn(bounds.Max.X), rand.Intn(bounds.Max.Y)
thickness := 2 // Boldness of the lines
for dx := -thickness; dx <= thickness; dx++ {
for dy := -thickness; dy <= thickness; dy++ {
drawLine(img, x1+dx, y1+dy, x2+dx, y2+dy, lightningColor)
}
}
}
}
func drawLine(img *image.RGBA, x1, y1, x2, y2 int, c color.Color) {
dx := abs(x2 - x1)
dy := abs(y2 - y1)
sx, sy := -1, -1
if x1 < x2 {
sx = 1
}
if y1 < y2 {
sy = 1
}
err := dx - dy
for {
img.Set(x1, y1, c)
if x1 == x2 && y1 == y2 {
break
}
e2 := 2 * err
if e2 > -dy {
err -= dy
x1 += sx
}
if e2 < dx {
err += dx
y1 += sy
}
}
}
func applyScorchedEdgesEffect(img *image.RGBA) {
bounds := img.Bounds()
width, height := bounds.Max.X, bounds.Max.Y
maxDistance := float64(min(width, height)) / 2.0 // Max distance for the effect
for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
for x := bounds.Min.X; x < bounds.Max.X; x++ {
originalColor := img.At(x, y)
r, g, b, a := originalColor.RGBA()
// Calculate distance from the nearest edge
distanceX := min(x, width-x)
distanceY := min(y, height-y)
distance := min(distanceX, distanceY)
// Calculate the effect intensity based on distance
intensity := float64(distance) / maxDistance
intensity = intensity * intensity // Square the intensity for a non-linear effect
// Apply the scorched effect by reducing brightness
scorchedColor := color.RGBA{
R: uint8(float64(r>>8) * intensity),
G: uint8(float64(g>>8) * intensity),
B: uint8(float64(b>>8) * intensity),
A: uint8(a >> 8),
}
img.Set(x, y, scorchedColor)
}
}
}
func abs(x int) int {
if x < 0 {
return -x
}
return x
}
func min(a, b int) int {
if a < b {
return a
}
return b
}
func handleNegativeImageCommand(s *discordgo.Session, m *discordgo.MessageCreate, imgURL string, intensityStr string) {
intensity, err := strconv.Atoi(intensityStr)
processedFile, err := processImage(imgURL, intensity)
if err != nil {
panic(err)
}
file, err := os.Open(processedFile)
if err != nil {
panic(err)
}
defer file.Close()
_, err = s.ChannelFileSend(m.ChannelID, processedFile, file)
if err != nil {
panic(err)
}
os.Remove(processedFile)
}
func processImage(url string, intensity int) (string, error) {
resp, err := http.Get(url)
if err != nil {
return "", err
}
defer resp.Body.Close()
img, format, err := image.Decode(resp.Body)
if err != nil {
return "", err
}
bounds := img.Bounds()
negImg := image.NewRGBA(bounds)
for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
for x := bounds.Min.X; x < bounds.Max.X; x++ {
originalColor := img.At(x, y)
r, g, b, a := originalColor.RGBA()
negColor := color.RGBA{
R: uint8(255 - (intensity*int(r>>8))/100),
G: uint8(255 - (intensity*int(g>>8))/100),
B: uint8(255 - (intensity*int(b>>8))/100),
A: uint8(a >> 8),
}
negImg.Set(x, y, negColor)
}
}
outputFile := "negative_image." + format
out, err := os.Create(outputFile)
if err != nil {
return "", err
}
defer out.Close()
switch format {
case "jpeg":
err = jpeg.Encode(out, negImg, nil)
case "png":
err = png.Encode(out, negImg)
default:
return "", fmt.Errorf("unsupported image format")
}
if err != nil {
return "", err
}
return outputFile, nil
}
Key Enhancements:
-
Bolder Lightning Lines: The addBolderLightningLines function now uses a loop to draw multiple lines close together, effectively thickening the appearance of the lightning.
-
Scorched Edges Effect: The applyScorchedEdgesEffect function calculates the color intensity based on the distance from the closest edge and adjusts pixel brightness, simulating a burnt effect as you move towards the edges of the image.
These simple techniques can be further refined to suit specific visual requirements, but they provide a solid starting point for achieving the desired image effects within a Discord bot.