2024-07-06 16:18:05 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-07-06 17:27:04 +02:00
|
|
|
"database/sql"
|
2024-07-06 16:18:05 +02:00
|
|
|
"fmt"
|
2024-07-06 17:16:13 +02:00
|
|
|
"log"
|
2024-07-06 16:55:30 +02:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
2024-07-06 17:37:07 +02:00
|
|
|
"strings"
|
2024-07-06 18:06:55 +02:00
|
|
|
"syscall"
|
2024-07-06 16:55:30 +02:00
|
|
|
|
2024-07-06 16:29:25 +02:00
|
|
|
"github.com/BurntSushi/toml"
|
|
|
|
"github.com/bwmarrin/discordgo"
|
2024-07-06 17:16:13 +02:00
|
|
|
_ "github.com/lib/pq"
|
2024-07-06 16:18:05 +02:00
|
|
|
)
|
|
|
|
|
2024-07-06 17:16:13 +02:00
|
|
|
var (
|
2024-07-06 17:27:04 +02:00
|
|
|
config Config
|
|
|
|
client *discordgo.Session
|
|
|
|
db *sql.DB
|
2024-07-06 17:16:13 +02:00
|
|
|
)
|
2024-07-06 16:55:30 +02:00
|
|
|
|
2024-07-06 16:29:25 +02:00
|
|
|
type Config struct {
|
2024-07-06 16:59:12 +02:00
|
|
|
Discord Discord `toml:"discord"`
|
|
|
|
Database Database `toml:"database"`
|
2024-07-06 16:29:25 +02:00
|
|
|
}
|
|
|
|
|
2024-07-06 16:59:12 +02:00
|
|
|
type Discord struct {
|
2024-07-06 17:52:22 +02:00
|
|
|
Token string `toml:"token"`
|
2024-07-06 18:06:55 +02:00
|
|
|
AppID string `toml:"appid"`
|
2024-07-06 16:59:12 +02:00
|
|
|
GuildID string `toml:"guildid"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Database struct {
|
|
|
|
Host string `toml:"host"`
|
|
|
|
Port int `toml:"port"`
|
|
|
|
Name string `toml:"name"`
|
|
|
|
Username string `toml:"username"`
|
|
|
|
Password string `toml:"password"`
|
|
|
|
}
|
|
|
|
|
2024-07-06 16:29:25 +02:00
|
|
|
func loadConfig(filename string) (Config, error) {
|
|
|
|
var config Config
|
|
|
|
_, err := toml.DecodeFile(filename, &config)
|
|
|
|
return config, err
|
|
|
|
}
|
|
|
|
|
2024-07-06 17:27:04 +02:00
|
|
|
func connectDb(config Config) (*sql.DB, error) {
|
2024-07-06 17:16:13 +02:00
|
|
|
connectionString := fmt.Sprintf(
|
|
|
|
"host=%s port=%d user=%s password=%s dbname=%s sslmode=disable",
|
|
|
|
config.Database.Host,
|
|
|
|
config.Database.Port,
|
|
|
|
config.Database.Username,
|
|
|
|
config.Database.Password,
|
|
|
|
config.Database.Name,
|
|
|
|
)
|
|
|
|
|
|
|
|
db, err := sql.Open("postgres", connectionString)
|
|
|
|
if err != nil {
|
2024-07-06 17:27:04 +02:00
|
|
|
return nil, fmt.Errorf("error connecting to the database: %v", err)
|
2024-07-06 17:16:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
err = db.Ping()
|
|
|
|
if err != nil {
|
2024-07-06 17:27:04 +02:00
|
|
|
db.Close()
|
|
|
|
return nil, fmt.Errorf("error pinging the database: %v", err)
|
2024-07-06 17:16:13 +02:00
|
|
|
}
|
2024-07-06 17:27:04 +02:00
|
|
|
|
|
|
|
log.Println("Successfully connected to the database.")
|
|
|
|
return db, nil
|
2024-07-06 17:16:13 +02:00
|
|
|
}
|
|
|
|
|
2024-07-06 16:55:30 +02:00
|
|
|
func init() {
|
2024-07-06 17:16:13 +02:00
|
|
|
var err error
|
|
|
|
config, err = loadConfig("config.toml")
|
2024-07-06 16:29:25 +02:00
|
|
|
if err != nil {
|
2024-07-06 17:16:13 +02:00
|
|
|
log.Println("Error occurred whilst trying to load config:", err)
|
2024-07-06 16:29:25 +02:00
|
|
|
return
|
|
|
|
}
|
2024-07-06 16:55:30 +02:00
|
|
|
|
2024-07-06 16:59:12 +02:00
|
|
|
client, err = discordgo.New("Bot " + config.Discord.Token)
|
2024-07-06 16:29:25 +02:00
|
|
|
if err != nil {
|
2024-07-06 17:16:13 +02:00
|
|
|
log.Println("Error initializing bot:", err)
|
2024-07-06 16:29:25 +02:00
|
|
|
return
|
|
|
|
}
|
2024-07-06 16:55:30 +02:00
|
|
|
|
2024-07-06 17:27:04 +02:00
|
|
|
db, err = connectDb(config)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Error initializing db connection:", err)
|
|
|
|
return
|
|
|
|
}
|
2024-07-06 16:18:05 +02:00
|
|
|
}
|
2024-07-06 16:55:30 +02:00
|
|
|
|
2024-07-06 17:37:07 +02:00
|
|
|
func searchLink(message, format, sep string) string {
|
|
|
|
return fmt.Sprintf(format, strings.Join(
|
|
|
|
strings.Split(
|
|
|
|
message,
|
|
|
|
" ",
|
|
|
|
),
|
|
|
|
sep,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
commands = []discordgo.ApplicationCommand{
|
|
|
|
{
|
|
|
|
Name: "rickroll-em",
|
2024-07-06 18:06:55 +02:00
|
|
|
Description: "List all webhooks in the server",
|
2024-07-06 17:37:07 +02:00
|
|
|
},
|
|
|
|
}
|
2024-07-06 17:52:22 +02:00
|
|
|
commandsHandlers = map[string]func(client *discordgo.Session, i *discordgo.InteractionCreate){
|
|
|
|
"rickroll-em": func(client *discordgo.Session, i *discordgo.InteractionCreate) {
|
|
|
|
err := client.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
2024-07-06 17:37:07 +02:00
|
|
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
|
|
|
Data: &discordgo.InteractionResponseData{
|
|
|
|
Content: "Operation rickroll has begun",
|
|
|
|
Flags: discordgo.MessageFlagsEphemeral,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2024-07-06 17:52:22 +02:00
|
|
|
ch, err := client.UserChannelCreate(
|
2024-07-06 17:37:07 +02:00
|
|
|
i.ApplicationCommandData().TargetID,
|
|
|
|
)
|
|
|
|
if err != nil {
|
2024-07-06 17:52:22 +02:00
|
|
|
_, err = client.FollowupMessageCreate(i.Interaction, true, &discordgo.WebhookParams{
|
2024-07-06 17:37:07 +02:00
|
|
|
Content: fmt.Sprintf("Mission failed. Cannot send a message to this user: %q", err.Error()),
|
|
|
|
Flags: discordgo.MessageFlagsEphemeral,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
2024-07-06 17:52:22 +02:00
|
|
|
_, err = client.ChannelMessageSend(
|
2024-07-06 17:37:07 +02:00
|
|
|
ch.ID,
|
|
|
|
fmt.Sprintf("%s sent you this: https://youtu.be/dQw4w9WgXcQ", i.Member.Mention()),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2024-07-06 16:55:30 +02:00
|
|
|
func main() {
|
|
|
|
if client == nil {
|
2024-07-06 17:16:13 +02:00
|
|
|
log.Println("Bot client is not initialized")
|
2024-07-06 16:55:30 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
client.AddHandler(func(client *discordgo.Session, r *discordgo.Ready) {
|
2024-07-06 17:16:13 +02:00
|
|
|
log.Println("Bot is online")
|
2024-07-06 18:06:55 +02:00
|
|
|
|
|
|
|
cmdIDs := make(map[string]string, len(commands))
|
|
|
|
|
|
|
|
for _, cmd := range commands {
|
|
|
|
fmt.Println(cmd)
|
|
|
|
rcmd, err := client.ApplicationCommandCreate(client.State.User.ID, config.Discord.GuildID, &cmd)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Cannot create slash command %q: %v", cmd.Name, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cmdIDs[rcmd.ID] = rcmd.Name
|
|
|
|
}
|
2024-07-06 16:55:30 +02:00
|
|
|
})
|
|
|
|
|
2024-07-06 17:37:07 +02:00
|
|
|
client.AddHandler(func(client *discordgo.Session, i *discordgo.InteractionCreate) {
|
|
|
|
if h, ok := commandsHandlers[i.ApplicationCommandData().Name]; ok {
|
|
|
|
h(client, i)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2024-07-06 16:55:30 +02:00
|
|
|
err := client.Open()
|
|
|
|
if err != nil {
|
2024-07-06 17:16:13 +02:00
|
|
|
log.Println("Error opening connection:", err)
|
2024-07-06 16:55:30 +02:00
|
|
|
return
|
2024-07-06 17:27:04 +02:00
|
|
|
}
|
2024-07-06 16:55:30 +02:00
|
|
|
|
|
|
|
stop := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(stop, os.Interrupt, syscall.SIGTERM)
|
|
|
|
<-stop
|
|
|
|
|
2024-07-06 17:16:13 +02:00
|
|
|
log.Println("Gracefully shutting down.")
|
2024-07-06 16:55:30 +02:00
|
|
|
client.Close()
|
2024-07-06 17:27:04 +02:00
|
|
|
|
|
|
|
if db != nil {
|
|
|
|
db.Close()
|
|
|
|
}
|
2024-07-06 16:55:30 +02:00
|
|
|
}
|
|
|
|
|