added test commands

This commit is contained in:
2024-02-14 11:31:58 +01:00
parent 6bc10a131c
commit a05c6c5876
3 changed files with 105 additions and 19 deletions

View File

@@ -1 +1 @@
BOT_TOKEN = "" BOT_TOKEN="<Your bot token>"

7
go.mod
View File

@@ -3,9 +3,12 @@ module github.com/vaporvee/tag-bot
go 1.21.6 go 1.21.6
require ( require (
github.com/bwmarrin/discordgo v0.27.1 // indirect github.com/bwmarrin/discordgo v0.27.1
github.com/joho/godotenv v1.5.1
)
require (
github.com/gorilla/websocket v1.4.2 // indirect github.com/gorilla/websocket v1.4.2 // indirect
github.com/joho/godotenv v1.5.1 // indirect
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b // indirect golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b // indirect
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 // indirect golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 // indirect
) )

113
main.go
View File

@@ -2,7 +2,6 @@ package main
import ( import (
"fmt" "fmt"
"log"
"os" "os"
"os/signal" "os/signal"
"syscall" "syscall"
@@ -12,29 +11,113 @@ import (
) )
func main() { func main() {
err := godotenv.Load() godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file: ", err)
}
discord, err := discordgo.New("Bot " + os.Getenv("BOT_TOKEN")) discord, err := discordgo.New("Bot " + os.Getenv("BOT_TOKEN"))
log.Println(err) if err != nil {
fmt.Println("error creating Discord session,", err)
return
} else {
fmt.Println("Discord session created")
}
discord.Identify.Intents = discordgo.IntentsGuildMessages | discordgo.IntentsGuilds discord.Identify.Intents = discordgo.IntentsGuildMessages | discordgo.IntentsGuilds
discord.AddHandler(messageCreate) defer removeCommandFromAllGuilds(discord)
discord.AddHandler(ready)
discord.AddHandler(interactionCreate)
fmt.Println("Bot is now running. Press CTRL-C to exit.") err = discord.Open()
if err != nil {
fmt.Println("error opening connection,", err)
return
}
fmt.Printf("Bot is now running as \"%s\"!", discord.State.User.Username)
sc := make(chan os.Signal, 1) sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt) signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
<-sc <-sc
fmt.Println("\nShutting down...")
defer removeCommandFromAllGuilds(discord)
discord.Close() discord.Close()
} }
func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { func ready(s *discordgo.Session, event *discordgo.Ready) {
if m.Author.ID == s.State.User.ID { //bot doesn't take its own messages commands := []*discordgo.ApplicationCommand{
return {
Name: "test",
Description: "A test command.",
},
{
Name: "secondtest",
Description: "A second test command.",
Options: []*discordgo.ApplicationCommandOption{
{
Type: discordgo.ApplicationCommandOptionString,
Name: "query",
Description: "The query to search for.",
Required: true,
},
},
},
} }
if m.Message.Content == "test" {
s.ChannelMessageSendEmbed(m.ChannelID, &discordgo.MessageEmbed{ for _, guild := range event.Guilds {
Title: "TESTED WOOOWW", _, err := s.ApplicationCommandCreate(s.State.User.ID, guild.ID, commands[0])
if err != nil {
fmt.Println("error creating command,", err)
continue // Continue to the next guild
}
_, err = s.ApplicationCommandCreate(s.State.User.ID, guild.ID, commands[1])
if err != nil {
fmt.Println("error creating command,", err)
continue // Continue to the next guild
}
}
}
func interactionCreate(s *discordgo.Session, i *discordgo.InteractionCreate) {
if i.Type == discordgo.InteractionApplicationCommand {
if i.ApplicationCommandData().Name == "test" {
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "You tested me!",
},
})
}
if i.ApplicationCommandData().Name == "secondtest" {
// Check if the command has options
if len(i.ApplicationCommandData().Options) > 0 {
// Loop through the options and handle them
for _, option := range i.ApplicationCommandData().Options {
switch option.Name {
case "query":
value := option.Value.(string)
response := fmt.Sprintf("You provided the query: %s", value)
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: response,
},
}) })
} }
} }
}
}
}
}
func removeCommandFromAllGuilds(s *discordgo.Session) {
for _, guild := range s.State.Guilds {
existingCommands, err := s.ApplicationCommands(s.State.User.ID, guild.ID)
if err != nil {
fmt.Printf("error fetching existing commands for guild %s: %v\n", guild.Name, err)
continue
}
for _, existingCommand := range existingCommands {
err := s.ApplicationCommandDelete(s.State.User.ID, guild.ID, existingCommand.ID)
if err != nil {
fmt.Printf("error deleting command %s for guild %s: %v\n", existingCommand.Name, guild.Name, err)
}
}
}
}