Support message placeholders and configuration of message

This commit is contained in:
Tyler 2020-12-17 18:56:51 -05:00
parent 00377f7670
commit 05e12f4436
1 changed files with 19 additions and 11 deletions

30
main.go
View File

@ -13,18 +13,22 @@ import (
)
var (
s *state.State
channels []discord.ChannelID
s *state.State
channels []discord.ChannelID
prohibitedPhrases []string
)
func setupConfiguration() {
viper.SetDefault("prohibitedPhrases", "")
viper.SetDefault("channels", "")
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.")
viper.AutomaticEnv()
viper.SetConfigName("ottomate")
viper.AddConfigPath("/etc/ottomate/")
viper.AddConfigPath("$HOME/.ottomate")
viper.AddConfigPath(".")
viper.ReadInConfig()
}
@ -78,7 +82,7 @@ func main() {
ch := make(chan os.Signal)
signal.Notify(ch, os.Kill, os.Interrupt)
<- ch
<-ch
}
func messageCreate(e *gateway.MessageCreateEvent) {
@ -117,8 +121,8 @@ func messageCreate(e *gateway.MessageCreateEvent) {
if err != nil {
log.WithFields(log.Fields{
"guildId": e.GuildID,
"userId": e.Author.ID,
"guildId": e.GuildID,
"userId": e.Author.ID,
"messageId": e.ID,
}).Debug("Unable to delete message")
return
@ -129,7 +133,7 @@ func messageCreate(e *gateway.MessageCreateEvent) {
if err != nil || !member.Joined.IsValid() {
log.WithFields(log.Fields{
"guildId": e.GuildID,
"userId": e.Author.ID,
"userId": e.Author.ID,
}).Debug("Unable to retrieve guild member")
return
}
@ -139,7 +143,7 @@ func messageCreate(e *gateway.MessageCreateEvent) {
if joinTime.Before(time.Now().AddDate(0, 0, -7)) {
log.WithFields(log.Fields{
"guildId": e.GuildID,
"userId": e.Author.ID,
"userId": e.Author.ID,
}).Debug("Join date below cutoff")
return
}
@ -149,18 +153,22 @@ func messageCreate(e *gateway.MessageCreateEvent) {
if err != nil {
log.WithFields(log.Fields{
"guildId": e.GuildID,
"userId": e.Author.ID,
"userId": e.Author.ID,
}).Debug("Unable to create private channel with member")
return
}
_, err = s.SendText(ch.ID, "")
message := viper.GetString("message")
replacer := strings.NewReplacer("{username}", e.Author.Username, "{message}", e.Content)
_, err = s.SendText(ch.ID, replacer.Replace(message))
if err != nil {
// Unable to send message
log.WithFields(log.Fields{
"guildId": e.GuildID,
"userId": e.Author.ID,
"userId": e.Author.ID,
}).Debug("Unable to send message in private channel")
}
}
}