104 lines
2.6 KiB
Go
104 lines
2.6 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"os/signal"
|
|
"syscall"
|
|
|
|
"database/sql"
|
|
"net/url"
|
|
|
|
"github.com/bwmarrin/discordgo"
|
|
"github.com/joho/godotenv"
|
|
_ "github.com/lib/pq"
|
|
)
|
|
|
|
//TODO: make the codebase less garbage
|
|
|
|
type Command struct {
|
|
Definition discordgo.ApplicationCommand
|
|
}
|
|
|
|
var db *sql.DB
|
|
|
|
func main() {
|
|
godotenv.Load()
|
|
|
|
var err error
|
|
connStr := "postgresql://" + os.Getenv("DB_USER") + ":" + url.QueryEscape(os.Getenv("DB_PASSWORD")) + "@" + os.Getenv("DB_SERVER") + ":" + string(os.Getenv("DB_PORT")) + "/" + os.Getenv("DB_NAME") + "?sslmode=disable"
|
|
db, err = sql.Open("postgres", connStr)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
discord, err := discordgo.New("Bot " + os.Getenv("BOT_TOKEN"))
|
|
if err != nil {
|
|
fmt.Println("error creating Discord session,", err)
|
|
return
|
|
} else {
|
|
fmt.Println("Discord session created")
|
|
}
|
|
discord.Identify.Intents = discordgo.IntentsGuildMessages | discordgo.IntentsGuilds
|
|
defer removeCommandFromAllGuilds(discord)
|
|
discord.AddHandler(ready)
|
|
discord.AddHandler(interactionCreate)
|
|
|
|
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)
|
|
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
|
|
<-sc
|
|
fmt.Println("\nShutting down...")
|
|
defer removeCommandFromAllGuilds(discord)
|
|
discord.Close()
|
|
}
|
|
|
|
func ready(s *discordgo.Session, event *discordgo.Ready) {
|
|
commands := []*discordgo.ApplicationCommand{
|
|
&tag_command.Definition,
|
|
&short_get_tag_command.Definition,
|
|
}
|
|
|
|
for _, guild := range event.Guilds {
|
|
for _, command := range commands {
|
|
_, err := s.ApplicationCommandCreate(s.State.User.ID, guild.ID, command)
|
|
if err != nil {
|
|
fmt.Println("error creating command,", err)
|
|
continue // Continue to the next guild
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func interactionCreate(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
|
switch i.ApplicationCommandData().Name {
|
|
case "tag":
|
|
tag_command.Interaction(s, i)
|
|
case "g":
|
|
short_get_tag_command.tInteraction(s, i)
|
|
}
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|
|
}
|
|
}
|