@ -3,10 +3,15 @@ package main
import (
import (
"encoding/json"
"encoding/json"
"fmt"
"fmt"
"io"
"io/ioutil"
"io/ioutil"
"net/http"
"net/url"
"os"
"os"
"time"
"path/filepath"
"strconv"
"strings"
"strings"
"time"
"github.com/bwmarrin/discordgo"
"github.com/bwmarrin/discordgo"
)
)
@ -60,7 +65,31 @@ func status(s *discordgo.Session) {
go runPurge ( s )
go runPurge ( s )
return
return
}
}
func storeVerification ( v Verification ) {
defer log . PanicSafe ( )
fileURL , _ := url . Parse ( v . Photo )
path := fileURL . Path
segments := strings . Split ( path , "/" )
fileName := segments [ len ( segments ) - 1 ]
file , _ := os . Create ( fmt . Sprintf ( "./verifications/%s-%s-%s" , v . UserID , v . Username , fileName ) )
client := http . Client {
CheckRedirect : func ( r * http . Request , via [ ] * http . Request ) error {
r . URL . Opaque = r . URL . Path
return nil
} ,
}
resp , err := client . Get ( v . Photo )
if err != nil {
log . LogError ( "Unable to download verification %s-%s-%s" , v . UserID , v . Username , fileName )
}
defer resp . Body . Close ( )
defer file . Close ( )
_ , err = io . Copy ( file , resp . Body )
if err != nil {
log . LogError ( "Unable to store verification %s-%s-%s" , v . UserID , v . Username , fileName )
}
}
func loadConfig ( ) {
func loadConfig ( ) {
var c Config
var c Config
confFile , _ := ioutil . ReadFile ( configFile )
confFile , _ := ioutil . ReadFile ( configFile )
@ -105,6 +134,38 @@ func saveConfig() {
}
}
}
}
func findVerification ( s * discordgo . Session , m * discordgo . MessageCreate ) {
defer log . PanicSafe ( )
parts := strings . Split ( m . Content , " " )
discordId := parts [ 1 ]
_ , err := strconv . Atoi ( discordId )
if err != nil {
discordId = idFromUsername ( discordId )
}
user , err := s . GuildMember ( config . GuildID , discordId )
if err != nil {
log . LogErrorType ( err )
}
matches , err := filepath . Glob ( fmt . Sprintf ( "./verifications/*%+v*" , discordId ) )
if err != nil {
log . LogErrorType ( err )
return
}
if len ( matches ) != 1 {
s . ChannelMessageSend ( m . ChannelID , fmt . Sprintf ( "Error finding verification for ID %+v" , discordId ) )
return
}
verificationImage , err := os . Open ( matches [ 0 ] )
if err != nil {
log . LogErrorType ( err )
return
}
msg := fmt . Sprintf ( "```%+v\nJoined: %+v\n```" , user . User . Username , user . JoinedAt )
s . ChannelFileSendWithMessage ( m . ChannelID , msg , fmt . Sprintf ( "%+v Verification" , discordId ) , verificationImage )
}
func bumpTimer ( s * discordgo . Session ) {
func bumpTimer ( s * discordgo . Session ) {
if ! bump {
if ! bump {
return
return
@ -140,32 +201,6 @@ func (v Verification) prettyPrint() string {
return ret
return ret
}
}
func userFromID ( i string ) discordgo . User {
u , err := dg . GuildMember ( config . GuildID , i )
if err != nil {
log . LogErrorType ( err )
return discordgo . User { }
}
return * u . User
}
func idFromUsername ( username string ) string {
userID := ""
g , err := dg . GuildMembers ( config . GuildID , "" , 1000 )
log . LogInfo ( "reqPass guild is %+v." , config . GuildID )
if err == nil {
for _ , m := range g {
if strings . ToUpper ( m . Nick ) == strings . ToUpper ( username ) {
userID = m . User . ID
log . LogInfo ( "User ID found for %+v as %+v" , username , userID )
}
}
} else {
log . LogError ( "Unable to find user ID for %+v" , username )
}
return userID
}
func adminInteraction ( s * discordgo . Session , m string ) {
func adminInteraction ( s * discordgo . Session , m string ) {
admin , _ := s . GuildMember ( config . GuildID , m )
admin , _ := s . GuildMember ( config . GuildID , m )
counter , ok := config . Stats [ admin . User . ID ]
counter , ok := config . Stats [ admin . User . ID ]