This commit is contained in:
Kristian 2023-06-29 22:51:21 +02:00
commit ff73e12f8b
11 changed files with 3300 additions and 0 deletions

3
.env_example Normal file
View File

@ -0,0 +1,3 @@
export BOT_TOKEN=
export CRYPTO_TOKEN=
export OPENAI_TOKEN=

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
.env

51
bot/bot.go Normal file
View File

@ -0,0 +1,51 @@
package bot
import (
"bitbot/pb"
"github.com/bwmarrin/discordgo"
"github.com/charmbracelet/log"
"os"
"os/signal"
"strings"
)
var (
CryptoToken string
BotToken string
OpenAIToken string
)
func Run() {
discord, err := discordgo.New("Bot " + BotToken)
if err != nil {
log.Fatal(err)
}
discord.AddHandler(newMessage)
discord.Open()
defer discord.Close()
log.Info("BitBot is running...")
pb.Run()
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
<-c
}
func newMessage(discord *discordgo.Session, message *discordgo.MessageCreate) {
if message.Author.ID == discord.State.User.ID {
return
}
switch {
case strings.Contains(message.Content, "!bit"):
gptResponse := chatGPT(message.Content)
discord.ChannelTyping(message.ChannelID)
discord.ChannelMessageSendComplex(message.ChannelID, gptResponse)
case strings.Contains(message.Content, "!cry"):
currentCryptoPrice := getCurrentCryptoPrice(message.Content)
discord.ChannelMessageSendComplex(message.ChannelID, currentCryptoPrice)
}
}

43
bot/chat.go Normal file
View File

@ -0,0 +1,43 @@
package bot
import (
"context"
"github.com/bwmarrin/discordgo"
openai "github.com/sashabaranov/go-openai"
)
func chatGPT(message string) *discordgo.MessageSend {
client := openai.NewClient(OpenAIToken)
resp, err := client.CreateChatCompletion(
context.Background(),
openai.ChatCompletionRequest{
MaxTokens: 800,
FrequencyPenalty: 0.3,
PresencePenalty: 0.6,
Model: openai.GPT3Dot5Turbo0301,
Messages: []openai.ChatCompletionMessage{
{
Role: openai.ChatMessageRoleSystem,
Content: "You are an amazing and versatile person! As an ethical hacker, coder, polyglot, and poet.",
},
{
Role: openai.ChatMessageRoleUser,
Content: message,
},
},
},
)
if err != nil {
return &discordgo.MessageSend{
Content: "Sorry, there was an error trying to connect to api",
}
}
gptResponse := (resp.Choices[0].Message.Content)
embed := &discordgo.MessageSend{
Content: gptResponse,
}
return embed
}

69
bot/command-crypto.go Normal file
View File

@ -0,0 +1,69 @@
package bot
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"regexp"
"strconv"
"strings"
"time"
"github.com/bwmarrin/discordgo"
)
const URL string = "https://min-api.cryptocompare.com/data/price"
type CryptoData struct {
Usd float64 `json:"USD"`
}
func getCurrentCryptoPrice(message string) *discordgo.MessageSend {
r, _ := regexp.Compile(`\s?[A-Z]{3,5}\s?`)
currency := r.FindString(message)
curr := strings.ReplaceAll(currency, " ", "")
if curr == "" {
return &discordgo.MessageSend{
Content: "Sorry, cant recognize crypto currency shortcode try uppercase and with spaces around",
}
}
cryptoURL := fmt.Sprintf("%s?fsym=%s&tsyms=USD&api_key=%s", URL, curr, CryptoToken)
fmt.Println(cryptoURL)
client := http.Client{Timeout: 5 * time.Second}
response, err := client.Get(cryptoURL)
if err != nil {
return &discordgo.MessageSend{
Content: "Sorry, there was an error trying to connect to api",
}
}
body, _ := ioutil.ReadAll(response.Body)
defer response.Body.Close()
var data CryptoData
json.Unmarshal([]byte(body), &data)
fmt.Println(body, data, data.Usd)
usd := strconv.FormatFloat(data.Usd, 'f', 2, 64)
embed := &discordgo.MessageSend{
Embeds: []*discordgo.MessageEmbed{{
Type: discordgo.EmbedTypeRich,
Title: "Current Price",
Description: "Price for " + curr,
Fields: []*discordgo.MessageEmbedField{
{
Name: "1 " + curr,
Value: usd + " USD",
Inline: true,
},
},
},
},
}
return embed
}

104
go.mod Normal file
View File

@ -0,0 +1,104 @@
module bitbot
go 1.18
require (
github.com/bwmarrin/discordgo v0.26.1
github.com/charmbracelet/log v0.2.1
github.com/labstack/echo/v5 v5.0.0-20220201181537-ed2888cfa198
github.com/pocketbase/pocketbase v0.14.3
)
require (
github.com/AlecAivazis/survey/v2 v2.3.6 // indirect
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 // indirect
github.com/aws/aws-sdk-go v1.44.234 // indirect
github.com/aws/aws-sdk-go-v2 v1.17.7 // indirect
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.4.10 // indirect
github.com/aws/aws-sdk-go-v2/config v1.18.19 // indirect
github.com/aws/aws-sdk-go-v2/credentials v1.13.18 // indirect
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.13.1 // indirect
github.com/aws/aws-sdk-go-v2/feature/s3/manager v1.11.60 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.31 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.4.25 // indirect
github.com/aws/aws-sdk-go-v2/internal/ini v1.3.32 // indirect
github.com/aws/aws-sdk-go-v2/internal/v4a v1.0.23 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.9.11 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.1.26 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.9.25 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.14.0 // indirect
github.com/aws/aws-sdk-go-v2/service/s3 v1.31.1 // indirect
github.com/aws/aws-sdk-go-v2/service/sso v1.12.6 // indirect
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.14.6 // indirect
github.com/aws/aws-sdk-go-v2/service/sts v1.18.7 // indirect
github.com/aws/smithy-go v1.13.5 // indirect
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/charmbracelet/lipgloss v0.7.1 // indirect
github.com/disintegration/imaging v1.6.2 // indirect
github.com/domodwyer/mailyak/v3 v3.5.0 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/fatih/color v1.15.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
github.com/ganigeorgiev/fexpr v0.3.0 // indirect
github.com/go-logfmt/logfmt v0.6.0 // indirect
github.com/go-ozzo/ozzo-validation/v4 v4.3.0 // indirect
github.com/golang-jwt/jwt/v4 v4.5.0 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/google/wire v0.5.0 // indirect
github.com/googleapis/gax-go/v2 v2.8.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.18 // indirect
github.com/mattn/go-runewidth v0.0.14 // indirect
github.com/mattn/go-sqlite3 v1.14.16 // indirect
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d // indirect
github.com/muesli/reflow v0.3.0 // indirect
github.com/muesli/termenv v0.15.1 // indirect
github.com/pocketbase/dbx v1.10.0 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/cobra v1.6.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v1.2.2 // indirect
go.opencensus.io v0.24.0 // indirect
gocloud.dev v0.29.0 // indirect
golang.org/x/image v0.6.0 // indirect
golang.org/x/mod v0.9.0 // indirect
golang.org/x/net v0.8.0 // indirect
golang.org/x/oauth2 v0.6.0 // indirect
golang.org/x/term v0.6.0 // indirect
golang.org/x/text v0.8.0 // indirect
golang.org/x/time v0.3.0 // indirect
golang.org/x/tools v0.7.0 // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
google.golang.org/api v0.114.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20230331144136-dcfb400f0633 // indirect
google.golang.org/grpc v1.54.0 // indirect
google.golang.org/protobuf v1.30.0 // indirect
lukechampine.com/uint128 v1.3.0 // indirect
modernc.org/cc/v3 v3.40.0 // indirect
modernc.org/ccgo/v3 v3.16.13 // indirect
modernc.org/libc v1.22.3 // indirect
modernc.org/mathutil v1.5.0 // indirect
modernc.org/memory v1.5.0 // indirect
modernc.org/opt v0.1.3 // indirect
modernc.org/sqlite v1.21.1 // indirect
modernc.org/strutil v1.1.3 // indirect
modernc.org/token v1.1.0 // indirect
)
require (
github.com/gorilla/websocket v1.5.0 // indirect
github.com/joho/godotenv v1.4.0
github.com/sashabaranov/go-openai v1.10.1
golang.org/x/crypto v0.7.0 // indirect
golang.org/x/sys v0.6.0 // indirect
)

2956
go.sum Normal file

File diff suppressed because it is too large Load Diff

37
main.go Normal file
View File

@ -0,0 +1,37 @@
package main
import (
"bitbot/bot"
"github.com/charmbracelet/log"
"github.com/joho/godotenv"
"os"
)
func init() {
if err := godotenv.Load(); err != nil {
log.Fatal("no .env file found")
}
}
func main() {
botToken, ok := os.LookupEnv("BOT_TOKEN")
if !ok {
log.Fatal("Must set Discord token asn env variable: BOT_TOKEN")
}
cryptoToken, ok := os.LookupEnv("CRYPTO_TOKEN")
if !ok {
log.Fatal("Must set crypto token as env variable: CRYPTO_TOKEN")
}
openAIToken, ok := os.LookupEnv("OPENAI_TOKEN")
if !ok {
log.Fatal("Must set OpenAI token as env variable: OPENAI_TOKEN")
}
bot.BotToken = botToken
bot.CryptoToken = cryptoToken
bot.OpenAIToken = openAIToken
bot.Run()
}

36
pb/pb.go Normal file
View File

@ -0,0 +1,36 @@
package pb
import (
"net/http"
"github.com/labstack/echo/v5"
"github.com/pocketbase/pocketbase"
"github.com/pocketbase/pocketbase/apis"
"github.com/pocketbase/pocketbase/core"
"github.com/charmbracelet/log"
)
func Run() {
app := pocketbase.New()
app.OnBeforeServe().Add(func(e *core.ServeEvent) error {
// add new "GET /hello" route to the app router (echo)
e.Router.AddRoute(echo.Route{
Method: http.MethodGet,
Path: "/hello",
Handler: func(c echo.Context) error {
return c.String(200, "Hello world!")
},
Middlewares: []echo.MiddlewareFunc{
apis.ActivityLogger(app),
},
})
return nil
})
if err := app.Start(); err != nil {
log.Fatal(err)
}
}

BIN
pb_data/data.db Normal file

Binary file not shown.

BIN
pb_data/logs.db Normal file

Binary file not shown.