do no more blocking of invite links from the same guild
package listeners
import (
+ "fmt"
"net/http"
"regexp"
"strings"
@@ -15,7 +16,7 @@ import (
)
var (
- rxInvLink = regexp.MustCompile(`(?i)(https?:\/\/)?(www\.)?(discord\.gg|discord(app)?\.com\/invite)\/.*`)
+ rxInvLink = regexp.MustCompile(`(?i)(?:https?:\/\/)?(?:www\.)?(?:discord\.gg|discord(?:app)?\.com\/invite)\/(.*)`)
rxGenLink = regexp.MustCompile(`(?i)(https?:\/\/)?(www\.)?([\w-\S]+\.)+\w{1,10}\/?[\S]+`)
)
@@ -42,61 +43,70 @@ func (l *ListenerInviteBlock) HandlerMessageEdit(s *discordgo.Session, e *discor
func (l *ListenerInviteBlock) invokeCheck(s *discordgo.Session, msg *discordgo.Message) {
cont := msg.Content
- if l.checkForInviteLink(cont) {
- l.detected(s, msg)
+ ok, matches := l.checkForInviteLink(cont)
+ if ok {
+ l.detected(s, msg, matches)
return
}
link := rxGenLink.FindString(cont)
if link != "" {
- match, err := l.followLink(link)
+ ok, matches, err := l.followLink(link)
if err != nil {
util.Log.Error("Failed following link: ", err)
return
}
- if match {
- l.detected(s, msg)
+ if ok {
+ l.detected(s, msg, matches)
}
}
}
-func (l *ListenerInviteBlock) checkForInviteLink(cont string) bool {
- return rxInvLink.MatchString(cont)
+func (l *ListenerInviteBlock) checkForInviteLink(cont string) (bool, [][]string) {
+ matches := rxInvLink.FindAllStringSubmatch(cont, -1)
+ return matches != nil, matches
}
-func (l *ListenerInviteBlock) followLink(link string) (bool, error) {
+func (l *ListenerInviteBlock) followLink(link string) (bool, [][]string, error) {
if !strings.HasPrefix(link, "http://") && !strings.HasPrefix(link, "https://") {
link = "http://" + link
}
resp, err := http.DefaultClient.Get(link)
if err != nil {
- return false, nil
+ return false, nil, nil
}
- return l.checkForInviteLink(resp.Request.URL.String()), nil
+ ok, matches := l.checkForInviteLink(resp.Request.URL.String())
+ return ok, matches, nil
}
-func (l *ListenerInviteBlock) detected(s *discordgo.Session, e *discordgo.Message) error {
+func (l *ListenerInviteBlock) detected(s *discordgo.Session, e *discordgo.Message, matches [][]string) error {
+ fmt.Println(matches)
+
enabled, err := l.db.GetGuildInviteBlock(e.GuildID)
if database.IsErrDatabaseNotFound(err) {
return nil
}
- if err != nil {
+ if err != nil || enabled == "" {
return err
}
- if enabled == "" {
- return nil
- }
ok, override, err := l.pmw.CheckPermissions(s, e.GuildID, e.Author.ID, "!sp.guild.mod.inviteblock.send")
- if err != nil {
+ if err != nil || ok || override {
return err
}
- if ok || override {
+ if invites, err := s.GuildInvites(e.GuildID); err == nil {
+ inviteCode := matches[0][1]
+ for _, inv := range invites {
+ if inv.Code == inviteCode {
return nil
}
+ }
+ } else {
+ util.Log.Error(err)
+ }
return s.ChannelMessageDelete(e.ChannelID, e.ID)
}
