Compare commits

...

45 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
6647489083 Update 'README.md' 2020-06-11 13:58:09 -05:00
978fb3f950 Update 'README.md' 2020-06-11 12:37:17 -05:00
4c5d046d2d Update 'README.md' 2020-06-11 12:36:48 -05:00
853eae6c4e Update 'README.md' 2020-06-11 12:36:04 -05:00
ffbfce3ebb set subreddit image search to also filter by known image domains 2020-06-11 11:20:15 -05:00
adcaa81750 set message content to be delimited by whitespace 2020-06-11 03:34:27 -05:00
e7589c4188 fixed checking message token and set to correct message token 2020-06-10 01:57:10 -05:00
dd95bcf73e added source command 2020-06-10 01:15:05 -05:00
12cfe02319 reworked coinflip command 2020-06-10 01:10:38 -05:00
d668c2d3c9 added roll command 2020-06-10 01:05:21 -05:00
3dac24062e moved coinflip to it's own function 2020-06-10 00:42:47 -05:00
61f8f50030 added coin flip 2020-06-10 00:40:52 -05:00
0df7326c1c fixed rng seed to only be called once at initilization 2020-06-10 00:31:39 -05:00
8fc0d3062b fixed rng seed to only be called once at initilization 2020-06-10 00:30:53 -05:00
56b88c8f51 added tagged user to coinflip 2020-06-09 15:22:17 -05:00
a1ed26f736 check messages from the bot and ignore 2020-06-09 15:17:19 -05:00
ec76743a7c set default case of searching for images 2020-06-09 15:13:47 -05:00
6e3297df4f fixed some base cases 2020-06-09 14:41:49 -05:00
968d670b29 added func to search based on best criteria 2020-06-09 14:38:26 -05:00
872cdf45f3 change to consistent naming scheme 2020-06-09 14:02:32 -05:00
184e045243 set up list for redditbooru subreddits 2020-06-09 14:01:47 -05:00
80a5b8c05f renamed images.go file 2020-06-09 13:29:30 -05:00
a22c3e6b2f changed panics to just print 2020-06-06 23:01:03 -05:00
f887166ca5 renamed image file 2020-06-06 22:58:21 -05:00
9b7ca0a125 added subreddit image request 2020-06-06 22:55:42 -05:00
8 changed files with 430 additions and 134 deletions

View File

@@ -2,3 +2,20 @@
Rewrite of my Discord bot, BunnyBot, in Go. Rewrite of my Discord bot, BunnyBot, in Go.
Previous version, written in Javascript using Node.js: https://git.dtam.pw/daniel/discord-bot-js Previous version, written in Javascript using Node.js: https://git.dtam.pw/daniel/discord-bot-js
## To do
Restore previous functionality:
- Allow roll ranges
- Add 8 ball
- Add compute
- Add voice commands
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
## 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

@@ -17,7 +17,7 @@ type Auth struct {
} }
// build the auth struct // build the auth struct
func buildAuth() { func build_auth() {
// read the auth json file // read the auth json file
json, err := ioutil.ReadFile("auth.json") json, err := ioutil.ReadFile("auth.json")
if err != nil { if err != nil {

View File

@@ -6,17 +6,31 @@ import (
"os/signal" "os/signal"
"syscall" "syscall"
"strings" "strings"
"time"
"math/rand"
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
) )
var ( var (
auth Auth auth Auth
// cache settings
cache_location string = "cache/"
cache_time = 3 * time.Hour // 3 hours
) )
func init() { func init() {
// set the randomized seed
rand.Seed(time.Now().UnixNano())
// build the authentication struct // build the authentication struct
buildAuth() build_auth()
// attempt to make our cached
if _, err := os.Stat(cache_location); os.IsNotExist(err) {
os.Mkdir(cache_location, 0777)
}
} }
func main() { func main() {
@@ -49,27 +63,59 @@ func main() {
} }
func message_create (s *discordgo.Session, m *discordgo.MessageCreate) { func message_create (s *discordgo.Session, m *discordgo.MessageCreate) {
// ignore messages from the bot
if m.Author.ID == s.State.User.ID {
return
}
// make sure we match our bot's message token // make sure we match our bot's message token
if len(m.Content) > 2 { if len(m.Content) > 2 {
// get the input token
input_token := m.Content[0:2] input_token := m.Content[0:2]
if strings.ToLower(string(input_token)) != "t." {
// check if we match our token
found := false
// try to find our token
if strings.ToLower(string(input_token)) == "b." || strings.ToLower(string(input_token)) == "//" {
found = true
}
// return if this message isn't for us
if found == false {
return return
} }
} else { // message too short, don't do anything
return
} }
// get our message without our bot's token // get our message's content without our bot's token
message := m.Content[2:] content := strings.ToLower(m.Content[2:])
// awwnime (test) // split our message's content into parts based on space
if message == "awwnime" { message := strings.Fields(content)
// determine our actions
if message[0] == "coinflip" || message[0] == "coin" { // flip a coin
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 // get url
url := <-get_imgur_image("awwnime") url := <-get_image(message[0])
// print message with url // make sure we have a url returned
s.ChannelMessageSend(m.ChannelID, url) if len(url) > 0 {
s.ChannelMessageSend(m.ChannelID, url)
} else {
s.ChannelMessageSend(m.ChannelID, "I couldn't find that, sauce?")
}
} }
//get_redditbooru("awwnime")
} }

70
commands.go Normal file

File diff suppressed because one or more lines are too long

View File

@@ -1,120 +0,0 @@
/*
This contains our functions used for image searches
*/
package main
import (
"io/ioutil"
"net/http"
"time"
"math/rand"
"fmt"
"strconv"
"github.com/buger/jsonparser"
)
// get the length of array for our parser
func getArrayLen(value []byte) (int, error) {
ret := 0
arrayCallback := func(value []byte, dataType jsonparser.ValueType, offset int, err error) {
ret++
}
if _, err := jsonparser.ArrayEach(value, arrayCallback); err != nil {
return 0, fmt.Errorf("getArrayLen ArrayEach error: %v", err)
}
return ret, nil
}
// 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 {
panic(err)
}
// randomize the seed for the random int
rand.Seed(time.Now().UnixNano())
// 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
ret := make(chan string)
go func() {
defer close(ret)
// create the proper url with the subreddit
url := "https://api.imgur.com/3/gallery/r/" + sub + "/time/1"
// set 5 second timeout on request
client := http.Client {
Timeout: 5 * time.Second,
}
// create the request
req, err := http.NewRequest("GET", url, nil)
req.Header.Add("Authorization", "Client-ID " + auth.imgur)
// get the content of the page
resp, err := client.Do(req)
defer resp.Body.Close()
// read response
out, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)
}
// randomize the seed for the random int
rand.Seed(time.Now().UnixNano())
// get a random number for the image
//outlen, _ := getArrayLen(out)
//random_img := rand.Intn(outlen)
// parse the data (fix this)
img_url, _ := jsonparser.GetString(out, "[0]", "[0]", "link")
fmt.Println(string(img_url))
ret <- ""
}()
return ret
}

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=

224
images.go Normal file
View File

@@ -0,0 +1,224 @@
/*
This contains our functions used for image searches
*/
package main
import (
"io/ioutil"
"net/http"
"time"
"math/rand"
"fmt"
"strconv"
"os"
"github.com/buger/jsonparser"
)
// get the length of array for our parser
func getArrayLen(value []byte) (int, error) {
ret := 0
arrayCallback := func(value []byte, dataType jsonparser.ValueType, offset int, err error) {
ret++
}
if _, err := jsonparser.ArrayEach(value, arrayCallback); err != nil {
return 0, fmt.Errorf("getArrayLen ArrayEach error: %v", err)
}
return ret, nil
}
// get the length of object for our parser
func getObjectLen(value []byte) (int, error) {
ret := 0
objectCallback := func(key []byte, value []byte, dataType jsonparser.ValueType, offset int) error {
ret++
return nil
}
if err := jsonparser.ObjectEach(value, objectCallback); err != nil {
return 0, fmt.Errorf("getObjectLen ObjectEach error: %v", err)
}
return ret, nil
}
// search based on the most appropriate location
func get_image(sub string) <-chan string {
// make the channell
ret := make(chan string)
go func() {
defer close(ret)
// setup variable
var image string
var cache_exists bool = false
// 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
// nothing found
ret <- ""
}()
return ret
}
// imgur request
func get_imgur_image(sub string) <-chan string {
// make channel
ret := make(chan string)
go func() {
defer close(ret)
// create the proper url with the subreddit
url := "https://api.imgur.com/3/gallery/r/" + sub + "/time/1"
// set 5 second timeout on request
client := http.Client {
Timeout: 5 * time.Second,
}
// create the request
req, err := http.NewRequest("GET", url, nil)
req.Header.Add("Authorization", "Client-ID " + auth.imgur)
// get the content of the page
resp, err := client.Do(req)
defer resp.Body.Close()
// read response
out, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("Error reading response from imgur, ", err)
return
}
// get a random number for the image
//outlen, _ := getArrayLen(out)
//random_img := rand.Intn(outlen)
// parse the data (fix this)
img_url, _ := jsonparser.GetString(out, "[0]", "[0]", "link")
fmt.Println(string(img_url))
ret <- ""
}()
return ret
}
// subreddit request
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"
// set 5 second timeout on request
client := http.Client {
Timeout: 5 * time.Second,
}
req, err := http.NewRequest("GET", url, nil)
req.Header.Add("User-Agent", "BunnyBot")
// get the content of the page
resp, err := client.Do(req)
defer resp.Body.Close()
// read response
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
// loop through and try to find a post that isn't a text post
for i := 0; i < limit; i++ {
// get a random number
random_img := rand.Intn(limit)
// check the post hint to see what type of post it is
hint, _ := jsonparser.GetString(out, "data", "children", "[" + strconv.Itoa(random_img) + "]", "data", "post_hint")
// get the domain to also check againist
domain, _ := jsonparser.GetString(out, "data", "children", "[" + strconv.Itoa(random_img) + "]", "data", "domain")
// make sure that it is an image, or at least a gif
if hint == "image" || hint == "link" || hint == "rich:video" || domain == "i.redd.it" || domain == "i.imgur.com" {
image, _ := jsonparser.GetString(out, "data", "children", "[" + strconv.Itoa(random_img) + "]", "data", "url")
ret <- image
return
}
}
}()
return ret
}