2020-12-17 23:53:26 +00:00
package main
import (
"github.com/diamondburned/arikawa/discord"
"github.com/diamondburned/arikawa/gateway"
"github.com/diamondburned/arikawa/state"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
"os"
"os/signal"
"strings"
"time"
)
var (
2020-12-17 23:56:51 +00:00
s * state . State
channels [ ] discord . ChannelID
2020-12-17 23:53:26 +00:00
prohibitedPhrases [ ] string
)
func setupConfiguration ( ) {
viper . SetDefault ( "prohibitedPhrases" , "" )
viper . SetDefault ( "channels" , "" )
2020-12-17 23:56:51 +00:00
viper . SetDefault ( "message" , "Hi {username}, you've posted a message that was automatically removed for matching a filter. If you need help, please use the correct channel." )
2020-12-17 23:53:26 +00:00
viper . AutomaticEnv ( )
viper . SetConfigName ( "ottomate" )
2020-12-18 00:09:04 +00:00
viper . SetConfigType ( "yaml" )
2020-12-17 23:56:51 +00:00
viper . AddConfigPath ( "/etc/ottomate/" )
viper . AddConfigPath ( "$HOME/.ottomate" )
viper . AddConfigPath ( "." )
2020-12-17 23:53:26 +00:00
viper . ReadInConfig ( )
}
func main ( ) {
setupConfiguration ( )
var token = viper . GetString ( "token" )
if token == "" {
log . Fatalln ( "Invalid token/token is not set" )
}
2020-12-18 00:04:56 +00:00
var channelStrs [ ] string
if viper . ConfigFileUsed ( ) != "" {
channelStrs = viper . GetStringSlice ( "channels" )
} else {
channelStrs = strings . Split ( viper . GetString ( "channels" ) , "," )
}
2020-12-17 23:53:26 +00:00
channels = make ( [ ] discord . ChannelID , len ( channelStrs ) )
for i , channelStr := range channelStrs {
sf , err := discord . ParseSnowflake ( channelStr )
if err != nil {
continue
}
channels [ i ] = discord . ChannelID ( sf )
}
2020-12-18 00:04:56 +00:00
if viper . ConfigFileUsed ( ) != "" {
prohibitedPhrases = viper . GetStringSlice ( "phohibitedPhrases" )
} else {
prohibitedPhrases = strings . Split ( viper . GetString ( "prohibitedPhrases" ) , "," )
}
2020-12-17 23:53:26 +00:00
var err error
s , err = state . New ( "Bot " + token )
if err != nil {
log . WithError ( err ) . Fatalln ( "Session failed" )
}
s . AddHandler ( messageCreate )
if err := s . Open ( ) ; err != nil {
log . WithError ( err ) . Fatalln ( "Failed to connect" )
}
defer s . Close ( )
u , err := s . Me ( )
if err != nil {
log . WithError ( err ) . Fatalln ( "Failed to get myself" )
}
log . WithField ( "username" , u . Username ) . Info ( "Started bot" )
ch := make ( chan os . Signal )
signal . Notify ( ch , os . Kill , os . Interrupt )
2020-12-17 23:56:51 +00:00
<- ch
2020-12-17 23:53:26 +00:00
}
func messageCreate ( e * gateway . MessageCreateEvent ) {
if e . Content == "" {
return
}
found := false
for _ , channel := range channels {
if channel == e . ChannelID {
found = true
break
}
}
if ! found {
return
}
found = false
for _ , phrase := range prohibitedPhrases {
if strings . Contains ( e . Content , phrase ) {
// Send message
found = true
break
}
}
if ! found {
return
}
member , err := s . Member ( e . GuildID , e . Author . ID )
if err != nil || ! member . Joined . IsValid ( ) {
log . WithFields ( log . Fields {
"guildId" : e . GuildID ,
2020-12-17 23:56:51 +00:00
"userId" : e . Author . ID ,
2020-12-17 23:53:26 +00:00
} ) . Debug ( "Unable to retrieve guild member" )
return
}
joinTime := member . Joined . Time ( )
if joinTime . Before ( time . Now ( ) . AddDate ( 0 , 0 , - 7 ) ) {
log . WithFields ( log . Fields {
"guildId" : e . GuildID ,
2020-12-17 23:56:51 +00:00
"userId" : e . Author . ID ,
2020-12-17 23:53:26 +00:00
} ) . Debug ( "Join date below cutoff" )
return
}
2020-12-17 23:57:19 +00:00
err = s . DeleteMessage ( e . ChannelID , e . ID )
if err != nil {
log . WithFields ( log . Fields {
"guildId" : e . GuildID ,
"userId" : e . Author . ID ,
"messageId" : e . ID ,
} ) . Debug ( "Unable to delete message" )
return
}
2020-12-17 23:53:26 +00:00
ch , err := s . CreatePrivateChannel ( e . Author . ID )
if err != nil {
log . WithFields ( log . Fields {
"guildId" : e . GuildID ,
2020-12-17 23:56:51 +00:00
"userId" : e . Author . ID ,
2020-12-17 23:53:26 +00:00
} ) . Debug ( "Unable to create private channel with member" )
return
}
2020-12-17 23:56:51 +00:00
message := viper . GetString ( "message" )
replacer := strings . NewReplacer ( "{username}" , e . Author . Username , "{message}" , e . Content )
_ , err = s . SendText ( ch . ID , replacer . Replace ( message ) )
2020-12-17 23:53:26 +00:00
if err != nil {
// Unable to send message
log . WithFields ( log . Fields {
"guildId" : e . GuildID ,
2020-12-17 23:56:51 +00:00
"userId" : e . Author . ID ,
2020-12-17 23:53:26 +00:00
} ) . Debug ( "Unable to send message in private channel" )
}
2020-12-17 23:56:51 +00:00
}