added usage string

added command to print usage

updated readme
commands now accept arguments
master
Zachary Epps 4 years ago
parent 033758fb90
commit d0dfd84d26
  1. 16
      README.md
  2. 77
      main.go

@ -5,6 +5,16 @@ a discord bot that grabs quotes from a specified user and stores them in a datab
[Invite it](https://discordapp.com/api/oauth2/authorize?client_id=372856823889526785&permissions=67584&scope=bot) to a discord server and type `!grab @<user>` in a channel that the bot is in.
To retrieve a random quote it's `!quote @<user>`
`!quote-help` or `!quote-usage` prints the usage for the bot
To retrieve a quote further back in the channel's history pass `!grab` a number along with a user:
`!grab @<user> 2`
will grab that user's message, 2 messages back
similarly, `!quote @<user> 2` will return 2 random quotes from that user
# Self hosting
To build this you'll need Go 1.10+ because of the sqlite3 dependency. You'll also have a properly set up GOPATH and PATH that includes GOPATH/bin
@ -16,4 +26,8 @@ simply
or, if you want a different quote database file
`DiscordQuoteBot-Go -t "token" -d "newQuotes.db"`
`DiscordQuoteBot-Go -t "token" -d "newQuotes.db"`
to configure the bot to search further back for messages to grab:
`DiscordQuoteBot-Go -m 50 -t "token"`

@ -14,9 +14,21 @@ import (
"database/sql"
"math/rand"
"time"
"strconv"
)
const BaseUsageString string = `to save a quote ` + "`!grab @<user>`" + `
to get a random quote ` + "`!quote @<user>`" + `
adding a number after the username results in different behavior:
` + "`!grab @<user> 2`" + ` grabs 2 messages back relative to the command
` + "`!quote @<user> 2`" + ` pulls 2 random quotes from that user
Currently configured to search back a total of %d messages`
var (
UsageString string
MessageBacklogSearch int
Token string
DBFile string
DBConn *sql.DB
@ -25,11 +37,15 @@ var (
)
func main() {
// Get the bot token from the command line
// Get the commandline args
flag.IntVar(&MessageBacklogSearch, "m", 20, "the total number of messages back to search for a quote")
flag.StringVar(&Token, "t", "", "Bot Token")
flag.StringVar(&DBFile, "d", "quotes.db", "Name of database file")
flag.Parse()
UsageString = fmt.Sprintf(BaseUsageString, MessageBacklogSearch)
// if we have no token then we quit, cause we need that shit
if Token == "" {
flag.Usage()
return
@ -106,7 +122,7 @@ func main() {
return
}
fmt.Println("Bot is running. Ctrl+C to stop...")
log.Println("Bot is running. Ctrl+C to stop...")
sig := make(chan os.Signal, 1)
signal.Notify(sig, syscall.SIGTERM, syscall.SIGINT, os.Interrupt, os.Kill)
@ -129,11 +145,29 @@ func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
c, _ := s.Channel(m.ChannelID)
// print usage
if strings.HasPrefix(m.Content, "!quote-help") || strings.HasPrefix(m.Content, "!quote-usage") {
s.ChannelMessageSend(m.ChannelID, UsageString)
}
if strings.HasPrefix(m.Content, "!grab") {
// get the userID and set the default quote number to grab (1 == the last message that user posted)
user := strings.Trim(strings.Split(m.Content, " ")[1], "@!<>")
quoteNum := 1
// if we have more "arguments" to !grab then we assume it's a number
if splitMsg := strings.Split(m.Content, " "); len(splitMsg) == 3 {
var err error
quoteNum, err = strconv.Atoi(splitMsg[2])
if err != nil {
log.Println("Failure to convert argument to number, defaulting to 1")
quoteNum = 1
}
}
// gets the last 20 messages before the command
channelMsgs, err := s.ChannelMessages(m.ChannelID, 20, m.ID, "", "")
channelMsgs, err := s.ChannelMessages(m.ChannelID, MessageBacklogSearch, m.ID, "", "")
if err != nil {
log.Fatal("Error getting channel messages", err)
return
@ -142,13 +176,19 @@ func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
go func(userID string, messages []*discordgo.Message) {
// start going back through messages until we hit the last message
// sent by the user specified in userID
for i := 0; i < len(messages); i += 1 {
for i, qNum := 0, 0; i < len(messages); i += 1 {
if messages[i].Author.ID == userID {
// when we get that message we notify that channel
// and we send it to the database
s.ChannelMessageSend(m.ChannelID, "grabbed!")
toDB<-[]string{c.GuildID, userID, messages[i].Content}
return
// if it is a quote then we increase our quote counter
qNum += 1
// if it's the quote that we wanted then we save it!
if qNum == quoteNum {
// when we get that message we notify that channel
// and we send it to the database
s.ChannelMessageSend(m.ChannelID, "grabbed!")
toDB<-[]string{c.GuildID, userID, messages[i].Content}
return
}
}
}
@ -161,7 +201,19 @@ func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
}
if strings.HasPrefix(m.Content, "!quote") {
// get the user, and default out the number of quotes
user := strings.Trim(strings.Split(m.Content, " ")[1], "@!<>") // gets the userid
quoteNum := 1
// if we get an extra argument to !quote then we assume it's a number and get that many quotes
if splitMsg := strings.Split(m.Content, " "); len(splitMsg) == 3 {
var err error
quoteNum, err = strconv.Atoi(splitMsg[2])
if err != nil {
log.Println("Failure to convert argument to number, defaulting to 1")
quoteNum = 1
}
}
go func() {
queryData<-[]string{c.GuildID, user} // send our data
@ -169,7 +221,10 @@ func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
// if we get any quotes back, then we send a random one
if messages := <-queryData; len(messages) > 0 {
rand.Seed(time.Now().UTC().UnixNano())
s.ChannelMessageSend(m.ChannelID, messages[rand.Intn(len(messages))])
for i := 0; i < quoteNum; i++ {
s.ChannelMessageSend(m.ChannelID, messages[rand.Intn(len(messages))])
}
} else {
s.ChannelMessageSend(m.ChannelID, "Couldn't find anything :shrug:")
}
@ -197,7 +252,7 @@ func InitDB() (*sql.DB, error) {
}
if _, err := tmpDB.Query("SELECT * FROM quotes"); err != nil {
fmt.Println("Running initial setup for database...")
log.Println("Running initial setup for database...")
if _, err := tmpDB.Exec("CREATE TABLE quotes(Guild TEXT, UserID TEXT, Quote TEXT)"); err != nil {
log.Fatal("Could not create table", err)

Loading…
Cancel
Save