-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
177 lines (150 loc) · 4.65 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
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
package main
import (
"flag"
"fmt"
"os"
"os/signal"
"regexp"
"strings"
"syscall"
"time"
"github.com/bwmarrin/discordgo"
"github.com/stegosawr/Albedo/command"
"github.com/stegosawr/Albedo/embed"
"github.com/stegosawr/Albedo/reactions"
"github.com/stegosawr/Albedo/static"
"github.com/stegosawr/Albedo/utils"
)
// Variables used for command line parameters
var (
Token string
)
func init() {
flag.StringVar(&Token, "t", "", "Bot Token")
flag.Parse()
}
func main() {
// Create a new Discord session using the provided bot token.
dg, err := discordgo.New("Bot " + Token)
if err != nil {
fmt.Println("error creating Discord session,", err)
return
}
// Register the messageCreate func as a callback for MessageCreate events.
dg.AddHandler(messageCreate)
dg.AddHandler(messageReactionAdd)
// In this example, we only care about receiving message events.
dg.Identify.Intents = discordgo.IntentsGuildMessages | discordgo.IntentsGuildMessageReactions
// Open a websocket connection to Discord and begin listening.
err = dg.Open()
if err != nil {
fmt.Println("error opening connection,", err)
return
}
if _, err := dg.Channel(static.AnimeNewsChannelID); err == nil {
timeTillInitialRun, _ := time.ParseDuration(fmt.Sprintf("%vh", 25-time.Now().Hour()))
t := time.NewTimer(timeTillInitialRun)
go func() {
for {
<-t.C
t.Reset(24 * time.Hour)
command.UpdateAnime(dg)
}
}()
}
// Wait here until CTRL-C or other term signal is received.
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)
<-sc
// Cleanly close down the Discord session.
dg.Close()
}
// This function will be called (due to AddHandler above) every time a new
// message is created on any channel that the authenticated bot has access to.
func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
// Ignore all messages created by the bot itself
// This isn't required in this specific example but it's a good practice.
if m.Author.ID == s.State.User.ID {
return
}
regexList := map[string]*regexp.Regexp{
// has to be done first -> unshortens twitter URLs so the bot can react
static.TwitterProxy: regexp.MustCompile(`https://t.co/\w+`),
static.AmiAmi: regexp.MustCompile(`https://www.amiami.(?:com|jp)/.+(?:[gs]code=[\w-]+|s_keywords=([\w-%]+))`),
static.CuddlyOctopus: regexp.MustCompile(`https://cuddlyoctopus.com/product/[^/]+`),
static.NHentai: regexp.MustCompile(`^\+?[0-9]{5,6}`),
static.SolarisJapan: regexp.MustCompile(`https://solarisjapan\.com.*/products/.+`),
}
for k, v := range regexList {
matchedRegex := v.FindString(m.Content)
if len(matchedRegex) < 1 {
continue
}
if k == static.TwitterProxy {
newURL, err := utils.UnShortenURL(matchedRegex)
if err != nil {
s.ChannelMessageSend(m.ChannelID, err.Error())
break
}
m.Content = strings.ReplaceAll(m.Content, matchedRegex, newURL)
continue
}
switch k {
case static.AmiAmi, static.CuddlyOctopus, static.SolarisJapan:
embeds, err := embed.Embed(s, m, k)
if err != nil {
s.ChannelMessageSend(m.ChannelID, err.Error())
break
}
for _, embed := range embeds {
msg, err := s.ChannelMessageSendEmbed(m.ChannelID, embed)
if err != nil {
s.ChannelMessageSend(m.ChannelID, err.Error())
break
}
if k == static.AmiAmi {
s.MessageReactionAdd(m.ChannelID, msg.ID, "⬅️")
s.MessageReactionAdd(m.ChannelID, msg.ID, "➡️")
}
if k == static.AmiAmi || k == static.CuddlyOctopus || k == static.SolarisJapan {
s.MessageReactionAdd(m.ChannelID, msg.ID, "💶")
s.MessageReactionAdd(m.ChannelID, msg.ID, "💴")
s.MessageReactionAdd(m.ChannelID, msg.ID, "💵")
s.MessageReactionAdd(m.ChannelID, msg.ID, "💷")
}
}
case static.NHentai:
command.NhentaiShow(s, m)
}
break
}
if !strings.HasPrefix(m.Content, static.Prefix) || m.Content == static.Prefix {
return
}
switch strings.TrimPrefix(m.Content, static.Prefix) {
case "help":
command.Help(s, m)
case "ping":
command.Ping(s, m)
case "dailyMedia":
command.UpdateAnime(s)
case "animeSites":
s.ChannelMessageSend(m.ChannelID, "https://theindex.moe/")
case "bestReleases":
s.ChannelMessageSend(m.ChannelID, "https://releases.moe/")
case "delmsg":
command.DeleteAllMessagesInChannel(s, m.ChannelID)
}
}
func messageReactionAdd(s *discordgo.Session, mra *discordgo.MessageReactionAdd) {
//ignore bot reactions
if mra.UserID == s.State.User.ID {
return
}
// pass data to the correct handlers
err := reactions.Process(s, mra)
if err != nil {
s.ChannelMessageSend(mra.ChannelID, err.Error())
}
}