-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMessageHandler.go
64 lines (55 loc) · 1.49 KB
/
MessageHandler.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
package main
import (
"fmt"
"github.com/bwmarrin/discordgo"
"strings"
)
func HandleMessage(s *discordgo.Session, m *discordgo.MessageCreate) {
if m.Author.ID == s.State.User.ID {
return
}
deleteMessage := func() {
err := s.ChannelMessageDelete(m.ChannelID, m.Message.ID)
if err != nil {
L.Printf("Failed to delete the original message with error: %S", err.Error())
return
}
}
if m.Content == "!help" {
help(s, m)
deleteMessage()
return
}
if len(m.Mentions) != 1 {
return
}
if m.Mentions[0].ID != s.State.User.ID {
return
}
defer func() {
if r := recover(); r != nil {
L.Printf("Recovering from error '%s'.", r)
msg := discordgo.MessageSend{
Embed: &discordgo.MessageEmbed{Color: 0xff0000, Description: fmt.Sprintf("Something went wrong with your request `%s`.\n"+
"Please try again laster or contact the server administrator.\n Request failed with error: ```%s```", replaceMentions(m.Content, s), r),
Title: "Error",
},
}
_, err := s.ChannelMessageSendComplex(m.ChannelID, &msg)
if err != nil {
L.Fatal(err)
}
}
}()
if callback := CommandList.getCallback(m); callback != nil {
callback(s, m)
} else {
return
}
deleteMessage()
}
func replaceMentions(message string, s *discordgo.Session) string {
message = strings.Replace(message, fmt.Sprintf("<@!%s>", s.State.User.ID), "@"+s.State.User.Username, -1)
message = strings.Replace(message, fmt.Sprintf("<@%s>", s.State.User.ID), "@"+s.State.User.Username, -1)
return message
}