Simple bot
This commit is contained in:
parent
6cf715e620
commit
b63a368c35
41
main.go
41
main.go
@ -2,11 +2,16 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"os/signal"
|
||||||
|
"syscall"
|
||||||
|
|
||||||
"github.com/BurntSushi/toml"
|
"github.com/BurntSushi/toml"
|
||||||
"github.com/bwmarrin/discordgo"
|
"github.com/bwmarrin/discordgo"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var client *discordgo.Session
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Token string `toml:"token"`
|
Token string `toml:"token"`
|
||||||
GuildID string `toml:"guildId"`
|
GuildID string `toml:"guildId"`
|
||||||
@ -18,17 +23,43 @@ func loadConfig(filename string) (Config, error) {
|
|||||||
return config, err
|
return config, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func main(){
|
func init() {
|
||||||
config, err := loadConfig("config.toml")
|
config, err := loadConfig("config.toml")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Error occured whilst trying to load config: ", err)
|
fmt.Println("Error occurred whilst trying to load config:", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
client, err := discordgo.New("Bot " + config.Token)
|
client, err = discordgo.New("Bot " + config.Token)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Error initalizing bot: ", err)
|
fmt.Println("Error initializing bot:", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer client.Close()
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
if client == nil {
|
||||||
|
fmt.Println("Bot client is not initialized")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
client.AddHandler(func(client *discordgo.Session, r *discordgo.Ready) {
|
||||||
|
fmt.Println("Bot is online")
|
||||||
|
})
|
||||||
|
|
||||||
|
err := client.Open()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Error opening connection:", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
stop := make(chan os.Signal, 1)
|
||||||
|
signal.Notify(stop, os.Interrupt, syscall.SIGTERM)
|
||||||
|
<-stop
|
||||||
|
|
||||||
|
fmt.Println("Gracefully shutting down.")
|
||||||
|
client.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user