-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.go
More file actions
215 lines (184 loc) · 5.24 KB
/
bot.go
File metadata and controls
215 lines (184 loc) · 5.24 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package main
import (
"flag"
"fmt"
"log"
"math/rand"
"os"
"os/signal"
"strings"
"syscall"
"time"
"github.com/bwmarrin/discordgo"
)
const LOG_FILE string = "/var/log/ane_bot/app.log"
var heMod bool = true
var Token string
var dmPermission bool = false
var ms *MessageStash
var dg *discordgo.Session
var defaultMemberPermissions int64 = discordgo.PermissionManageServer
var commands = []*discordgo.ApplicationCommand{
{
Name: "time",
Description: "get time with nano sec",
DefaultPermission: &dmPermission,
DefaultMemberPermissions: &defaultMemberPermissions,
},
{
Name: "he_enable",
Description: "enable he mod",
DefaultPermission: &dmPermission,
DefaultMemberPermissions: &defaultMemberPermissions,
},
{
Name: "he_disable",
Description: "dicable he mod",
DefaultPermission: &dmPermission,
DefaultMemberPermissions: &defaultMemberPermissions,
},
}
var commandHandlers = map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate){
"time": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "current time is: " + time.Now().Format("2006-01-02 15:04:05.999999999"),
},
})
},
"he_enable": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
heMod = true
logging("he mod enabled")
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "he mod enabled",
},
})
},
"he_disable": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
heMod = false
logging("he mod disabled")
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "he mod disabled",
},
})
},
}
func init() {
ms = new(MessageStash)
log.SetOutput(os.Stdout)
flag.StringVar(&Token, "t", "", "Bot Token")
flag.Parse()
var err error
dg, err = discordgo.New("Bot " + Token)
if err != nil {
log.Fatalf("Invalid bot parameters: %v", err)
}
}
func main() {
var err error
dg.AddHandler(messageCreate)
dg.AddHandler(baseHandler)
dg.Identify.Intents = discordgo.IntentsGuildMessages
err = dg.Open()
if err != nil {
log.Println("error opening connection,", err)
return
}
defer dg.Close()
log.Println("Adding commands...")
registeredCommands := make([]*discordgo.ApplicationCommand, len(commands))
for i, v := range commands {
cmd, err := dg.ApplicationCommandCreate(dg.State.User.ID, "", v)
if err != nil {
log.Panicf("Cannot create '%v' command: %v", v.Name, err)
}
log.Println("add cmd", v.Name)
registeredCommands[i] = cmd
}
fmt.Println("Bot is now running. Press CTRL-C to exit.")
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
log.Println("Bot exit with code", <-sc)
log.Println("Removing commands...")
for _, v := range registeredCommands {
err := dg.ApplicationCommandDelete(dg.State.User.ID, "", v.ID)
if err != nil {
log.Panicf("Cannot delete '%v' command: %v", v.Name, err)
}
log.Println("remove cmd ", v.Name)
}
log.Println("Bot close")
}
func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
if m.Author.ID == s.State.User.ID {
return
}
if m.Content == "ping" {
s.ChannelMessageSend(m.ChannelID, "Pong")
return
}
if m.Content == "pong" {
s.ChannelMessageSend(m.ChannelID, "Ping")
return
}
if heMod {
if ms.CheckOverflow(m.ChannelID, m.Author.ID, m.Content) {
if rand.Intn(100) > 40 {
ms.Flush()
go sendMessage(s, m.ChannelID, m.Content)
}
} else {
ms.Fill(m.Author.ID, m.ChannelID, m.Content)
}
}
if heMod && m.Content == "👋" {
s.ChannelMessageSend(m.ChannelID, "👋")
}
if strings.Contains(m.Content, "what is") {
s.ChannelMessageSend(m.ChannelID, "У тебя прав нет, фрикич<:he:856194932770471936>")
}
}
func baseHandler(s *discordgo.Session, i *discordgo.InteractionCreate) {
if h, ok := commandHandlers[i.ApplicationCommandData().Name]; ok {
h(s, i)
}
}
func logging(s string) {
logFile, err := os.OpenFile(LOG_FILE, os.O_APPEND|os.O_RDWR|os.O_CREATE, 0644)
if err != nil {
return
}
defer logFile.Close()
log.SetOutput(logFile)
log.Println(s)
}
func sendMessage(s *discordgo.Session, ChannelID string, message string) {
time.Sleep(1 * time.Second)
s.ChannelMessageSend(ChannelID, message)
}
type MessageStash struct {
authorID string
channelID string
message string
}
func (ms *MessageStash) Empty() bool {
return ms.channelID == ""
}
func (ms *MessageStash) CheckOverflow(channelID string, authorID string, message string) bool {
return ms.authorID != authorID && ms.channelID == channelID && ms.message == message
}
func (ms *MessageStash) Fill(authorID string, channelID string, message string) {
ms.authorID = authorID
ms.channelID = channelID
ms.message = message
}
func (ms *MessageStash) Flush() {
ms.authorID = ""
ms.channelID = ""
ms.message = ""
}