-
Notifications
You must be signed in to change notification settings - Fork 0
/
dsservice.go
130 lines (113 loc) Β· 3.84 KB
/
dsservice.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 discord
import (
"errors"
"fmt"
"math/rand"
"net/url"
"regexp"
"strings"
"github.com/bwmarrin/discordgo"
"github.com/nelsonleduc/calmanbot/config"
"github.com/nelsonleduc/calmanbot/service"
)
type DSService interface {
service.Service
MessageFromSessionAndMessage(session *discordgo.Session, message *discordgo.Message) service.Message
}
type dsService struct {
session *discordgo.Session
}
func NewDSService(discordSession *discordgo.Session) DSService {
newService := dsService{discordSession}
service.RegisterServiceForTriggers(newService)
return newService
}
func (d dsService) Post(post service.Post, groupMessage service.Message) {
discordMessage := groupMessage.(dsMessage)
d.postToChannel(post, discordMessage.ChannelID)
}
func (d dsService) postToChannel(post service.Post, channelID string) {
if post.Type == service.PostTypeText {
d.session.ChannelMessageSend(channelID, post.Text)
} else if post.Type == service.PostTypeImage {
var footer *discordgo.MessageEmbedFooter
if postURL, err := url.Parse(post.Text); err == nil {
footer = &discordgo.MessageEmbedFooter{
Text: postURL.Host,
}
} else {
footer = nil
}
d.session.ChannelMessageSendEmbed(channelID, &discordgo.MessageEmbed{
Image: &discordgo.MessageEmbedImage{
URL: post.Text,
},
Footer: footer,
})
} else if post.Type == service.PostTypeURL {
d.session.ChannelMessageSendEmbed(channelID, &discordgo.MessageEmbed{
URL: post.RawText,
Title: post.Text,
})
}
}
func (d dsService) NoteProcessing(groupMessage service.Message) {
discordMessage := groupMessage.(dsMessage)
emojis := []string{"π", "π€", "π¦₯", "π", "π", "π", "π¦", "π"}
chosen := emojis[rand.Intn(len(emojis))]
err := discordMessage.session.MessageReactionAdd(discordMessage.ChannelID, discordMessage.ID, chosen)
if err != nil && config.Configuration().VerboseMode() {
fmt.Printf("Error posting reaction: chose: %v err: %v\n", chosen, err)
}
}
func (d dsService) ServiceMonitor() (service.Monitor, error) {
return nil, errors.New("Unsupported")
}
func (d dsService) MessageFromSessionAndMessage(session *discordgo.Session, message *discordgo.Message) service.Message {
c, err := session.Channel(message.ChannelID)
isDirect := false
if err != nil && config.Configuration().VerboseMode() {
fmt.Printf("Error fetching channel %v", err)
} else {
isDirect = c.Type == discordgo.ChannelTypeDM
}
processed := processText(session, message, isDirect)
return dsMessage{message, session, processed}
}
func processText(session *discordgo.Session, message *discordgo.Message, isDirect bool) string {
verboseLog := config.Configuration().VerboseMode()
modifiedText := message.Content
if verboseLog {
fmt.Printf("Parsing discord text: \"%s\"\n", modifiedText)
}
for _, mention := range message.Mentions {
re := regexp.MustCompile("<@.?" + mention.ID + ">")
modifiedText = re.ReplaceAllString(modifiedText, "@"+mention.Username)
if verboseLog {
fmt.Printf("Replacing \"%v\" with \"%v\"\n", mention.ID, mention.Username)
}
}
if len(message.MentionRoles) > 0 {
g, _ := session.GuildRoles(message.GuildID)
for _, mention := range message.MentionRoles {
for _, role := range g {
if role.ID == mention {
re := regexp.MustCompile("<@.?" + mention + ">")
modifiedText = re.ReplaceAllString(modifiedText, "@"+role.Name)
if verboseLog {
fmt.Printf("Replacing role \"%v\" with \"%v\"\n", mention, role.Name)
}
}
}
}
}
re := regexp.MustCompile("<@.?" + session.State.User.ID + ">")
modifiedText = re.ReplaceAllString(modifiedText, "@"+session.State.User.Username)
if isDirect && !strings.Contains(modifiedText, "@"+session.State.User.Username+" ") {
modifiedText = "@" + session.State.User.Username + " " + modifiedText
}
if verboseLog {
fmt.Printf("Final discord text: \"%s\"\n", modifiedText)
}
return modifiedText
}