Add compliment command and refactor (#7)

Update example config

Signed-off-by: Etzelia <etzelia@hotmail.com>

Add compliment command and refactor

Signed-off-by: Etzelia <etzelia@hotmail.com>

Reviewed-on: https://git.etztech.xyz/Etzelia/sedbot/pulls/7
ban
Etzelia 2020-07-02 23:52:20 +02:00
parent 66a418261e
commit f204ee6e37
7 changed files with 70 additions and 16 deletions

View File

@ -13,6 +13,7 @@ BirbMC Discord generi-bot.
* `inspire` - Get a random "inspirational" message from [InspiroBot](https://inspirobot.me)
* `dad` - Get a random dad joke from [icanhazdadjoke](https://icanhazdadjoke.com)
* `insult` - The fan favorite returns
* `compliment` - The other fan favorite returns
* `names <in-game name> [<01/02/2006>]` - Minecraft name history (optionally at a specific time)
### Moderation

View File

@ -29,6 +29,11 @@ type Config struct {
Adjectives []string `toml:"adjectives"`
Nouns []string `toml:"nouns"`
} `toml:"insult"`
Compliment struct {
Verbs []string `toml:"verbs"`
Nouns []string `toml:"nouns"`
MinorThings []string `toml:"minor_things"`
} `toml:"compliment"`
}
type MessageRole struct {

View File

@ -0,0 +1,42 @@
package discord
import (
"fmt"
"strings"
)
func init() {
commands["compliment"] = command{
validate: func(cmd commandInit) bool {
return true
},
run: func(cmd commandInit) (string, error) {
if !memeRateLimit.Try() {
return "", nil
}
fields := strings.Fields(cmd.message.Content)
var target string
if len(fields) > 1 {
target = strings.Join(fields[1:], " ")
} else if cmd.message.Member.Nick != "" {
target = cmd.message.Member.Nick
} else {
target = cmd.message.Author.Username
}
compliment := fmt.Sprintf("%s, I would %s my %s just to %s.",
target,
random(cmd.config.Compliment.Verbs),
random(cmd.config.Compliment.Nouns),
random(cmd.config.Compliment.MinorThings),
)
sendMessage(cmd.session, cmd.message.ChannelID, compliment, true)
return "", nil
},
help: "Compliment someone!",
}
}

View File

@ -74,7 +74,7 @@ func sendMessage(s *discordgo.Session, channelID, content string, scrub bool) *d
var err error
if scrub {
msg, err = s.ChannelMessageSendComplex(channelID, &discordgo.MessageSend{
Content: content,
Content: content,
AllowedMentions: &discordgo.MessageAllowedMentions{},
})
} else {

View File

@ -2,9 +2,7 @@ package discord
import (
"fmt"
r "math/rand"
"strings"
"time"
)
func init() {
@ -43,16 +41,3 @@ func init() {
help: "Insult someone!",
}
}
var rand = r.New(r.NewSource(time.Now().Unix()))
func random(list []string) string {
size := len(list)
if size == 0 {
return ""
} else if size == 1 {
return list[0]
}
idx := rand.Intn(size)
return list[idx]
}

View File

@ -1,6 +1,7 @@
package discord
import (
r "math/rand"
"time"
)
@ -24,3 +25,16 @@ func (r *rateLimit) Try() bool {
}
return false
}
var rand = r.New(r.NewSource(time.Now().Unix()))
func random(list []string) string {
size := len(list)
if size == 0 {
return ""
} else if size == 1 {
return list[0]
}
idx := rand.Intn(size)
return list[idx]
}

View File

@ -36,6 +36,13 @@ comparisons = []
adjectives = []
nouns = []
# compliments
# <args>, I would <verb> my <noun> just to <minor thing>.
[compliment]
verbs = []
nouns = []
minor_things = []
# echoes are any basic command -> message
[[echoes]]
name = "discord"