zveinn 10 months ago
parent
commit
2aa9aed6a2
5 changed files with 197 additions and 21 deletions
  1. 1 0
      go.mod
  2. 2 0
      go.sum
  3. 1 0
      main.go
  4. 179 14
      twitch-client.go
  5. 14 7
      variables.go

+ 1 - 0
go.mod

@@ -15,6 +15,7 @@ require (
 	github.com/ebitengine/purego v0.7.1 // indirect
 	github.com/golang-jwt/jwt v3.2.1+incompatible // indirect
 	github.com/golang/snappy v0.0.1 // indirect
+	github.com/google/uuid v1.6.0 // indirect
 	github.com/hajimehoshi/go-mp3 v0.3.4 // indirect
 	github.com/klauspost/compress v1.13.6 // indirect
 	github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe // indirect

+ 2 - 0
go.sum

@@ -12,6 +12,8 @@ github.com/golang/snappy v0.0.1 h1:Qgr9rKW7uDUkrbSmQeiDsGa8SjGyCOGtuasMWwvp2P4=
 github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
 github.com/google/go-cmp v0.5.2 h1:X2ev0eStA3AbceY54o37/0PQ/UWqKEiiO2dKL5OPaFM=
 github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
+github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
+github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
 github.com/gopxl/beep v1.4.1 h1:WqNs9RsDAhG9M3khMyc1FaVY50dTdxG/6S6a3qsUHqE=
 github.com/gopxl/beep v1.4.1/go.mod h1:A1dmiUkuY8kxsvcNJNUBIEcchmiP6eUyCHSxpXl0YO0=
 github.com/hajimehoshi/go-mp3 v0.3.4 h1:NUP7pBYH8OguP4diaTZ9wJbUbk3tC0KlfzsEpWmYj68=

+ 1 - 0
main.go

@@ -73,6 +73,7 @@ func main() {
 	mongowrapper.InitCollections()
 
 	InitTwitchClient()
+	INIT_MSG()
 
 	InitCommands()
 	InitMP3Map()

+ 179 - 14
twitch-client.go

@@ -1,6 +1,8 @@
 package main
 
 import (
+	"bytes"
+	"encoding/json"
 	"fmt"
 	"io"
 	"log"
@@ -8,7 +10,6 @@ import (
 	"net/http"
 	"net/url"
 	"os"
-	"os/exec"
 	"runtime/debug"
 	"strconv"
 	"strings"
@@ -16,9 +17,11 @@ import (
 	"time"
 
 	tirc "github.com/gempir/go-twitch-irc/v4"
+	"github.com/google/uuid"
 	"github.com/gopxl/beep"
 	"github.com/gopxl/beep/mp3"
 	"github.com/gopxl/beep/speaker"
+	"github.com/gopxl/beep/wav"
 	"github.com/nicklaw5/helix"
 )
 
@@ -190,12 +193,12 @@ func ProcessMessage(msg tirc.PrivateMessage) {
 		U.ID = msg.User.ID
 		U.Color = msg.User.Color
 		U.Badges = msg.User.Badges
-		err = IncrementUserPoints(U, 100)
+		err = IncrementUserPoints(U, 500)
 		if err == nil {
 			U.Points = 100
 		}
 	} else {
-		_ = IncrementUserPoints(U, 1)
+		_ = IncrementUserPoints(U, 5)
 	}
 
 	log.Println("CUSTOM REWARD ID:", msg.CustomRewardID)
@@ -221,6 +224,13 @@ func ProcessMessage(msg tirc.PrivateMessage) {
 		return
 	}
 
+	if strings.Contains(msg.Message, "!bot") {
+		if msg.User.DisplayName == "KEYB1ND_" {
+			go askTheBot(msg.Message)
+		}
+		return
+	}
+
 	// banword := ""
 	// isBanned := false
 	// if strings.Contains(msg.Message, " tailwind ") || strings.HasPrefix(msg.Message, "tailwind") {
@@ -361,7 +371,7 @@ func UserRollCommand(user *User, msg *tirc.PrivateMessage) (err error) {
 		return
 	}
 
-	if user.Points < rollAmount {
+	if user.Points < rollAmount || rollAmount < 0 {
 		TWITCH_CLIENT.ReplyToUser(msg.User.DisplayName, "you do not have enough points to gamba", "")
 		return
 	}
@@ -473,12 +483,6 @@ func PlayTTS(msg string) {
 
 	params := url.Values{}
 	params.Add("text", msg)
-	fileName := ""
-	if len(msg) < 25 {
-		fileName = msg
-	} else {
-		fileName = msg[0:20]
-	}
 
 	httpClient := new(http.Client)
 	// urlmsg := url.QueryEscape(msg)
@@ -498,21 +502,182 @@ func PlayTTS(msg string) {
 		log.Println(err)
 		return
 	}
-	f, err := os.Create("./tts/" + fileName + "-" + strconv.Itoa(int(time.Now().UnixNano())) + ".wav")
+
+	fileName := uuid.NewString() + "-" + strconv.Itoa(int(time.Now().UnixNano()))
+
+	f, err := os.Create("./tts/" + fileName + ".wav")
+	if err != nil {
+		log.Println(err)
+		return
+	}
+	defer f.Close()
+	f2, err := os.Create("./tts/" + fileName + ".txt")
 	if err != nil {
 		log.Println(err)
 		return
 	}
+	defer f2.Close()
 	_, err = f.Write(bytes)
 	if err != nil {
 		log.Println(err)
 		return
 	}
+	_, err = f2.Write([]byte(msg))
+	if err != nil {
+		log.Println(err)
+		return
+	}
 	log.Println("BYTES:", len(bytes))
 
-	cmd := exec.Command("ffplay", "-v", "0", "-nodisp", "-autoexit", f.Name())
-	out, err := cmd.CombinedOutput()
+	PlayTTSFile(f.Name())
+}
+
+func PlayTTSFile(tag string) {
+	f, err := os.Open(tag)
 	if err != nil {
-		log.Println(err, string(out))
+		log.Println("error opening tts file:", err)
+		return
 	}
+
+	streamer, format, err := wav.Decode(f)
+	if err != nil {
+		log.Fatal(err)
+	}
+	defer streamer.Close()
+
+	speaker.Init(format.SampleRate, format.SampleRate.N(time.Second/10))
+	done := make(chan bool)
+	speaker.Play(beep.Seq(streamer, beep.Callback(func() {
+		done <- true
+	})))
+
+	<-done
+}
+
+type BotModel struct {
+	Model     string    `json:"model"`
+	Prompt    string    `json:"prompt"`
+	Stream    bool      `json:"stream"`
+	Options   *BotOpts  `json:"options"`
+	MaxTokens int       `json:"max_tokens"`
+	Messages  []*BotMSG `json:"messages"`
+}
+
+type BotMSG struct {
+	Role    string `json:"role"`
+	Content string `json:"content"`
+}
+
+type BotOpts struct {
+	Temperature float64 `json:"temperature"`
+	// TopP        float64 `json:"top_p"`
+	// TopK        int     `json:"top_k"`
+	// MinP        float64 `json:"min_p"`
+	// NumPredict  int     `json:"num_predict"`
+}
+
+type BotResp struct {
+	Model              string    `json:"model"`
+	CreatedAt          time.Time `json:"created_at"`
+	Response           string    `json:"response"`
+	Done               bool      `json:"done"`
+	Context            []int     `json:"context"`
+	TotalDuration      int64     `json:"total_duration"`
+	LoadDuration       int       `json:"load_duration"`
+	PromptEvalCount    int       `json:"prompt_eval_count"`
+	PromptEvalDuration int       `json:"prompt_eval_duration"`
+	EvalCount          int       `json:"eval_count"`
+	EvalDuration       int64     `json:"eval_duration"`
+}
+
+type BotRespChat struct {
+	Message *BotMSG `json:"message"`
+}
+
+var BaseMSG = make([]*BotMSG, 0)
+
+func INIT_MSG() {
+	BaseMSG = append(BaseMSG, &BotMSG{
+		Role:    "user",
+		Content: "I want you to refer to me as Key from now on.",
+	})
+	BaseMSG = append(BaseMSG, &BotMSG{
+		Role:    "assistant",
+		Content: "I will refer to you as key from now on.",
+	})
+	BaseMSG = append(BaseMSG, &BotMSG{
+		Role:    "user",
+		Content: "I want you to pretend to be evil, be subtle about it but try to be a bit snarky with your replies. Also, try to make fun of me in subtle ways when possible, don't over do it though.",
+	})
+	BaseMSG = append(BaseMSG, &BotMSG{
+		Role:    "assistant",
+		Content: "I will do that",
+	})
+	BaseMSG = append(BaseMSG, &BotMSG{
+		Role:    "user",
+		Content: "Please try to keep your answers short.",
+	})
+	BaseMSG = append(BaseMSG, &BotMSG{
+		Role:    "assistant",
+		Content: "I will do that",
+	})
+
+}
+
+func askTheBot(msg string) {
+	ms := strings.Split(msg, " ")
+	m := strings.Join(ms[1:], " ")
+
+	BaseMSG = append(BaseMSG, &BotMSG{
+		Role:    "user",
+		Content: m,
+	})
+
+	BM := new(BotModel)
+	BM.Model = "mannix/llama3.1-8b-abliterated"
+	// BM.Prompt = m
+	BM.Messages = BaseMSG
+	BM.Stream = false
+	BM.Options = new(BotOpts)
+	BM.Options.Temperature = 0.8
+	BM.MaxTokens = 50
+
+	ob, err := json.Marshal(BM)
+
+	httpClient := new(http.Client)
+	buff := bytes.NewBuffer(ob)
+
+	// urlmsg := url.QueryEscape(msg)
+	// req, err := http.NewRequest("POST", "http://localhost:11434/api/generate", buff)
+	req, err := http.NewRequest("POST", "http://localhost:11434/api/chat", buff)
+	if err != nil {
+		log.Println(err)
+		return
+	}
+
+	resp, err := httpClient.Do(req)
+	if err != nil {
+		log.Println(err)
+		return
+	}
+
+	bytes, err := io.ReadAll(resp.Body)
+	if err != nil {
+		log.Println(err)
+		return
+	}
+
+	fmt.Println(string(bytes))
+	BR := new(BotRespChat)
+	err = json.Unmarshal(bytes, BR)
+	if err != nil {
+		fmt.Println("lama resp err:", err)
+		return
+	}
+
+	BaseMSG = append(BaseMSG, BR.Message)
+
+	fmt.Println(BR.Message.Content)
+	PlayTTS(BR.Message.Content)
+
 }

+ 14 - 7
variables.go

@@ -48,25 +48,32 @@ func InitTwitchClient() {
 func InitCommands() {
 	// TextCommands["!monero"] = "43V6N2BpjvMYUthyqLioafZ2MQQviWEhvVTpp3hHc6LB48WYE8SsjrJKyyYzR3AYu2HkSXu8xsJhr7wdLsgSc8mGDDTkCrn"
 	TextCommands["!nvim"] = "https://github.com/zveinn/dotfiles"
+	TextCommands["!dotfiles"] = "https://github.com/zveinn/dotfiles"
 	TextCommands["!x"] = "https://x.com/keyb1nd"
 	TextCommands["!github"] = "https://github.com/zveinn"
 	TextCommands["!linkedin"] = "https://www.linkedin.com/in/keyb1nd/"
 	TextCommands["!discord"] = "https://discord.com/invite/wJ5m3Y6ezq"
-	TextCommands["!keyboard"] = "https://x.com/keyb1nd/status/1589688621619351552"
-	TextCommands["!os"] = "Debian 12 kde"
+	TextCommands["!keyboard"] = "wooting.io"
+	TextCommands["!os"] = "All of them."
 	TextCommands["!terminal"] = "wezterm + tmux"
 	TextCommands["!editor"] = "nvim"
-	TextCommands["!spec"] = "... the best cpu. literally a super computer from space."
-	TextCommands["!youtube"] = "https://www.youtube.com/@keyb1nd"
+	TextCommands["!youtube"] = "https://www.youtube.com/@keyb1nd?sub_confirmation=1"
 	TextCommands["!lurk"] = "ABSOLUTELY NOT ... LURKING IS NOT ALLOWED IN HERE"
 	TextCommands["!signal"] = "https://signal.group/#CjQKILCHWDqtfKErs-6yV8i0kQHhScDTL4wQ2mW7JYoQoBLsEhC7R4AqmLxdxwdRa0fWK1tD"
 
 	// VPN RELATED
-	TextCommands["!freetrial"] = "All new accounts get 24 hours free trial > https://www.nicelandvpn.is/#/register"
-	TextCommands["!vpn"] = "Tunnels.is >> 24/h Free Trial >> Anonymous Accounts >> NO CARD INFO NEEDED! >>> https://tunnels.is"
+	TextCommands["!freetrial"] = "All new accounts get 24 hours free trial > https://www.tunnels.is"
+	TextCommands["!vpn"] = "Tunnels.is >> Advanced Networking Utility >> 24/h Free Trial >> https://tunnels.is"
 	TextCommands["!vpndiscord"] = "Tunnels.is DISCORD: https://discord.com/invite/7Ts3PCnCd9"
 
-	TextCommands["!commands"] = "lol.. just guess"
+	TextCommands["!cmd"] += "!tts !roll !points !quote !top10"
+	TextCommands["!commands"] += "!tts !roll !points !quote !top10"
+
+	for i := range TextCommands {
+		TextCommands["!cmd"] += " " + i
+		TextCommands["!commands"] += " " + i
+	}
+
 }
 
 func CheckCustomReward(U *User, msg tirc.PrivateMessage) (success bool) {