Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
311497086e | ||
|
|
f41bd63fcd | ||
|
|
d3388aadc7 |
+1
-1
@@ -39,7 +39,7 @@ func main() {
|
|||||||
CheckPermissions: false,
|
CheckPermissions: false,
|
||||||
}
|
}
|
||||||
|
|
||||||
dg.AddHandler(messages.OnMessage)
|
dg.AddHandler(messages.CleanAmazonURLs)
|
||||||
|
|
||||||
err = dg.Open()
|
err = dg.Open()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -8,17 +8,17 @@ import (
|
|||||||
"github.com/bwmarrin/discordgo"
|
"github.com/bwmarrin/discordgo"
|
||||||
)
|
)
|
||||||
|
|
||||||
func OnMessage(s *discordgo.Session, message *discordgo.MessageCreate) {
|
func CleanAmazonURLs(s *discordgo.Session, message *discordgo.MessageCreate) {
|
||||||
defer func() {
|
defer func() {
|
||||||
if r := recover(); r != nil {
|
if r := recover(); r != nil {
|
||||||
log.Println("Recovered from panic in OnMessage")
|
log.Println("Recovered from panic in CleanAmazonURLs")
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
if message.Author.Bot {
|
if message.Author.Bot {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
content := message.Content
|
content := message.Content
|
||||||
amazonRegexString := `(http|https):\/\/((?:[\w-_\.]+)amazon(?:\.[\w\-_]+)+)([\w\/\-\.,@?^=%&~\+#]*[\w\-\@?^=%&/~\+#])?`
|
amazonRegexString := `(http|https):\/\/((?:[\w-_\.]*)amazon(?:\.[\w\-_]+)+)([\w\/\-\.,@?^=%&~\+#]*[\w\-\@?^=%&/~\+#])?`
|
||||||
amazonRegex := regexp.MustCompile(amazonRegexString)
|
amazonRegex := regexp.MustCompile(amazonRegexString)
|
||||||
urls := amazonRegex.FindAllString(message.Content, -1)
|
urls := amazonRegex.FindAllString(message.Content, -1)
|
||||||
if len(urls) == 0 {
|
if len(urls) == 0 {
|
||||||
@@ -28,6 +28,10 @@ func OnMessage(s *discordgo.Session, message *discordgo.MessageCreate) {
|
|||||||
if strings.Contains(url, "ref=") || strings.Contains(url, "?") {
|
if strings.Contains(url, "ref=") || strings.Contains(url, "?") {
|
||||||
parts := strings.Split(url, "/")
|
parts := strings.Split(url, "/")
|
||||||
new := strings.Join(parts[:len(parts)-1], "/")
|
new := strings.Join(parts[:len(parts)-1], "/")
|
||||||
|
if strings.Contains(new, "ref=") {
|
||||||
|
parts = strings.Split(new, "ref=")
|
||||||
|
new = parts[0]
|
||||||
|
}
|
||||||
content = strings.ReplaceAll(content, url, new)
|
content = strings.ReplaceAll(content, url, new)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user