Compare commits

..

20 Commits

Author SHA1 Message Date
ae7a0b79d9 update go packages 2023-03-06 00:22:54 -06:00
0453f02dd0 updated bot's source link 2022-03-04 17:31:48 -06:00
0f8fe62587 removed redditbooru (site closed) 2022-01-19 15:25:47 -06:00
c13053f116 add etc files 2022-01-17 16:46:30 -06:00
b765f1cbb1 added comments 2021-01-28 00:44:27 -06:00
a914fcdbf9 fixed fomatting on moon command 2021-01-28 00:43:23 -06:00
6510e1abc3 added 'moon' command 2021-01-28 00:31:20 -06:00
51ad3d22f2 added 'retarded' command 2021-01-27 22:44:52 -06:00
a1878fb39a fix readme 2020-09-12 03:30:58 -05:00
b9b9dd0e10 updated todos 2020-09-12 03:29:49 -05:00
2fec1fe46f updated readme with build instructions 2020-09-12 03:27:58 -05:00
6be3f231db updated readme with build instructions 2020-09-12 03:27:36 -05:00
52b4bb6a1a added ability to have custom coinflips 2020-09-12 02:31:39 -05:00
17d9128577 updated todos 2020-09-12 01:18:30 -05:00
f98837bc9d Update 'README.md' 2020-06-29 17:08:36 -05:00
a33446e5e7 ignore messages that are too short 2020-06-13 17:15:50 -05:00
6966568ec1 Update 'README.md' 2020-06-13 14:16:08 -05:00
bef0de7e5b Updated 'README.md' 2020-06-12 22:45:40 -05:00
14e68fa3ed Merge branch 'master' of https://git.dtam.pw/daniel/GoBunnyBot 2020-06-12 22:40:56 -05:00
e5e9905824 Added caching capabilities for image requests 2020-06-12 22:40:37 -05:00
6 changed files with 194 additions and 127 deletions

View File

@@ -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

View File

@@ -14,7 +14,10 @@ import (
var (
auth Auth
redditbooru_sources []string
// cache settings
cache_location string = "cache/"
cache_time = 3 * time.Hour // 3 hours
)
func init() {
@@ -24,8 +27,10 @@ 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)
}
}
func main() {
@@ -81,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
@@ -91,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])

File diff suppressed because one or more lines are too long

14
go.mod Normal file
View 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
View 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=

158
images.go
View File

@@ -11,7 +11,7 @@ import (
"math/rand"
"fmt"
"strconv"
"strings"
"os"
"github.com/buger/jsonparser"
)
@@ -44,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
@@ -95,26 +54,38 @@ func get_image(sub string) <-chan string {
// setup variable
var image string
var found bool = false
var cache_exists bool = false
// check to see if it is in the redditbooru sources
for _,title := range redditbooru_sources {
if title == sub {
image = <-get_redditbooru_image(sub)
found = true
// check if a cached version exists
file, err := os.Stat(cache_location + sub)
// if it exists, check it's timestamp
if !os.IsNotExist(err) {
// get the current time
currenttime := time.Now()
// get the file's last modified time
modifiedtime := file.ModTime()
// get the difference in time
difftime := currenttime.Sub(modifiedtime)
// check if it meets our cutoff time
if difftime > cache_time {
// too old, delete the old file
os.Remove(cache_location + sub)
} else {
// else it's new enough, let it be used later
cache_exists = true
}
}
// check reddit
image = <-get_subreddit_image(sub, cache_exists)
ret <- image
return
}
}
// if not in redditbooru, check reddit
if found == false {
image = <-get_subreddit_image(sub)
ret <- image
return
}
// nothing found
ret <- ""
@@ -123,47 +94,6 @@ func get_image(sub string) <-chan string {
return ret
}
// redditbooru request
func get_redditbooru_image(sub string) <-chan string{
// make the channel
ret := make(chan string)
go func() {
defer close(ret)
// 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
out, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("Error reading response from redditbooru, ", err)
return
}
// get a random number for the image
outlen,err := getArrayLen(out)
random_img := rand.Intn(outlen)
// select a random url from our list
img_url,err := 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
@@ -210,12 +140,28 @@ func get_imgur_image(sub string) <-chan string {
}
// subreddit request
func get_subreddit_image(sub string) <-chan string {
func get_subreddit_image(sub string, cache bool) <-chan string {
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 { // else pull it from the web
// create the proper url with the subreddit
url := "https://www.reddit.com/r/" + sub + "/.json?show=all&limit=100"
@@ -232,12 +178,24 @@ func get_subreddit_image(sub string) <-chan string {
defer resp.Body.Close()
// read response
out, err := ioutil.ReadAll(resp.Body)
outdata, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("Error reading response from reddit, ", 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)
}
// make sure we aren't grabbing a text post by cylcing through looking for an image
limit64, _ := jsonparser.GetInt(out, "data", "dist")
limit := int(limit64) // convert from int64 to int