Compare commits
18 Commits
14e68fa3ed
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| ae7a0b79d9 | |||
| 0453f02dd0 | |||
| 0f8fe62587 | |||
| c13053f116 | |||
| b765f1cbb1 | |||
| a914fcdbf9 | |||
| 6510e1abc3 | |||
| 51ad3d22f2 | |||
| a1878fb39a | |||
| b9b9dd0e10 | |||
| 2fec1fe46f | |||
| 6be3f231db | |||
| 52b4bb6a1a | |||
| 17d9128577 | |||
| f98837bc9d | |||
| a33446e5e7 | |||
| 6966568ec1 | |||
| bef0de7e5b |
10
README.md
10
README.md
@@ -10,6 +10,12 @@ Restore previous functionality:
|
||||
- Add compute
|
||||
- Add voice commands
|
||||
|
||||
Add caching mechanism for images
|
||||
New functionalities:
|
||||
- ~~Add caching mechanism for images~~
|
||||
- Routely delete cache
|
||||
- Make sure same image isn't repeated for x amount of time
|
||||
- ~~Add ability to make coinflip with options to replace heads/tails~~
|
||||
- Add statistics tracking
|
||||
|
||||
Add statistics tracking
|
||||
## Building standalone
|
||||
Instructions for myself on how to build this for an Alpine Linux container: CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build bunnybot.go commands.go images.go auth.go
|
||||
|
||||
12
bunnybot.go
12
bunnybot.go
@@ -14,7 +14,6 @@ import (
|
||||
|
||||
var (
|
||||
auth Auth
|
||||
redditbooru_sources []string
|
||||
|
||||
// cache settings
|
||||
cache_location string = "cache/"
|
||||
@@ -28,9 +27,6 @@ func init() {
|
||||
// build the authentication struct
|
||||
build_auth()
|
||||
|
||||
// build the redditbooru sources slice
|
||||
build_redditbooru_sources()
|
||||
|
||||
// attempt to make our cached
|
||||
if _, err := os.Stat(cache_location); os.IsNotExist(err) {
|
||||
os.Mkdir(cache_location, 0777)
|
||||
@@ -90,6 +86,8 @@ func message_create (s *discordgo.Session, m *discordgo.MessageCreate) {
|
||||
return
|
||||
}
|
||||
|
||||
} else { // message too short, don't do anything
|
||||
return
|
||||
}
|
||||
|
||||
// get our message's content without our bot's token
|
||||
@@ -100,11 +98,15 @@ func message_create (s *discordgo.Session, m *discordgo.MessageCreate) {
|
||||
|
||||
// determine our actions
|
||||
if message[0] == "coinflip" || message[0] == "coin" { // flip a coin
|
||||
s.ChannelMessageSend(m.ChannelID, coinflip(m.Author.ID))
|
||||
s.ChannelMessageSend(m.ChannelID, coinflip(m.Author.ID, content))
|
||||
} else if message[0] == "roll" { // roll a number
|
||||
s.ChannelMessageSend(m.ChannelID, roll(m.Author.ID))
|
||||
} else if message[0] == "source" { // print source code
|
||||
s.ChannelMessageSend(m.ChannelID, source())
|
||||
} else if message[0] == "retarded" { // retarded youtube video
|
||||
s.ChannelMessageSend(m.ChannelID, "https://youtu.be/kav7tifmyTg")
|
||||
} else if message[0] == "moon" { // wsb moon stock ticker copypasta
|
||||
s.ChannelMessageSend(m.ChannelID, moon(content)) // print moon text
|
||||
} else if len(message[0]) > 0 { // as long as there is a message, try to find a picture
|
||||
// get url
|
||||
url := <-get_image(message[0])
|
||||
|
||||
41
commands.go
41
commands.go
File diff suppressed because one or more lines are too long
14
go.mod
Normal file
14
go.mod
Normal file
@@ -0,0 +1,14 @@
|
||||
module GoBunnyBot
|
||||
|
||||
go 1.17
|
||||
|
||||
require (
|
||||
github.com/buger/jsonparser v1.1.1
|
||||
github.com/bwmarrin/discordgo v0.27.0
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/gorilla/websocket v1.5.0 // indirect
|
||||
golang.org/x/crypto v0.7.0 // indirect
|
||||
golang.org/x/sys v0.6.0 // indirect
|
||||
)
|
||||
45
go.sum
Normal file
45
go.sum
Normal file
@@ -0,0 +1,45 @@
|
||||
github.com/buger/jsonparser v1.1.1 h1:2PnMjfWD7wBILjqQbt530v576A/cAbQvEW9gGIpYMUs=
|
||||
github.com/buger/jsonparser v1.1.1/go.mod h1:6RYKKt7H4d4+iWqouImQ9R2FZql3VbhNgx27UK13J/0=
|
||||
github.com/bwmarrin/discordgo v0.27.0 h1:4ZK9KN+rGIxZ0fdGTmgdCcliQeW8Zhu6MnlFI92nf0Q=
|
||||
github.com/bwmarrin/discordgo v0.27.0/go.mod h1:NJZpH+1AfhIcyQsPeuBKsUtYrRnjkyu0kIVMCHkZtRY=
|
||||
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
|
||||
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
|
||||
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
||||
golang.org/x/crypto v0.7.0 h1:AvwMYaRytfdeVt3u6mLaxYtErKYjxA2OXjJ1HHq6t3A=
|
||||
golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU=
|
||||
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
|
||||
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
|
||||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
||||
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
|
||||
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
|
||||
golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc=
|
||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ=
|
||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
||||
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
|
||||
golang.org/x/term v0.6.0/go.mod h1:m6U89DPEgQRMq3DNkDClhWw02AUbt2daBVO4cn4Hv9U=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
||||
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
|
||||
golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
|
||||
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
|
||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
125
images.go
125
images.go
@@ -11,7 +11,6 @@ import (
|
||||
"math/rand"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"os"
|
||||
|
||||
"github.com/buger/jsonparser"
|
||||
@@ -45,47 +44,6 @@ func getObjectLen(value []byte) (int, error) {
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
// build our sources list for redditbooru
|
||||
func build_redditbooru_sources() {
|
||||
// the url for the sources from redditbooru
|
||||
url := "https://redditbooru.com/sources/"
|
||||
|
||||
// set 5 second timeout on request
|
||||
client := http.Client {
|
||||
Timeout: 5 * time.Second,
|
||||
}
|
||||
|
||||
// get the content of the page
|
||||
resp, err := client.Get(url)
|
||||
if err != nil {
|
||||
fmt.Print("Error getting redditbooru sources, ")
|
||||
panic(err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
// read response
|
||||
out, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
fmt.Print("Error reading response from redditbooru, ")
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// get the length of the sources
|
||||
outlen, err := getArrayLen(out)
|
||||
|
||||
// set our slice to the appropriate size
|
||||
redditbooru_sources = make([]string, outlen)
|
||||
|
||||
// set our sources into the slice
|
||||
for i := 0; i < outlen; i++ {
|
||||
// pull out the title
|
||||
title,_ := jsonparser.GetString(out, "[" + strconv.Itoa(i) + "]", "title")
|
||||
|
||||
// set to lowercase then into our slice
|
||||
redditbooru_sources[i] = strings.ToLower(title)
|
||||
}
|
||||
}
|
||||
|
||||
// search based on the most appropriate location
|
||||
func get_image(sub string) <-chan string {
|
||||
// make the channell
|
||||
@@ -96,7 +54,6 @@ func get_image(sub string) <-chan string {
|
||||
|
||||
// setup variable
|
||||
var image string
|
||||
var found bool = false
|
||||
var cache_exists bool = false
|
||||
|
||||
// check if a cached version exists
|
||||
@@ -123,23 +80,11 @@ func get_image(sub string) <-chan string {
|
||||
}
|
||||
}
|
||||
|
||||
// check to see if it is in the redditbooru sources
|
||||
for _,title := range redditbooru_sources {
|
||||
if title == sub {
|
||||
image = <-get_redditbooru_image(sub, cache_exists)
|
||||
found = true
|
||||
ret <- image
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// if not in redditbooru, check reddit
|
||||
if found == false {
|
||||
// check reddit
|
||||
image = <-get_subreddit_image(sub, cache_exists)
|
||||
ret <- image
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
// nothing found
|
||||
ret <- ""
|
||||
@@ -149,72 +94,6 @@ func get_image(sub string) <-chan string {
|
||||
return ret
|
||||
}
|
||||
|
||||
// redditbooru request
|
||||
func get_redditbooru_image(sub string, cache bool) <-chan string{
|
||||
// make the channel
|
||||
ret := make(chan string)
|
||||
|
||||
go func() {
|
||||
defer close(ret)
|
||||
|
||||
// information variable
|
||||
var out []byte
|
||||
|
||||
// if a cached version exists, use that instead
|
||||
if cache == true {
|
||||
// read our cached file
|
||||
outdata, err := ioutil.ReadFile(cache_location + sub)
|
||||
if err != nil {
|
||||
fmt.Println("Error reading from cached reddit file, ", err)
|
||||
return
|
||||
}
|
||||
|
||||
// copy to our info var
|
||||
out = outdata
|
||||
} else {
|
||||
// create the proper url with the subreddit
|
||||
url := "https://" + sub + ".redditbooru.com/images/?limit=1000"
|
||||
|
||||
// set 5 second timeout on request
|
||||
client := http.Client {
|
||||
Timeout: 5 * time.Second,
|
||||
}
|
||||
|
||||
// get the content of the page
|
||||
resp, err := client.Get(url)
|
||||
defer resp.Body.Close()
|
||||
|
||||
// read response
|
||||
outdata, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
fmt.Println("Error reading response from redditbooru, ", err)
|
||||
return
|
||||
}
|
||||
|
||||
// copy to our info var
|
||||
out = outdata
|
||||
|
||||
// attempt to make a cache file, don't do anything if it doesn't get created (who cares?)
|
||||
os.Create(cache_location + sub)
|
||||
var file, _ = os.OpenFile(cache_location + sub, os.O_RDWR, 0777)
|
||||
defer file.Close()
|
||||
file.Write(outdata)
|
||||
}
|
||||
|
||||
// get a random number for the image
|
||||
outlen,_ := getArrayLen(out)
|
||||
random_img := rand.Intn(outlen)
|
||||
|
||||
// select a random url from our list
|
||||
img_url,_ := jsonparser.GetString(out, "[" + strconv.Itoa(random_img) + "]", "cdnUrl")
|
||||
|
||||
// set the return value
|
||||
ret <- img_url
|
||||
}()
|
||||
|
||||
return ret
|
||||
}
|
||||
|
||||
// imgur request
|
||||
func get_imgur_image(sub string) <-chan string {
|
||||
// make channel
|
||||
|
||||
Reference in New Issue
Block a user