initial code commit

This commit is contained in:
2025-03-23 13:38:56 -04:00
parent aa1889f6fe
commit edf72fe289
16 changed files with 427 additions and 0 deletions
+24
View File
@@ -0,0 +1,24 @@
package commands
import "github.com/bwmarrin/discordgo"
var BoopCommand = &discordgo.ApplicationCommand{
Name: "boop",
Description: "Say Boop!",
}
func HandleBoop(s *discordgo.Session, i *discordgo.InteractionCreate) {
var username string
if i.Member != nil {
username = i.Member.User.Username
} else if i.User != nil {
username = i.User.Username
}
response := &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: username + ", Boop!",
},
}
s.InteractionRespond(i.Interaction, response)
}
+61
View File
@@ -0,0 +1,61 @@
package commands
import (
"encoding/json"
"io/ioutil"
"math/rand"
"net/http"
"time"
helpers "ion606_bot/Bot/Helpers"
"github.com/bwmarrin/discordgo"
)
var CatfactCommand = &discordgo.ApplicationCommand{
Name: "catfact",
Description: "Fetches a random cat fact",
}
type CatFactAPIResponse struct {
Facts []struct {
FactNumber int `json:"fact_number"`
Fact string `json:"fact"`
} `json:"facts"`
}
func HandleCatfact(s *discordgo.Session, i *discordgo.InteractionCreate) {
client := http.Client{
Timeout: 10 * time.Second,
}
resp, err := client.Get("https://www.catfacts.net/api/")
if err != nil {
helpers.HandleError(s, i, err)
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
helpers.HandleError(s, i, err)
return
}
var result CatFactAPIResponse
err = json.Unmarshal(body, &result)
if err != nil || len(result.Facts) == 0 {
helpers.HandleError(s, i, err)
return
}
// Pick a random fact from the array.
rand.Seed(time.Now().UnixNano())
randomFact := result.Facts[rand.Intn(len(result.Facts))].Fact
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: randomFact,
},
})
}
+18
View File
@@ -0,0 +1,18 @@
package commands
import "github.com/bwmarrin/discordgo"
var CuddleCommand = &discordgo.ApplicationCommand{
Name: "cuddle",
Description: "Gently cuddle for your day!",
}
func HandleCuddle(s *discordgo.Session, i *discordgo.InteractionCreate) {
response := &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "Here's a gentle cuddle for your day! 💕",
},
}
s.InteractionRespond(i.Interaction, response)
}
+18
View File
@@ -0,0 +1,18 @@
package commands
import "github.com/bwmarrin/discordgo"
var HugCommand = &discordgo.ApplicationCommand{
Name: "hug",
Description: "Sends you a big warm hug!",
}
func HandleHug(s *discordgo.Session, i *discordgo.InteractionCreate) {
response := &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "Sending you a big warm hug! 🤗",
},
}
s.InteractionRespond(i.Interaction, response)
}
+63
View File
@@ -0,0 +1,63 @@
package commands
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"time"
Helpers "ion606_bot/Bot/Helpers"
"github.com/bwmarrin/discordgo"
)
var MeowCommand = &discordgo.ApplicationCommand{
Name: "meow",
Description: "Sends a random cat image",
}
type SefinekAPIResponse struct {
Success bool `json:"success"`
Status int `json:"status"`
Info struct {
Category string `json:"category"`
Endpoint string `json:"endpoint"`
} `json:"info"`
Message string `json:"message"`
}
func HandleMeow(s *discordgo.Session, i *discordgo.InteractionCreate) {
client := http.Client{
Timeout: 10 * time.Second,
}
resp, err := client.Get("https://api.sefinek.net/api/v2/random/animal/cat")
if err != nil {
Helpers.HandleError(s, i, fmt.Errorf("fetching cat image: %w", err))
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
Helpers.HandleError(s, i, fmt.Errorf("reading cat image response: %w", err))
return
}
var apiResp SefinekAPIResponse
err = json.Unmarshal(body, &apiResp)
if err != nil || !apiResp.Success {
Helpers.HandleError(s, i, fmt.Errorf("parsing cat image response"))
return
}
// Use the "message" field which contains the image URL.
imageURL := apiResp.Message
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: imageURL,
},
})
}
+18
View File
@@ -0,0 +1,18 @@
package commands
import "github.com/bwmarrin/discordgo"
var PurrCommand = &discordgo.ApplicationCommand{
Name: "purr",
Description: "Purr...",
}
func HandlePurr(s *discordgo.Session, i *discordgo.InteractionCreate) {
response := &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "Purr...",
},
}
s.InteractionRespond(i.Interaction, response)
}
+18
View File
@@ -0,0 +1,18 @@
package commands
import "github.com/bwmarrin/discordgo"
var SnuggleCommand = &discordgo.ApplicationCommand{
Name: "snuggle",
Description: "Time to snuggle up and feel cozy!",
}
func HandleSnuggle(s *discordgo.Session, i *discordgo.InteractionCreate) {
response := &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "Time to snuggle up and feel cozy! 🥰",
},
}
s.InteractionRespond(i.Interaction, response)
}
+18
View File
@@ -0,0 +1,18 @@
package helpers
import (
"fmt"
"github.com/bwmarrin/discordgo"
)
// HandleError sends an error response including the error text and an emoji.
func HandleError(s *discordgo.Session, i *discordgo.InteractionCreate, err error) {
content := fmt.Sprintf("Error: %s <:error:1353407137421721620>", err.Error())
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: content,
},
})
}
+72
View File
@@ -0,0 +1,72 @@
package bot
import (
"fmt"
"log"
"os"
"os/signal"
commands "ion606_bot/Bot/Commands"
"github.com/bwmarrin/discordgo"
)
var BotToken string
// commandHandlers maps command names to their interaction handler functions.
var commandHandlers = map[string]func(*discordgo.Session, *discordgo.InteractionCreate){
"meow": commands.HandleMeow,
"purr": commands.HandlePurr,
"boop": commands.HandleBoop,
"hug": commands.HandleHug,
"cuddle": commands.HandleCuddle,
"snuggle": commands.HandleSnuggle,
"catfact": commands.HandleCatfact,
}
// Run starts the Discord bot session and listens for both message and slash command events.
func Run() {
// create a session using the provided bot token.
discord, err := discordgo.New("Bot " + BotToken)
if err != nil {
log.Fatal("Error creating Discord session: ", err)
}
// add event handlers for messages and interactions.
discord.AddHandler(newMessage)
discord.AddHandler(handleInteractionCreate)
err = discord.Open()
if err != nil {
log.Fatal("Error opening Discord session: ", err)
}
RegisterCommands(discord, "")
fmt.Println("Bot running....")
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
<-c
err = discord.Close()
if err != nil {
log.Println("Error closing Discord session: ", err)
}
}
func newMessage(s *discordgo.Session, m *discordgo.MessageCreate) {
// prevent the bot from responding to its own messages.
if m.Author.ID == s.State.User.ID {
return
}
// additional message handling logic can go here.
}
func handleInteractionCreate(s *discordgo.Session, i *discordgo.InteractionCreate) {
name := i.ApplicationCommandData().Name
if handler, found := commandHandlers[name]; found {
handler(s, i)
} else {
log.Printf("No handler found for command: %v", name)
}
}
+33
View File
@@ -0,0 +1,33 @@
package bot
import (
"log"
Commands "ion606_bot/Bot/Commands"
helpers "ion606_bot/Bot/Helpers"
"github.com/bwmarrin/discordgo"
)
var commandsList = []*discordgo.ApplicationCommand{
Commands.MeowCommand,
Commands.PurrCommand,
Commands.BoopCommand,
Commands.HugCommand,
Commands.CuddleCommand,
Commands.SnuggleCommand,
Commands.CatfactCommand,
}
func RegisterCommands(s *discordgo.Session, guildID string) {
for _, cmd := range commandsList {
_, err := s.ApplicationCommandCreate(s.State.User.ID, guildID, cmd)
if err != nil {
log.Printf("Cannot create '%v' command: %v", cmd.Name, err)
// Handle error using HandleError
helpers.HandleError(s, nil, err)
} else {
log.Printf("Registered command: %v", cmd.Name)
}
}
}