Added Stop command

improved init command
improved error handler
This commit is contained in:
darius 2024-05-03 22:37:29 +02:00
parent 62e7283f47
commit 1592192b01
5 changed files with 61 additions and 4 deletions

View File

@ -41,4 +41,8 @@ var Commands = []*discordgo.ApplicationCommand{
},
},
},
{
Name: "stop",
Description: "stop bot",
},
}

View File

@ -1,8 +1,10 @@
package handlers
import (
"fmt"
"github.com/bwmarrin/discordgo"
"kleincordBot/services"
"log"
)
func ReadBackCommand(data discordgo.ApplicationCommandInteractionData, s *discordgo.Session, i *discordgo.InteractionCreate) {
@ -32,3 +34,46 @@ func Test1Command(s *discordgo.Session, i *discordgo.InteractionCreate) {
services.HandleError(err, s)
}
}
func StopCommand(s *discordgo.Session, i *discordgo.InteractionCreate) {
s.InteractionRespond(
i.Interaction,
&discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "bye",
},
},
)
log.Fatalf("stop command")
}
func InitCommand(s *discordgo.Session, i *discordgo.InteractionCreate) {
var serverListString string
for i, server := range services.ServerList {
serverListString = serverListString + fmt.Sprintf("\n %d) ", i+1) + server.Name
}
err := s.InteractionRespond(
i.Interaction,
&discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Embeds: []*discordgo.MessageEmbed{
{
Title: "Servers",
Fields: []*discordgo.MessageEmbedField{
{
Value: serverListString,
},
},
Type: discordgo.EmbedTypeArticle,
},
},
},
},
)
if err != nil {
services.HandleError(err, s)
}
}

View File

@ -1 +0,0 @@
package handlers

View File

@ -11,11 +11,14 @@ func CommandRouter(s *discordgo.Session, i *discordgo.InteractionCreate) {
switch data.Name {
case "init":
services.AddServer(i.GuildID, s)
handlers.InitCommand(s, i)
case "test1":
handlers.Test1Command(s, i)
case "read_back":
handlers.ReadBackCommand(data, s, i)
case "delete":
handlers.DeleteCommand(data, s, i)
case "stop":
handlers.StopCommand(s, i)
}
}

View File

@ -1,16 +1,19 @@
package services
import (
"fmt"
"github.com/bwmarrin/discordgo"
"strconv"
"time"
)
func HandleError(err error, s *discordgo.Session) {
s.ChannelMessageSendComplex(strconv.Itoa(1236038688627101749), &discordgo.MessageSend{
_, err = s.ChannelMessageSendComplex(strconv.Itoa(1236038688627101749), &discordgo.MessageSend{
Content: "@here",
Embed: &discordgo.MessageEmbed{
Title: "Error",
Color: 0xff0000,
Timestamp: time.Now().Format(time.RFC3339),
Fields: []*discordgo.MessageEmbedField{
{
Value: "```" + err.Error() + "```",
@ -18,4 +21,7 @@ func HandleError(err error, s *discordgo.Session) {
},
},
})
if err != nil {
fmt.Println(err)
}
}