You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
145 lines
3.2 KiB
145 lines
3.2 KiB
package main |
|
|
|
import ( |
|
"fmt" |
|
"os" |
|
"os/signal" |
|
"syscall" |
|
"time" |
|
|
|
"app/cmd" |
|
|
|
"github.com/kf5grd/keybasebot" |
|
"github.com/kf5grd/keybasebot/pkg/kvstore" |
|
"samhofi.us/x/keybase/v2" |
|
"samhofi.us/x/keybase/v2/types/chat1" |
|
) |
|
|
|
func main() { |
|
opts := parseArgs(os.Args) |
|
b := keybasebot.New("", keybase.SetHomePath(opts.HomePath)) |
|
b.Debug = opts.Debug |
|
b.JSON = opts.JSON |
|
b.LogWriter = os.Stdout |
|
b.LogConv = chat1.ConvIDStr(opts.LogConvIDStr) |
|
// set the bot owner for private commands |
|
if opts.Owner != "" { |
|
b.Meta["owner"] = opts.Owner |
|
} |
|
if opts.KvStoreTeam != "" { |
|
b.Meta["kvstore"] = opts.KvStoreTeam |
|
loadNsfwAllowed(b) |
|
} |
|
// set reddit login, which enables reddit commands |
|
if opts.RedditUser != "" && opts.RedditPass != "" { |
|
b.Meta["reddit-user"] = opts.RedditUser |
|
b.Meta["reddit-pass"] = opts.RedditPass |
|
} |
|
|
|
// register the bot commands |
|
b.Commands = append(b.Commands, |
|
keybasebot.BotCommand{ |
|
Name: "ping", |
|
Ad: &cmd.PingAd, |
|
Run: keybasebot.Adapt( |
|
cmd.SendPong, |
|
keybasebot.MessageType("text"), |
|
keybasebot.CommandPrefix("!ping"), |
|
), |
|
}, |
|
keybasebot.BotCommand{ |
|
Name: "age", |
|
Ad: &cmd.AgeAd, |
|
Run: keybasebot.Adapt( |
|
cmd.Age, |
|
keybasebot.MessageType("text"), |
|
keybasebot.CommandPrefix("!age"), |
|
), |
|
}, |
|
keybasebot.BotCommand{ |
|
Name: "convert", |
|
Ad: &cmd.ConvertAd, |
|
Run: keybasebot.Adapt( |
|
cmd.Convert, |
|
keybasebot.MessageType("text"), |
|
keybasebot.CommandPrefix("!convert"), |
|
), |
|
}, |
|
keybasebot.BotCommand{ |
|
Name: "price", |
|
Ad: &cmd.PriceAd, |
|
Run: keybasebot.Adapt( |
|
cmd.SendPrice, |
|
keybasebot.MessageType("text"), |
|
keybasebot.CommandPrefix("!price"), |
|
), |
|
}, |
|
keybasebot.BotCommand{ |
|
Name: "nsfw", |
|
Ad: nil, |
|
Run: keybasebot.Adapt( |
|
setNsfw, |
|
keybasebot.MessageType("text"), |
|
keybasebot.CommandPrefix("!nsfw"), |
|
requireOwner(), |
|
), |
|
}, |
|
) |
|
// if there are reddit credentials add the reddit commands |
|
if opts.RedditPass != "" && opts.RedditUser != "" { |
|
b.Commands = append(b.Commands, |
|
keybasebot.BotCommand{ |
|
Name: "eyebleach", |
|
Ad: &cmd.EyebleachAd, |
|
Run: keybasebot.Adapt( |
|
cmd.Eyebleach, |
|
keybasebot.MessageType("text"), |
|
keybasebot.CommandPrefix("!eyebleach"), |
|
), |
|
}, |
|
keybasebot.BotCommand{ |
|
Name: "boobs", |
|
Ad: nil, |
|
Run: keybasebot.Adapt( |
|
cmd.Boobs, |
|
keybasebot.MessageType("text"), |
|
keybasebot.CommandPrefix("!boobs"), |
|
nsfwFilter(), |
|
), |
|
}, |
|
) |
|
fmt.Printf("%+v\n", opts) |
|
} |
|
|
|
// catch ctrl-c so we can clean up |
|
c := make(chan os.Signal) |
|
signal.Notify(c, os.Interrupt, syscall.SIGTERM) |
|
go func() { |
|
<-c |
|
b.Logger.Info("Caught SIGINT, cleaning up.") |
|
b.KB.ClearCommands() |
|
b.Logger.Info("Cleared command adverts. Bye.") |
|
time.Sleep(time.Second * 2) |
|
os.Exit(0) |
|
}() |
|
// then run |
|
b.Run() |
|
} |
|
|
|
func loadNsfwAllowed(b *keybasebot.Bot) { |
|
// get the team from meta |
|
data, ok := b.Meta["kvstore"] |
|
if ok { |
|
// make something to fill |
|
allowedTeams := make(map[chat1.ConvIDStr]bool) |
|
team := data.(string) |
|
kvKey := kvstore.New("nsfwAllowed", &allowedTeams, -1) |
|
err := kvstore.Get(b.KB, team, "ssh0le", &kvKey) |
|
if err != nil { |
|
return |
|
} |
|
// set the allowed teams into meta |
|
b.Meta["nsfwAllowed"] = allowedTeams |
|
} |
|
return |
|
} |