diff --git a/cmd_ask.go b/cmd_ask.go new file mode 100644 index 0000000..8fc3e62 --- /dev/null +++ b/cmd_ask.go @@ -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), + }, + }, + }, + }, + }) + }, +} diff --git a/cmd_dadjoke.go b/cmd_dadjoke.go new file mode 100644 index 0000000..42b7898 --- /dev/null +++ b/cmd_dadjoke.go @@ -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), + }, + }) + }, +} diff --git a/cmd_ping.go b/cmd_ping.go new file mode 100644 index 0000000..b080eca --- /dev/null +++ b/cmd_ping.go @@ -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]), + }, + }, + }, + }) + }, +} diff --git a/command_tag.go b/cmd_tag.go similarity index 100% rename from command_tag.go rename to cmd_tag.go diff --git a/command_dadjoke.go b/command_dadjoke.go deleted file mode 100644 index fe53f72..0000000 --- a/command_dadjoke.go +++ /dev/null @@ -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, - }, - }) - }, -} diff --git a/command_notify.go b/command_notify.go deleted file mode 100644 index e4e0379..0000000 --- a/command_notify.go +++ /dev/null @@ -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") - } - } - }, -} diff --git a/config.go b/config.go new file mode 100644 index 0000000..65d970c --- /dev/null +++ b/config.go @@ -0,0 +1,8 @@ +package main + +var color map[string]string = map[string]string{ + "red": "#FF6B6B", + "yellow": "#FFD93D", + "green": "#6BCB77", + "primary": "#4D96FF", +} diff --git a/gather_apis.go b/gather_apis.go new file mode 100644 index 0000000..66f861b --- /dev/null +++ b/gather_apis.go @@ -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] +} diff --git a/main.go b/main.go index 9d8ecba..48d36eb 100644 --- a/main.go +++ b/main.go @@ -5,6 +5,8 @@ import ( "log" "os" "os/signal" + "strconv" + "strings" "syscall" "database/sql" @@ -57,3 +59,13 @@ func main() { func int64Ptr(i int64) *int64 { 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) +} diff --git a/register_commands.go b/register_commands.go index 4f5f189..615f2a5 100644 --- a/register_commands.go +++ b/register_commands.go @@ -15,7 +15,7 @@ type Command struct { 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) { for _, guild := range event.Guilds {