-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
71 lines (58 loc) · 1.24 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package main
import (
"fmt"
"log"
"os"
"os/signal"
"syscall"
"github.com/bwmarrin/discordgo"
"github.com/joho/godotenv"
)
var (
Token string
)
func init() {
// Load .env file
err := godotenv.Load()
if err != nil {
log.Println("Warning: Error loading .env file:", err)
}
// Get token from environment
Token = os.Getenv("BOT_TOKEN")
if Token == "" {
log.Fatalln("No token provided. Please set BOT_TOKEN in your .env file")
}
}
func main() {
// Create a new Discord session
dg, err := discordgo.New("Bot " + Token)
if err != nil {
fmt.Println("Error creating Discord session:", err)
return
}
// Register handlers
dg.AddHandler(messageCreate)
// Open websocket connection
err = dg.Open()
if err != nil {
fmt.Println("Error opening connection:", err)
return
}
fmt.Println("Bot is running. Press CTRL-C to exit.")
// Wait for CTRL-C
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM)
<-sc
// Clean close
dg.Close()
}
func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
// Ignore messages from the bot itself
if m.Author.ID == s.State.User.ID {
return
}
// Example command handling
if m.Content == "!ping" {
s.ChannelMessageSend(m.ChannelID, "Pong!")
}
}