-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
130 lines (110 loc) · 3.6 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package main
import (
"flag"
"fmt"
"log"
"os"
"os/signal"
"path/filepath"
"strings"
"syscall"
"time"
"github.com/bwmarrin/discordgo"
"github.com/l-yc/discord-tofu/config"
"github.com/l-yc/discord-tofu/brain"
"github.com/l-yc/discord-tofu/answer"
)
type Flags struct {
ConfigFile string
}
var (
flags Flags
)
func init() {
flag.StringVar(&flags.ConfigFile, "c", "config.toml", "Config File")
flag.Parse()
}
func main() {
config.ReadConfig(flags.ConfigFile)
// set up logging
filename := time.Now().Format(time.RFC3339)
filename = strings.ReplaceAll(filename, "-", "_")
filename = strings.ReplaceAll(filename, "T", "_")
filename = strings.ReplaceAll(filename, ":", "_")
filename = strings.ReplaceAll(filename, "+", "_")
dir := config.Cfg.LogsDirectory
if _, err := os.Stat(dir); os.IsNotExist(err) {
err = os.Mkdir(dir, 0755)
if err != nil {
log.Fatalf("Cannot create logs directory: %v", err)
}
log.Printf("Created logs directory at %s", dir)
}
logFile := filepath.Join(dir, filename + ".log")
f, err := os.OpenFile(logFile, os.O_RDWR | os.O_CREATE | os.O_APPEND, 0666)
if err != nil {
log.Fatalf("Error opening file: %v", err)
}
defer f.Close()
log.SetOutput(f)
// discord code
discord, err := discordgo.New("Bot " + config.Cfg.Token)
if err != nil {
log.Println("Error creating discord session:", err)
return
}
// Invite the bot
fmt.Println("Invite the bot to your server by visiting the following link:")
fmt.Printf("https://discord.com/oauth2/authorize?client_id=%s&scope=bot", config.Cfg.ClientID)
fmt.Println()
// Register the MessageCreate func as a callback for MessageCreate events.
discord.AddHandler(answer.MessageCreate)
// In this example, we only care about receiving message events.
discord.Identify.Intents = discordgo.
MakeIntent(discordgo.IntentsGuildMessages | discordgo.IntentsDirectMessages)
// Open a websocket connection to Discord and begin listening.
err = discord.Open()
if err != nil {
log.Println("error opening connection,", err)
return
}
// Monitor interrupt to exit
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
// Monitor state
go func() {
log.Println("Listening for status updates")
brain.Input <- brain.BrainInput{
Type: brain.BrainInputTypeStatus,
}
for {
select {
case state := <-brain.State:
log.Println("Update state", state)
statuses := []discordgo.Status{
discordgo.StatusOnline,
discordgo.StatusIdle,
discordgo.StatusDoNotDisturb,
}
err := discord.UpdateStatusComplex(discordgo.UpdateStatusData{
Game: &discordgo.Game{
Name: "::help | " + state.Mood,
Type: discordgo.GameTypeWatching,
},
Status: string(statuses[state.Status]),
})
if err != nil {
log.Println("Error updating status:", err)
}
break
case <-sc:
return
}
}
}()
// Wait here until CTRL-C or other term signal is received.
fmt.Println("Bot is now running. Press CTRL-C to exit.")
<-sc
// Cleanly close down the Discord session.
discord.Close()
}