-
Notifications
You must be signed in to change notification settings - Fork 1
/
discord.go
executable file
·129 lines (103 loc) · 3.82 KB
/
discord.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
package main
import (
"fmt"
"os"
"os/signal"
"syscall"
"time"
"strings"
"github.com/bwmarrin/discordgo"
)
var token = "BOT-TOKEN"
var botChID = "BOT-RSS-SHARE-CHANNEL-ID"
var Dg *discordgo.Session
func initSession() {
// Create a new DC session using the bot's token
dg, err := discordgo.New("Bot " + token)
if err != nil {
panic(err)
}
err = dg.Open()
if err != nil {
fmt.Println("Error openning connection, ", err)
l.Fatalf("Error openning connection, ", err)
return
}
Dg = dg
}
func ConnectToDC() {
initSession()
go ParseRSS()
time.Sleep(time.Second)
// Register the messageCreate func as a callback for MessageCreate events.
Dg.AddHandler(messageCreate)
Dg.Identify.Intents = discordgo.IntentsGuildMessages
// Wait here until CTRL-C or other term signal is received.
fmt.Println("Bot is now running, press CTRL+C to exit.")
l.Println("[INFO] DCScragor started to running.")
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
<-sc
// Cleanly close down the Discord session.
Dg.Close()
l.Println("[INFO] DCScragor stopped.")
}
func handleHelp(s *discordgo.Session, m *discordgo.MessageCreate) {
s.ChannelMessageSend(m.ChannelID, "Available commands:\n----------------------------\n!dcscragor add_blog <URL> Adds your RSS feed URL to the scrape wish list.")
}
func handleAddBlogUrltoList(s *discordgo.Session, m *discordgo.MessageCreate) {
splits := strings.Split(m.Content, " ")
url := splits[2]
if len(splits) != 3 {
s.ChannelMessageSend(m.ChannelID, "You are prompted wrong command to add blog request. Usage: !dcscragor add_blog <URL>")
l.Printf("DCScragor got a wrong blog adding request from %s, Command: %s", m.Author, m.Content)
} else{
file, err := os.OpenFile("blog_request.list", os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
l.Println(err)
}
defer file.Close()
if !strings.Contains(readFile("blog_request.list"), url) {
if _, err := file.WriteString(url + "\n"); err != nil {
l.Fatal(err)
} else{
s.ChannelMessageSend(m.ChannelID, "Blog URL is added to wish list.")
l.Printf("DCScragor added a new blog to the wish list. Author: %s, URL: %s", m.Author, url)
}
} else{
s.ChannelMessageSend(m.ChannelID, "You've already requested for this URL!")
}
}
}
// 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
}
if m.Content == "!dcscragor" {
s.ChannelMessageSend(m.ChannelID, "Hey! I'm here. Use !dcscragor help")
l.Printf("DCScragor got a command from %s, Command: !dcscragor", m.Author)
}
// !dcscragor add_blog <URL>
if strings.HasPrefix(m.Content, "!dcscragor") && len(strings.Split(m.Content, " ")) == 3{
if strings.Split(m.Content, " ")[1] == "add_blog" {
if strings.Contains(strings.Split(m.Content, " ")[2], "http://") || strings.Contains(strings.Split(m.Content, " ")[2], "https://") {
handleAddBlogUrltoList(s,m)
l.Printf("DCScragor got a blog adding request from %s, Command: %s", m.Author, m.Content)
} else{
s.ChannelMessageSend(m.ChannelID, "Please use a proper URL!")
}
} else {
s.ChannelMessageSend(m.ChannelID, "Please use !dcscragor add_blog <URL> command to request your URL to add wish list.")
}
}
// !dcscragor help
if strings.HasPrefix(m.Content, "!dcscragor") && len(strings.Split(m.Content, " ")) == 2{
if strings.Split(m.Content, " ")[1] == "help"{
handleHelp(s,m)
}
}
}