mirror of https://github.com/velour/catbase.git
Added die roller, fixed bot command addressing.
This commit is contained in:
parent
1099b66492
commit
4280440028
|
@ -81,8 +81,9 @@ func (b *Bot) checkHelp(channel string, parts []string) {
|
||||||
// Checks if message is a command and returns its curtailed version
|
// Checks if message is a command and returns its curtailed version
|
||||||
func (b *Bot) isCmd(message string) (bool, string) {
|
func (b *Bot) isCmd(message string) (bool, string) {
|
||||||
cmdc := b.Config.CommandChar
|
cmdc := b.Config.CommandChar
|
||||||
botnick := b.Conn.Me.Nick
|
botnick := strings.ToLower(b.Conn.Me.Nick)
|
||||||
iscmd := false
|
iscmd := false
|
||||||
|
message = strings.ToLower(message)
|
||||||
|
|
||||||
if strings.HasPrefix(message, cmdc) && len(cmdc) > 0 {
|
if strings.HasPrefix(message, cmdc) && len(cmdc) > 0 {
|
||||||
iscmd = true
|
iscmd = true
|
||||||
|
|
|
@ -6,7 +6,7 @@
|
||||||
"Plugins": [],
|
"Plugins": [],
|
||||||
"Server": "127.0.0.1:6666",
|
"Server": "127.0.0.1:6666",
|
||||||
"Nick": "alepaletest",
|
"Nick": "alepaletest",
|
||||||
"Pass": "alepaletest:test",
|
"Pass": "AlePaleTest:test",
|
||||||
"CommandChar": "!",
|
"CommandChar": "!",
|
||||||
"QuoteChance": 0.99,
|
"QuoteChance": 0.99,
|
||||||
"QuoteTime": 1,
|
"QuoteTime": 1,
|
||||||
|
@ -16,8 +16,6 @@
|
||||||
"UntappdToken": "<Your Token>",
|
"UntappdToken": "<Your Token>",
|
||||||
"UntappdFreq": 3600,
|
"UntappdFreq": 3600,
|
||||||
|
|
||||||
"comment": "Follows is the old bot",
|
|
||||||
|
|
||||||
"WelcomeMsgs": [
|
"WelcomeMsgs": [
|
||||||
"Real men use screen, %s.",
|
"Real men use screen, %s.",
|
||||||
"Joins upset the hivemind's OCD, %s.",
|
"Joins upset the hivemind's OCD, %s.",
|
||||||
|
@ -25,6 +23,8 @@
|
||||||
"%s, I WILL CUT YOU!"
|
"%s, I WILL CUT YOU!"
|
||||||
],
|
],
|
||||||
|
|
||||||
|
"comment": "Follows is the old bot",
|
||||||
|
|
||||||
"bot": {
|
"bot": {
|
||||||
"dbname": "deepintir",
|
"dbname": "deepintir",
|
||||||
"inchan": "out",
|
"inchan": "out",
|
||||||
|
|
1
main.go
1
main.go
|
@ -45,6 +45,7 @@ func main() {
|
||||||
// b.AddHandler(plugins.NewTestPlugin(b))
|
// b.AddHandler(plugins.NewTestPlugin(b))
|
||||||
b.AddHandler("admin", plugins.NewAdminPlugin(b))
|
b.AddHandler("admin", plugins.NewAdminPlugin(b))
|
||||||
b.AddHandler("talker", plugins.NewTalkerPlugin(b))
|
b.AddHandler("talker", plugins.NewTalkerPlugin(b))
|
||||||
|
b.AddHandler("dice", plugins.NewDicePlugin(b))
|
||||||
b.AddHandler("beers", plugins.NewBeersPlugin(b))
|
b.AddHandler("beers", plugins.NewBeersPlugin(b))
|
||||||
b.AddHandler("remember", plugins.NewRememberPlugin(b))
|
b.AddHandler("remember", plugins.NewRememberPlugin(b))
|
||||||
b.AddHandler("skeleton", plugins.NewSkeletonPlugin(b))
|
b.AddHandler("skeleton", plugins.NewSkeletonPlugin(b))
|
||||||
|
|
|
@ -0,0 +1,87 @@
|
||||||
|
package plugins
|
||||||
|
|
||||||
|
import "bitbucket.org/phlyingpenguin/godeepintir/bot"
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"math/rand"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// This is a dice plugin to serve as an example and quick copy/paste for new plugins.
|
||||||
|
|
||||||
|
type DicePlugin struct {
|
||||||
|
Bot *bot.Bot
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewDicePlugin creates a new DicePlugin with the Plugin interface
|
||||||
|
func NewDicePlugin(bot *bot.Bot) *DicePlugin {
|
||||||
|
return &DicePlugin{
|
||||||
|
Bot: bot,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func rollDie(sides int) int {
|
||||||
|
return rand.Intn(sides) + 1
|
||||||
|
}
|
||||||
|
|
||||||
|
// Message responds to the bot hook on recieving messages.
|
||||||
|
// This function returns true if the plugin responds in a meaningful way to the users message.
|
||||||
|
// Otherwise, the function returns false and the bot continues execution of other plugins.
|
||||||
|
func (p *DicePlugin) Message(message bot.Message) bool {
|
||||||
|
channel := message.Channel
|
||||||
|
parts := strings.Fields(message.Body)
|
||||||
|
|
||||||
|
log.Println(len(parts), parts, message.Command)
|
||||||
|
if (len(parts) == 2 || len(parts) == 1) && message.Command {
|
||||||
|
var dice []string
|
||||||
|
if len(parts) == 1 {
|
||||||
|
dice = strings.Split(parts[0], "d")
|
||||||
|
fmt.Println()
|
||||||
|
} else {
|
||||||
|
dice = strings.Split(parts[1], "d")
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Println(dice)
|
||||||
|
|
||||||
|
if len(dice) == 2 {
|
||||||
|
// We actually have a die roll.
|
||||||
|
nDice, _ := strconv.Atoi(dice[0])
|
||||||
|
sides, _ := strconv.Atoi(dice[1])
|
||||||
|
rolls := "You rolled: "
|
||||||
|
|
||||||
|
for i := 0; i < nDice; i++ {
|
||||||
|
rolls = fmt.Sprintf("%s %d", rolls, rollDie(sides))
|
||||||
|
if i != nDice-1 {
|
||||||
|
rolls = fmt.Sprintf("%s,", rolls)
|
||||||
|
} else {
|
||||||
|
rolls = fmt.Sprintf("%s.", rolls)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
p.Bot.SendMessage(channel, rolls)
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// LoadData imports any configuration data into the plugin. This is not strictly necessary other
|
||||||
|
// than the fact that the Plugin interface demands it exist. This may be deprecated at a later
|
||||||
|
// date.
|
||||||
|
func (p *DicePlugin) LoadData() {
|
||||||
|
rand.Seed(time.Now().Unix())
|
||||||
|
}
|
||||||
|
|
||||||
|
// Help responds to help requests. Every plugin must implement a help function.
|
||||||
|
func (p *DicePlugin) Help(channel string, parts []string) {
|
||||||
|
p.Bot.SendMessage(channel, "Roll dice using notation XdY. Try \"3d20\".")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Empty event handler because this plugin does not do anything on event recv
|
||||||
|
func (p *DicePlugin) Event(kind string, message bot.Message) bool {
|
||||||
|
return false
|
||||||
|
}
|
Loading…
Reference in New Issue