Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement io.Reader, io.Writer for quote database #8

Merged
merged 4 commits into from
Dec 22, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 6 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
# The base go-image
FROM golang:1.21-alpine
# Builder image
FROM golang:1.21-alpine as build

RUN mkdir /app
COPY . /app

WORKDIR /app
RUN go build
Alextopher marked this conversation as resolved.
Show resolved Hide resolved

RUN go build -v
# Server image
FROM gcr.io/distroless/static-debian12

# Run the server executable
COPY --from=build /app/boyd_cooper /app/boyd_cooper
CMD [ "/app/boyd_cooper" ]
Alextopher marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 1 addition & 1 deletion commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ var handlers = map[string]func(*discordgo.Session, *discordgo.InteractionCreate)
return
}

err := writeQuote(quote)
err := writeQuote(quoteFile, quote)
if err != nil {
sendError(s, d, err)
return
Expand Down
9 changes: 7 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"github.com/joho/godotenv"
)

var quoteFile *os.File

func main() {
log.Println("Starting BoydBot...")
if err := godotenv.Load(); err != nil {
Expand All @@ -30,13 +32,16 @@ func main() {
}

// open the quotes file
quoteFile, err = os.OpenFile("quotes.txt", os.O_RDWR|os.O_APPEND|os.O_CREATE, 0644)
quoteFile, err := os.OpenFile("quotes.txt", os.O_RDWR|os.O_APPEND|os.O_CREATE, 0644)
if err != nil {
panic(err)
}
defer quoteFile.Close()

// Load the quotes
loadQuotes()
log.Println("Loading quotes...")
loadQuotes(quoteFile)
log.Println("Loaded", len(quoteList), "quotes")

// Index quotes
indexQuotes()
Expand Down
18 changes: 7 additions & 11 deletions quotes.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,23 @@ package main

import (
"bufio"
"log"
"os"
"io"
)

var quoteList []string
var quoteFile *os.File

// Loads the quote list from a file
func loadQuotes() {
log.Println("Loading quotes...")
// Loads the quote list from an input stream
func loadQuotes(input io.Reader) {
quoteList = make([]string, 0)

scanner := bufio.NewScanner(bufio.NewReader(quoteFile))
scanner := bufio.NewScanner(bufio.NewReader(input))
for scanner.Scan() {
quoteList = append(quoteList, scanner.Text())
}
log.Println("Loaded", len(quoteList), "quotes")
}

// Saves a quote to the database
func writeQuote(quote string) error {
_, err := quoteFile.WriteString(quote + "\n")
// Writes a quote to an output stream
func writeQuote(output io.Writer, quote string) error {
_, err := output.Write([]byte(quote + "\n"))
return err
}
20 changes: 6 additions & 14 deletions theory.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,13 @@ func buildSentence(asideChance uint32, interjectionChance uint32) string {
aside := [2]string{"Visiting hours are over!", "Why does that hydrant keep looking at me?"}
interjection := [15]string{"*chuckles*", "(Ho ho!)", "(Wait...)", "(Uh...)", "(Um...)", "*cough*", "(Uh...)", "(Hmm...)", "(Ha!)", "(Yeah, yeah, yeah...)", "(What?)", "(No, no, nonono...)", "(Okay, okay but...)", "(Huh?)", "(Oh-hoh, RIGHT...)"}

sentence := ""
if randGen.Uint32()%asideChance == 0 {
sentence = aside[rand.Uint32()%2]
return sentence
}
if randGen.Uint32()%interjectionChance == 0 {
sentence = interjection[rand.Uint32()%15]
return sentence
}

if randGen.Int()%2 == 0 {
sentence = subjects[rand.Int()%40] + " " + transitiveVerb[rand.Int()%12] + " " + object[rand.Int()%17] + " " + conclution[rand.Int()%9]
return aside[rand.Uint32()%2]
} else if randGen.Uint32()%interjectionChance == 0 {
return interjection[rand.Uint32()%15]
} else if randGen.Int()%2 == 0 {
return subjects[rand.Int()%40] + " " + transitiveVerb[rand.Int()%12] + " " + object[rand.Int()%17] + " " + conclution[rand.Int()%9]
} else {
sentence = subjects[rand.Uint32()%40] + " " + subjectConnector[rand.Uint32()%8] + " " + subjects[rand.Uint32()%39] + " " + intransitiveVerb[rand.Uint32()%17] + " " + preposition[rand.Uint32()%7] + " " + object[rand.Uint32()%17]
return subjects[rand.Uint32()%40] + " " + subjectConnector[rand.Uint32()%8] + " " + subjects[rand.Uint32()%39] + " " + intransitiveVerb[rand.Uint32()%17] + " " + preposition[rand.Uint32()%7] + " " + object[rand.Uint32()%17]
}

return sentence
}