This commit is contained in:
2024-02-21 08:29:06 +01:00
10 changed files with 161 additions and 117 deletions

34
cmd_ask.go Normal file
View File

@@ -0,0 +1,34 @@
package main
import "github.com/bwmarrin/discordgo"
var ask_command Command = Command{
Definition: discordgo.ApplicationCommand{
Name: "ask",
Description: "Ask anything and get a gif as response!",
Options: []*discordgo.ApplicationCommandOption{
{
Type: discordgo.ApplicationCommandOptionString,
Name: "question",
Description: "The question you want to ask",
Required: true,
},
},
},
Interact: func(s *discordgo.Session, i *discordgo.InteractionCreate) {
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Embeds: []*discordgo.MessageEmbed{
{
Type: discordgo.EmbedTypeImage,
Color: hexToDecimal(color["primary"]),
Image: &discordgo.MessageEmbedImage{
URL: simpleGetFromAPI("image", "https://yesno.wtf/api").(string),
},
},
},
},
})
},
}

20
cmd_dadjoke.go Normal file
View File

@@ -0,0 +1,20 @@
package main
import (
"github.com/bwmarrin/discordgo"
)
var dadjoke_command Command = Command{
Definition: discordgo.ApplicationCommand{
Name: "dadjoke",
Description: "Gives you a random joke that is as bad as your dad would tell them",
},
Interact: func(s *discordgo.Session, i *discordgo.InteractionCreate) {
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: simpleGetFromAPI("joke", "https://icanhazdadjoke.com/").(string),
},
})
},
}

53
cmd_ping.go Normal file
View File

@@ -0,0 +1,53 @@
package main
import (
"fmt"
"net/http"
"time"
"github.com/bwmarrin/discordgo"
)
var ping_command Command = Command{
Definition: discordgo.ApplicationCommand{
Name: "ping",
Description: "Returns the ping of the bot",
},
Interact: func(s *discordgo.Session, i *discordgo.InteractionCreate) {
start := time.Now()
client := http.Client{
Timeout: 5 * time.Second,
}
resp, err := client.Get("https://discord.com/api/v9/gateway/bot")
if err != nil {
fmt.Println("Error:", err)
return
}
defer resp.Body.Close()
ping := time.Since(start)
var ping_color string
if ping.Milliseconds() < 200 {
ping_color = "green"
} else if ping.Milliseconds() < 400 {
ping_color = "yellow"
} else {
ping_color = "red"
}
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Embeds: []*discordgo.MessageEmbed{
{
Title: s.State.User.Username + " ping",
Description: fmt.Sprintf("# %.2fms", ping.Seconds()*1000),
Type: discordgo.EmbedTypeArticle,
Color: hexToDecimal(color[ping_color]),
},
},
},
})
},
}

View File

@@ -1,53 +0,0 @@
package main
import (
"encoding/json"
"io"
"log"
"net/http"
"github.com/bwmarrin/discordgo"
)
var dadjoke_command Command = Command{
Definition: discordgo.ApplicationCommand{
Name: "dadjoke",
Description: "Gives you a random joke that is as bad as your dad would tell them",
},
Interact: func(s *discordgo.Session, i *discordgo.InteractionCreate) {
client := &http.Client{}
req, err := http.NewRequest("GET", "https://icanhazdadjoke.com/", nil)
if err != nil {
log.Println("Error creating request:", err)
return
}
req.Header.Set("Accept", "application/json")
resp, err := client.Do(req)
if err != nil {
log.Println("Error making request:", err)
return
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Println("Error reading response body:", err)
return
}
type Joke struct {
Joke string `json:"joke"`
}
var joke Joke
err = json.Unmarshal(body, &joke)
if err != nil {
log.Println("Error decoding JSON:", err)
return
}
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: joke.Joke,
},
})
},
}

View File

@@ -1,63 +0,0 @@
package main
import (
"fmt"
"github.com/bwmarrin/discordgo"
)
// disabled
var notify_command Command = Command{
Definition: discordgo.ApplicationCommand{
Name: "notify",
Description: "Manage social media notifications.",
Options: []*discordgo.ApplicationCommandOption{
{
Type: discordgo.ApplicationCommandOptionSubCommand,
Name: "add",
Description: "Set channels where your social media notifications should appear.",
Options: []*discordgo.ApplicationCommandOption{
{
Name: "platform",
Type: discordgo.ApplicationCommandOptionString,
Description: "The social media platform to receive notifications from.",
Required: true,
Choices: []*discordgo.ApplicationCommandOptionChoice{
{
Name: "Twitch",
Value: "twitch",
},
},
},
{
Name: "username",
Type: discordgo.ApplicationCommandOptionString,
Required: true,
Description: "The social media platform to receive notifications from.",
},
{
Name: "channel",
Type: discordgo.ApplicationCommandOptionChannel,
Required: true,
Description: "The social media platform to receive notifications from.",
},
},
},
{
Type: discordgo.ApplicationCommandOptionSubCommand,
Name: "remove",
Description: "Remove a social media notification.",
},
},
},
Interact: func(s *discordgo.Session, i *discordgo.InteractionCreate) {
options := i.ApplicationCommandData().Options[0]
switch options.Name {
case "add":
switch options.Options[0].Value {
case "twitch":
fmt.Print("twitch")
}
}
},
}

8
config.go Normal file
View File

@@ -0,0 +1,8 @@
package main
var color map[string]string = map[string]string{
"red": "#FF6B6B",
"yellow": "#FFD93D",
"green": "#6BCB77",
"primary": "#4D96FF",
}

33
gather_apis.go Normal file
View File

@@ -0,0 +1,33 @@
package main
import (
"encoding/json"
"io"
"log"
"net/http"
)
func simpleGetFromAPI(key string, url string) interface{} {
client := &http.Client{}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
log.Println("Error creating request:", err)
}
req.Header.Set("Accept", "application/json")
resp, err := client.Do(req)
if err != nil {
log.Println("Error making request:", err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Println("Error reading response body:", err)
}
var result map[string]interface{}
err = json.Unmarshal(body, &result)
if err != nil {
log.Println("Error decoding JSON:", err)
}
return result[key]
}

12
main.go
View File

@@ -5,6 +5,8 @@ import (
"log" "log"
"os" "os"
"os/signal" "os/signal"
"strconv"
"strings"
"syscall" "syscall"
"database/sql" "database/sql"
@@ -57,3 +59,13 @@ func main() {
func int64Ptr(i int64) *int64 { func int64Ptr(i int64) *int64 {
return &i return &i
} }
func hexToDecimal(hexColor string) int {
// Remove the hash symbol if it's present
hexColor = strings.TrimPrefix(hexColor, "#")
decimal, err := strconv.ParseInt(hexColor, 16, 64)
if err != nil {
return 0
}
return int(decimal)
}

View File

@@ -15,7 +15,7 @@ type Command struct {
ModalID string ModalID string
} }
var commands []Command = []Command{tag_command, short_get_tag_command, dadjoke_command, ping_command} var commands []Command = []Command{tag_command, short_get_tag_command, dadjoke_command, ping_command, ask_command}
func ready(s *discordgo.Session, event *discordgo.Ready) { func ready(s *discordgo.Session, event *discordgo.Ready) {
for _, guild := range event.Guilds { for _, guild := range event.Guilds {