Last active
May 29, 2019 23:22
-
-
Save cabrinha/59c82cf62bdf4047d2783f9b13aefe11 to your computer and use it in GitHub Desktop.
store quotes from the channel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package quotes | |
import ( | |
"fmt" | |
"strings" | |
"github.com/Necroforger/dgrouter/exrouter" | |
"github.com/cabrinha/v2/plugins/store" | |
"github.com/go-redis/redis" | |
log "github.com/sirupsen/logrus" | |
) | |
// Grab a quote from known messages in a channel | |
func Grab(ctx *exrouter.Context) { | |
log.Infof("Grab from %s received with args: %s", ctx.Msg.Author.Username, strings.Join(ctx.Args, ",")) | |
channel, _ := ctx.Ses.State.Channel(ctx.Msg.ChannelID) | |
messages := channel.Messages | |
// args are all the arguments after the command | |
args := ctx.Args.After(1) | |
// If we have no args, grab the last message said | |
if args == "" { | |
// -2: we don't want to grab the `grab` command | |
msg := messages[len(messages)-2] | |
// If the bot said it, return | |
if msg.Author.ID == ctx.Ses.State.User.ID { | |
return | |
} | |
result := store.Client.LPush(fmt.Sprintf("%s:quotes", msg.Author.ID), msg.Content) | |
if result.Err() != redis.Nil { | |
log.Infof("Stored new quote for %s: %s", msg.Author.Username, msg.Content) | |
ctx.Reply("Grabbed.") | |
} else { | |
log.Errorf("Unable to store quote for %s: err: %v", msg.Author.Username, result.Err()) | |
return | |
} | |
for _, msg := range messages { | |
if strings.Contains(msg.Content, args) && !strings.Contains(msg.Content, "!grab") { | |
log.Infof("Found quote from %s: %s", msg.Author.Username, msg.Content) | |
result := store.Client.LPush(fmt.Sprintf("%s:quotes", msg.Author.ID), msg.Content) | |
if result.Err() != redis.Nil { | |
log.Infof("Stored new quote for %s: %s", msg.Author.Username, msg.Content) | |
ctx.Reply("Grabbed.") | |
} else { | |
log.Errorf("Unable to store quote for %s: err: %v", msg.Author.Username, result.Err()) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment