This commit is contained in:
2023-06-29 22:51:21 +02:00
commit ff73e12f8b
11 changed files with 3300 additions and 0 deletions
+51
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
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
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
}