-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
111 lines (96 loc) · 2.42 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package main
import (
"bytes"
"flag"
"fmt"
"log"
"os"
"os/signal"
"strings"
"syscall"
"github.com/bwmarrin/discordgo"
)
// Chat @bot to usage bot
var prefix = "<@me>"
var botToken string
var bot *discordgo.Session
// use bot.ChannelMessageSend to send message to channel
func init() {
if len(os.Args) > 1 {
botToken = os.Args[1]
}
}
func main() {
if len(botToken) < 1 {
log.Println("Usage:", os.Args[0], "[discord bot token]")
return
}
var err error
bot, err = discordgo.New("Bot " + botToken)
if err != nil {
log.Println("Error creating Discord session:", err)
return
}
if prefix == "<@me>" {
currentUser, err := bot.User("@me")
if err != nil {
log.Panicln(err)
}
prefix = currentUser.Mention()
}
// Register the messageCreate func as a callback for MessageCreate events.
bot.AddHandler(messageCreate)
// Open a websocket connection to Discord and begin listening. (auto reconnect)
err = bot.Open()
if err != nil {
log.Println("Error opening connection:", err)
return
}
err = initJob()
if err != nil {
log.Panicln(err)
}
// Wait here until CTRL-C or other term signal is received.
log.Println("Bot is now running. Press CTRL-C to exit.")
log.Println("Usage: Chat @bot -h for more information.")
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
<-sc
log.Println("Terminating..")
// Cleanly close down the Discord session.
bot.Close()
}
// This function will be called (due to AddHandler above) every time a new
// message is created on any channel that the autenticated bot has access to.
func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
// Ignore all messages created by the bot itself
if m.Author.ID == s.State.User.ID {
return
}
content := strings.Trim(m.Content, "\r\n\t ")
if !strings.HasPrefix(content, prefix) {
return
}
buf := new(bytes.Buffer)
defer func() {
output := buf.String()
if len(output) < 1 {
return
}
_, err := s.ChannelMessageSend(m.ChannelID, output)
if err != nil {
log.Println(err)
}
}()
args, err := parseCommandLine(content[len(prefix):])
if err != nil {
fmt.Fprintln(buf, err)
return
}
fls := flag.NewFlagSet(prefix, flag.ContinueOnError)
fls.SetOutput(buf)
err = commandHandle(s, m, fls, args, buf)
if err != nil {
fmt.Fprintln(buf, err)
}
}