From 26338f6b16f84430aa8e0318c3021353f8e4b423 Mon Sep 17 00:00:00 2001 From: darius Date: Fri, 3 May 2024 17:39:11 +0200 Subject: [PATCH] init command added Delete command added --- commands.go | 38 ++++++++++++++++++++++++ handlers.go | 80 +++++++++++++++++++++++++++++++++++++++++++++++++++ initServer.go | 43 +++++++++++++++++++++++++++ main.go | 29 ++++++++++++++++++- readJson.go | 28 ++++++++++++++++++ 5 files changed, 217 insertions(+), 1 deletion(-) create mode 100644 commands.go create mode 100644 handlers.go create mode 100644 initServer.go create mode 100644 readJson.go diff --git a/commands.go b/commands.go new file mode 100644 index 0000000..df83968 --- /dev/null +++ b/commands.go @@ -0,0 +1,38 @@ +package main + +import "github.com/bwmarrin/discordgo" + +var commands = []*discordgo.ApplicationCommand{ + { + Name: "test1", + Description: "Showcase of a basic slash command", + }, + { + Name: "init", + Description: "init things", + }, + { + Name: "read_back", + Description: "Showcase of a basic slash command2", + Options: []*discordgo.ApplicationCommandOption{ + { + Type: discordgo.ApplicationCommandOptionString, + Name: "text", + Description: "text to read back", + Required: true, + }, + }, + }, + { + Name: "delete", + Description: "delete x amount recent messages in this channel", + Options: []*discordgo.ApplicationCommandOption{ + { + Type: discordgo.ApplicationCommandOptionString, + Name: "amount", + Description: "max 100", + Required: true, + }, + }, + }, +} diff --git a/handlers.go b/handlers.go new file mode 100644 index 0000000..68e406f --- /dev/null +++ b/handlers.go @@ -0,0 +1,80 @@ +package main + +import ( + "github.com/bwmarrin/discordgo" + "strconv" +) + +func MessageHandler(s *discordgo.Session, m *discordgo.MessageCreate) { + + if m.Author.ID == s.State.User.ID { + return + } + if m.Content == "test" { + s.ChannelMessageSend(m.ChannelID, "werkt") + } +} + +func CommandHandler(s *discordgo.Session, i *discordgo.InteractionCreate) { + data := i.ApplicationCommandData() + switch data.Name { + case "init": + AddServer(i.GuildID, s) + case "test1": + err := s.InteractionRespond( + i.Interaction, + &discordgo.InteractionResponse{ + Type: discordgo.InteractionResponseChannelMessageWithSource, + Data: &discordgo.InteractionResponseData{ + TTS: true, + Content: "Hello world!", + }, + }, + ) + if err != nil { + // Handle the error + } + case "read_back": + err := s.InteractionRespond( + i.Interaction, + &discordgo.InteractionResponse{ + Type: discordgo.InteractionResponseChannelMessageWithSource, + Data: &discordgo.InteractionResponseData{ + TTS: true, + Content: data.Options[0].Value.(string), + }, + }, + ) + if err != nil { + // Handle the error + } + case "delete": + amount, _ := strconv.Atoi(data.Options[0].Value.(string)) + if amount > 100 { + amount = 100 + } + messages, err := s.ChannelMessages(i.ChannelID, amount, "", "", "") + if err != nil { + return + } + err = s.InteractionRespond( + i.Interaction, + &discordgo.InteractionResponse{ + Type: discordgo.InteractionResponseChannelMessageWithSource, + Data: &discordgo.InteractionResponseData{ + TTS: true, + Content: "deleting " + strconv.Itoa(len(messages)) + " messages", + }, + }, + ) + message, err := s.ChannelMessageSend(i.ChannelID, "Please hold....") + if err != nil { + // Handle the error + } + for _, message := range messages { + s.ChannelMessageDelete(i.ChannelID, message.ID) + println(message.Content + " deleted") + } + s.ChannelMessageEdit(i.ChannelID, message.ID, strconv.Itoa(len(messages))+" Message deleted") + } +} diff --git a/initServer.go b/initServer.go new file mode 100644 index 0000000..273c37b --- /dev/null +++ b/initServer.go @@ -0,0 +1,43 @@ +package main + +import ( + "encoding/json" + "github.com/bwmarrin/discordgo" + "os" +) + +type Server struct { + Name string `json:"name"` + GuildID string `json:"GuildID"` + Webhooks []*discordgo.Webhook `json:"Webhooks"` +} + +func AddServer(GuildID string, s *discordgo.Session) Server { + servers := readJson() + preview, err := s.GuildPreview(GuildID) + if err != nil { + } + Webhooks, _ := s.GuildWebhooks(GuildID) + + server := Server{ + Name: preview.Name, + GuildID: GuildID, + Webhooks: Webhooks, + } + var exists bool + + for i, oldServer := range servers { + if oldServer.GuildID == server.GuildID { + servers[i] = server + exists = true + break + } + } + if !exists { + servers = append(servers, server) + } + + rankingsJson, _ := json.Marshal(servers) + err = os.WriteFile("servers.json", rankingsJson, 0644) + return server +} diff --git a/main.go b/main.go index adc776c..ca57149 100644 --- a/main.go +++ b/main.go @@ -1,13 +1,21 @@ package main import ( + "flag" "github.com/bwmarrin/discordgo" "github.com/joho/godotenv" "log" "os" + "os/signal" ) -func init() { +var ( + GuildID = flag.String("guild", "", "Test guild ID. If not passed - bot registers commands globally") + discord *discordgo.Session +) + +func main() { + flag.Parse() err := godotenv.Load() if err != nil { log.Fatalf(".env not found: %v", err) @@ -19,4 +27,23 @@ func init() { log.Fatalf(err.Error()) } + discord.AddHandler(MessageHandler) + discord.AddHandler(CommandHandler) + + err = discord.Open() + if err != nil { + log.Fatalf("Cannot open the session: %v", err) + } + + _, err = discord.ApplicationCommandBulkOverwrite(discord.State.User.ID, *GuildID, commands) + if err != nil { + print(err.Error()) + } + + defer discord.Close() + + stop := make(chan os.Signal, 1) + signal.Notify(stop, os.Interrupt) + log.Println("Press Ctrl+C to exit") + <-stop } diff --git a/readJson.go b/readJson.go new file mode 100644 index 0000000..ba9e1d1 --- /dev/null +++ b/readJson.go @@ -0,0 +1,28 @@ +package main + +import ( + "encoding/json" + "fmt" + "io" + "os" +) + +func readJson() []Server { + // Open our jsonFile + jsonFile, err := os.Open("servers.json") + // if we os.Open returns an error then handle it + if err != nil { + fmt.Println(err) + } + fmt.Println("Successfully Opened users.json") + // defer the closing of our jsonFile so that we can parse it later on + defer jsonFile.Close() + + byteValue, _ := io.ReadAll(jsonFile) + + var servers []Server + + json.Unmarshal(byteValue, &servers) + + return servers +}