Support message placeholders and configuration of message
This commit is contained in:
parent
00377f7670
commit
05e12f4436
28
main.go
28
main.go
@ -13,18 +13,22 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
s *state.State
|
s *state.State
|
||||||
channels []discord.ChannelID
|
channels []discord.ChannelID
|
||||||
prohibitedPhrases []string
|
prohibitedPhrases []string
|
||||||
)
|
)
|
||||||
|
|
||||||
func setupConfiguration() {
|
func setupConfiguration() {
|
||||||
viper.SetDefault("prohibitedPhrases", "")
|
viper.SetDefault("prohibitedPhrases", "")
|
||||||
viper.SetDefault("channels", "")
|
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.AutomaticEnv()
|
||||||
|
|
||||||
viper.SetConfigName("ottomate")
|
viper.SetConfigName("ottomate")
|
||||||
|
viper.AddConfigPath("/etc/ottomate/")
|
||||||
|
viper.AddConfigPath("$HOME/.ottomate")
|
||||||
|
viper.AddConfigPath(".")
|
||||||
|
|
||||||
viper.ReadInConfig()
|
viper.ReadInConfig()
|
||||||
}
|
}
|
||||||
@ -78,7 +82,7 @@ func main() {
|
|||||||
|
|
||||||
ch := make(chan os.Signal)
|
ch := make(chan os.Signal)
|
||||||
signal.Notify(ch, os.Kill, os.Interrupt)
|
signal.Notify(ch, os.Kill, os.Interrupt)
|
||||||
<- ch
|
<-ch
|
||||||
}
|
}
|
||||||
|
|
||||||
func messageCreate(e *gateway.MessageCreateEvent) {
|
func messageCreate(e *gateway.MessageCreateEvent) {
|
||||||
@ -117,8 +121,8 @@ func messageCreate(e *gateway.MessageCreateEvent) {
|
|||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.WithFields(log.Fields{
|
log.WithFields(log.Fields{
|
||||||
"guildId": e.GuildID,
|
"guildId": e.GuildID,
|
||||||
"userId": e.Author.ID,
|
"userId": e.Author.ID,
|
||||||
"messageId": e.ID,
|
"messageId": e.ID,
|
||||||
}).Debug("Unable to delete message")
|
}).Debug("Unable to delete message")
|
||||||
return
|
return
|
||||||
@ -129,7 +133,7 @@ func messageCreate(e *gateway.MessageCreateEvent) {
|
|||||||
if err != nil || !member.Joined.IsValid() {
|
if err != nil || !member.Joined.IsValid() {
|
||||||
log.WithFields(log.Fields{
|
log.WithFields(log.Fields{
|
||||||
"guildId": e.GuildID,
|
"guildId": e.GuildID,
|
||||||
"userId": e.Author.ID,
|
"userId": e.Author.ID,
|
||||||
}).Debug("Unable to retrieve guild member")
|
}).Debug("Unable to retrieve guild member")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -139,7 +143,7 @@ func messageCreate(e *gateway.MessageCreateEvent) {
|
|||||||
if joinTime.Before(time.Now().AddDate(0, 0, -7)) {
|
if joinTime.Before(time.Now().AddDate(0, 0, -7)) {
|
||||||
log.WithFields(log.Fields{
|
log.WithFields(log.Fields{
|
||||||
"guildId": e.GuildID,
|
"guildId": e.GuildID,
|
||||||
"userId": e.Author.ID,
|
"userId": e.Author.ID,
|
||||||
}).Debug("Join date below cutoff")
|
}).Debug("Join date below cutoff")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -149,18 +153,22 @@ func messageCreate(e *gateway.MessageCreateEvent) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
log.WithFields(log.Fields{
|
log.WithFields(log.Fields{
|
||||||
"guildId": e.GuildID,
|
"guildId": e.GuildID,
|
||||||
"userId": e.Author.ID,
|
"userId": e.Author.ID,
|
||||||
}).Debug("Unable to create private channel with member")
|
}).Debug("Unable to create private channel with member")
|
||||||
return
|
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 {
|
if err != nil {
|
||||||
// Unable to send message
|
// Unable to send message
|
||||||
log.WithFields(log.Fields{
|
log.WithFields(log.Fields{
|
||||||
"guildId": e.GuildID,
|
"guildId": e.GuildID,
|
||||||
"userId": e.Author.ID,
|
"userId": e.Author.ID,
|
||||||
}).Debug("Unable to send message in private channel")
|
}).Debug("Unable to send message in private channel")
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user