本文整理匯總了Golang中github.com/fluffle/sp0rkle/bot.Context.Storable方法的典型用法代碼示例。如果您正苦於以下問題:Golang Context.Storable方法的具體用法?Golang Context.Storable怎麽用?Golang Context.Storable使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/fluffle/sp0rkle/bot.Context
的用法示例。
在下文中一共展示了Context.Storable方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: urlScan
func urlScan(ctx *bot.Context) {
words := strings.Split(ctx.Text(), " ")
n, c := ctx.Storable()
for _, w := range words {
if util.LooksURLish(w) {
if u := uc.GetByUrl(w); u != nil {
if u.Nick != bot.Nick(ctx.Nick) &&
time.Since(u.Timestamp) > 2*time.Hour {
ctx.Reply("that URL first mentioned by %s %s ago",
u.Nick, util.TimeSince(u.Timestamp))
}
continue
}
u := urls.NewUrl(w, n, c)
if len(w) > autoShortenLimit && ctx.Public() {
u.Shortened = Encode(w)
}
if err := uc.Insert(u); err != nil {
ctx.ReplyN("Couldn't insert url '%s': %s", w, err)
continue
}
if u.Shortened != "" {
ctx.Reply("%s's URL shortened as %s%s%s",
ctx.Nick, bot.HttpHost(), shortenPath, u.Shortened)
}
lastseen[ctx.Target()] = u.Id
}
}
}
示例2: insert
// Factoid add: 'key := value' or 'key :is value'
func insert(ctx *bot.Context) {
if !ctx.Addressed || !util.IsFactoidAddition(ctx.Text()) {
return
}
var key, val string
if strings.Index(ctx.Text(), ":=") != -1 {
kv := strings.SplitN(ctx.Text(), ":=", 2)
key = ToKey(kv[0], false)
val = strings.TrimSpace(kv[1])
} else {
// we use :is to add val = "key is val"
kv := strings.SplitN(ctx.Text(), ":is", 2)
key = ToKey(kv[0], false)
val = strings.Join([]string{strings.TrimSpace(kv[0]),
"is", strings.TrimSpace(kv[1])}, " ")
}
n, c := ctx.Storable()
fact := factoids.NewFactoid(key, val, n, c)
// The "randomwoot" factoid contains random positive phrases for success.
joy := "Woo"
if rand := fc.GetPseudoRand("randomwoot"); rand != nil {
joy = rand.Value
}
if err := fc.Insert(fact); err == nil {
count := fc.GetCount(key)
LastSeen(ctx.Target(), fact.Id)
ctx.ReplyN("%s, I now know %d things about '%s'.", joy, count, key)
} else {
ctx.ReplyN("Error storing factoid: %s.", err)
}
}
示例3: set
// remind
func set(ctx *bot.Context) {
// s == <target> <reminder> in|at|on <time>
s := strings.Fields(ctx.Text())
if len(s) < 4 {
ctx.ReplyN("You asked me to remind %s.", ctx.Text())
return
}
at, ok, reminder, timestr := time.Now(), false, "", ""
for i := 1; i+1 < len(s); i++ {
lc := strings.ToLower(s[i])
if lc == "in" || lc == "at" || lc == "on" {
timestr = strings.Join(s[i+1:], " ")
} else if i+2 == len(s) {
// Hack to test the last word for e.g. "tomorrow"
i++
timestr = strings.ToLower(s[i])
} else {
continue
}
// TODO(fluffle): surface better errors from datetime.Parse
at, ok = datetime.Parse(timestr)
if ok {
reminder = strings.Join(s[1:i], " ")
break
}
}
if reminder == "" {
ctx.ReplyN("You asked me to remind %s.", ctx.Text())
return
}
if !ok {
ctx.ReplyN("Couldn't parse time string '%s'", timestr)
return
}
now := time.Now()
start := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local)
if at.Before(now) && at.After(start) {
at = at.Add(24 * time.Hour)
}
if at.Before(now) {
ctx.ReplyN("Time '%s' is in the past.", timestr)
return
}
n, c := ctx.Storable()
// TODO(fluffle): Use state tracking! And do this better.
t := bot.Nick(s[0])
if t.Lower() == strings.ToLower(ctx.Nick) ||
t.Lower() == "me" {
t = n
}
r := reminders.NewReminder(reminder, at, t, n, c)
if err := rc.Insert(r); err != nil {
ctx.ReplyN("Error saving reminder: %v", err)
return
}
// Any previously-generated list of reminders is now obsolete.
delete(listed, ctx.Nick)
ctx.ReplyN("%s", r.Acknowledge())
Remind(r, ctx)
}
示例4: recordKick
func recordKick(ctx *bot.Context) {
n, c := ctx.Storable()
kn := bot.Nick(ctx.Text())
// seenNickFromLine doesn't work with the hacks for KICKING and KICKED
// First, handle KICKING
kr := sc.LastSeenDoing(ctx.Nick, "KICKING")
if kr == nil {
kr = seen.SawNick(n, c, "KICKING", ctx.Args[2])
} else {
kr.Nick, kr.Chan = n, c
kr.Timestamp, kr.Text = time.Now(), ctx.Args[2]
}
kr.OtherNick = kn
_, err := sc.Upsert(kr.Id(), kr)
if err != nil {
ctx.Reply("Failed to store seen data: %v", err)
}
// Now, handle KICKED
ke := sc.LastSeenDoing(ctx.Text(), "KICKED")
if ke == nil {
ke = seen.SawNick(kn, c, "KICKED", ctx.Args[2])
} else {
ke.Nick, ke.Chan = kn, c
ke.Timestamp, ke.Text = time.Now(), ctx.Args[2]
}
ke.OtherNick = n
_, err = sc.Upsert(ke.Id(), ke)
if err != nil {
ctx.Reply("Failed to store seen data: %v", err)
}
}
示例5: tell
// tell
func tell(ctx *bot.Context) {
// s == <target> <stuff>
txt := ctx.Text()
idx := strings.Index(txt, " ")
if idx == -1 {
ctx.ReplyN("Tell who what?")
return
}
tell := txt[idx+1:]
n, c := ctx.Storable()
t := bot.Nick(txt[:idx])
if t.Lower() == strings.ToLower(ctx.Nick) ||
t.Lower() == "me" {
ctx.ReplyN("You're a dick. Oh, wait, that wasn't *quite* it...")
return
}
r := reminders.NewTell(tell, t, n, c)
if err := rc.Insert(r); err != nil {
ctx.ReplyN("Error saving tell: %v", err)
return
}
if s := pc.GetByNick(txt[:idx]); s.CanPush() {
push.Push(s, fmt.Sprintf("%s in %s asked me to tell you:",
ctx.Nick, ctx.Target()), tell)
}
// Any previously-generated list of reminders is now obsolete.
delete(listed, ctx.Nick)
ctx.ReplyN("%s", r.Acknowledge())
}
示例6: add
func add(ctx *bot.Context) {
n, c := ctx.Storable()
quote := quotes.NewQuote(ctx.Text(), n, c)
quote.QID = qc.NewQID()
if err := qc.Insert(quote); err == nil {
ctx.ReplyN("Quote added succesfully, id #%d.", quote.QID)
} else {
ctx.ReplyN("Error adding quote: %s.", err)
}
}
示例7: seenNickFromLine
// Look up or create a "seen" entry for the line.
// Explicitly don't handle updating line.Text or line.OtherNick
func seenNickFromLine(ctx *bot.Context) *seen.Nick {
sn := sc.LastSeenDoing(ctx.Nick, ctx.Cmd)
n, c := ctx.Storable()
if sn == nil {
sn = seen.SawNick(n, c, ctx.Cmd, "")
} else {
sn.Nick, sn.Chan = n, c
sn.Timestamp = time.Now()
}
return sn
}
示例8: chance
// Factoid chance: 'chance of that is' => sets chance of lastSeen[chan]
func chance(ctx *bot.Context) {
str := ctx.Text()
var chance float64
if strings.HasSuffix(str, "%") {
// Handle 'chance of that is \d+%'
if i, err := strconv.Atoi(str[:len(str)-1]); err != nil {
ctx.ReplyN("'%s' didn't look like a % chance to me.", str)
return
} else {
chance = float64(i) / 100
}
} else {
// Assume the chance is a floating point number.
if c, err := strconv.ParseFloat(str, 64); err != nil {
ctx.ReplyN("'%s' didn't look like a chance to me.", str)
return
} else {
chance = c
}
}
// Make sure the chance we've parsed lies in (0.0,1.0]
if chance > 1.0 || chance <= 0.0 {
ctx.ReplyN("'%s' was outside possible chance ranges.", str)
return
}
// Retrieve last seen ObjectId, replace with ""
ls := LastSeen(ctx.Target(), "")
// ok, we're good to update the chance.
if fact := fc.GetById(ls); fact != nil {
// Store the old chance, update with the new
old := fact.Chance
fact.Chance = chance
// Update the Modified field
fact.Modify(ctx.Storable())
// And store the new factoid data
if err := fc.Update(bson.M{"_id": ls}, fact); err == nil {
ctx.ReplyN("'%s' was at %.0f%% chance, now is at %.0f%%.",
fact.Key, old*100, chance*100)
} else {
ctx.ReplyN("I failed to replace '%s': %s", fact.Key, err)
}
} else {
ctx.ReplyN("Whatever that was, I've already forgotten it.")
}
}
示例9: recordStats
func recordStats(ctx *bot.Context) {
ns := sc.StatsFor(ctx.Nick, ctx.Target())
if ns == nil {
n, c := ctx.Storable()
ns = stats.NewStat(n, c)
}
ns.Update(ctx.Text())
if ns.Lines%10000 == 0 {
ctx.Reply("%s has said %d lines in this channel and "+
"should now shut the fuck up and do something useful",
ctx.Nick, ns.Lines)
}
if _, err := sc.Upsert(ns.Id(), ns); err != nil {
ctx.Reply("Failed to store stats data: %v", err)
}
}
示例10: lookup
func lookup(ctx *bot.Context) {
// Only perform extra prefix removal if we weren't addressed directly
key := ToKey(ctx.Text(), !ctx.Addressed)
var fact *factoids.Factoid
if fact = fc.GetPseudoRand(key); fact == nil && ctx.Cmd == client.ACTION {
// Support sp0rkle's habit of stripping off it's own nick
// but only for actions, not privmsgs.
if strings.HasSuffix(key, ctx.Me()) {
key = strings.TrimSpace(key[:len(key)-len(ctx.Me())])
fact = fc.GetPseudoRand(key)
}
}
if fact == nil {
return
}
// Chance is used to limit the rate of factoid replies for things
// people say a lot, like smilies, or 'lol', or 'i love the peen'.
chance := fact.Chance
if key == "" {
// This is doing a "random" lookup, triggered by someone typing in
// something entirely composed of the chars stripped by ToKey().
// To avoid making this too spammy, forcibly limit the chance to 40%.
chance = 0.4
}
if rand.Float64() < chance {
// Store this as the last seen factoid
LastSeen(ctx.Target(), fact.Id)
// Update the Accessed field
// TODO(fluffle): fd should take care of updating Accessed internally
fact.Access(ctx.Storable())
// And store the new factoid data
if err := fc.Update(bson.M{"_id": fact.Id}, fact); err != nil {
ctx.ReplyN("I failed to update '%s' (%s): %s ",
fact.Key, fact.Id, err)
}
recurse(fact, map[string]bool{key: true})
switch fact.Type {
case factoids.F_ACTION:
ctx.Do("%s", fact.Value)
default:
ctx.Reply("%s", fact.Value)
}
}
}
示例11: smoke
func smoke(ctx *bot.Context) {
if !smokeRx.MatchString(ctx.Text()) {
return
}
sn := sc.LastSeenDoing(ctx.Nick, "SMOKE")
n, c := ctx.Storable()
if sn != nil {
ctx.ReplyN("You last went for a smoke %s ago...",
util.TimeSince(sn.Timestamp))
sn.Nick, sn.Chan = n, c
sn.Timestamp = time.Now()
} else {
sn = seen.SawNick(n, c, "SMOKE", "")
}
if _, err := sc.Upsert(sn.Id(), sn); err != nil {
ctx.Reply("Failed to store smoke data: %v", err)
}
}
示例12: recordKarma
func recordKarma(ctx *bot.Context) {
// Karma can look like some.text.string++ or (text with spaces)--
// and there could be multiple occurrences of it in a string.
nick, _ := ctx.Storable()
for _, kt := range karmaThings(ctx.Text()) {
k := kc.KarmaFor(kt.thing)
if k == nil {
k = karma.New(kt.thing)
}
if kt.plus {
k.Plus(nick)
} else {
k.Minus(nick)
}
if _, err := kc.Upsert(k.Id(), k); err != nil {
ctx.Reply("Failed to insert Karma: %s", err)
}
}
}
示例13: cache
func cache(ctx *bot.Context) {
var u *urls.Url
if ctx.Text() == "" {
// assume we have been given "cache that"
if u = uc.GetById(lastseen[ctx.Target()]); u == nil {
ctx.ReplyN("I seem to have forgotten what to cache")
return
}
if u.CachedAs != "" {
ctx.ReplyN("That was already cached as %s%s%s at %s",
bot.HttpHost(), cachePath, u.CachedAs,
datetime.Format(u.CacheTime))
return
}
} else {
url := strings.TrimSpace(ctx.Text())
if idx := strings.Index(url, " "); idx != -1 {
url = url[:idx]
}
if !util.LooksURLish(url) {
ctx.ReplyN("'%s' doesn't look URLish", url)
return
}
if u = uc.GetByUrl(url); u == nil {
n, c := ctx.Storable()
u = urls.NewUrl(url, n, c)
} else if u.CachedAs != "" {
ctx.ReplyN("That was already cached as %s%s%s at %s",
bot.HttpHost(), cachePath, u.CachedAs,
datetime.Format(u.CacheTime))
return
}
}
if err := Cache(u); err != nil {
ctx.ReplyN("Failed to store cached url: %s", err)
return
}
ctx.ReplyN("%s cached as %s%s%s",
u.Url, bot.HttpHost(), cachePath, u.CachedAs)
}
示例14: shorten
func shorten(ctx *bot.Context) {
var u *urls.Url
if ctx.Text() == "" {
// assume we have been given "shorten that"
if u = uc.GetById(lastseen[ctx.Target()]); u == nil {
ctx.ReplyN("I seem to have forgotten what to shorten")
return
}
if u.Shortened != "" {
ctx.ReplyN("That was already shortened as %s%s%s",
bot.HttpHost(), shortenPath, u.Shortened)
return
}
} else {
url := strings.TrimSpace(ctx.Text())
if idx := strings.Index(url, " "); idx != -1 {
url = url[:idx]
}
if !util.LooksURLish(url) {
ctx.ReplyN("'%s' doesn't look URLish", url)
return
}
if u = uc.GetByUrl(url); u == nil {
n, c := ctx.Storable()
u = urls.NewUrl(url, n, c)
} else if u.Shortened != "" {
ctx.ReplyN("That was already shortened as %s%s%s",
bot.HttpHost(), shortenPath, u.Shortened)
return
}
}
if err := Shorten(u); err != nil {
ctx.ReplyN("Failed to store shortened url: %s", err)
return
}
ctx.ReplyN("%s shortened to %s%s%s",
u.Url, bot.HttpHost(), shortenPath, u.Shortened)
}
示例15: replace
// Factoid replace: 'replace that with' => updates lastSeen[chan]
func replace(ctx *bot.Context) {
ls := LastSeen(ctx.Target(), "")
if fact := fc.GetById(ls); fact != nil {
// Store the old factoid value
old := fact.Value
// Replace the value with the new one
fact.Value = ctx.Text()
// Update the Modified field
fact.Modify(ctx.Storable())
// And store the new factoid data
if err := fc.Update(bson.M{"_id": ls}, fact); err == nil {
ctx.ReplyN("'%s' was '%s', now is '%s'.",
fact.Key, old, fact.Value)
} else {
ctx.ReplyN("I failed to replace '%s': %s", fact.Key, err)
}
} else {
ctx.ReplyN("Whatever that was, I've already forgotten it.")
}
}