Compare commits
10 Commits
b36fd1dba4
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 1b3233aafe | |||
| 618b4e290a | |||
| b719dbd9fc | |||
| fe12f755c6 | |||
| 36d2a725e5 | |||
| b2b50d303b | |||
| 21ce2d0609 | |||
| 588b6d9fa3 | |||
| 534d4ef9ea | |||
| ccc3e62d9d |
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1,3 +1,5 @@
|
|||||||
.env
|
.env
|
||||||
node_modules
|
node_modules
|
||||||
__debug_bin.exe
|
__debug_bin.exe
|
||||||
|
__debug_bin
|
||||||
|
.vscode
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
include:
|
include:
|
||||||
- project: 'developerdurp/yml'
|
- project: 'developerdurp/yml'
|
||||||
ref: main
|
ref: main
|
||||||
file: 'pipelines/build-container.yml'
|
file: 'pipelines/golang.yml'
|
||||||
@@ -16,6 +16,9 @@ spec:
|
|||||||
app: durpot
|
app: durpot
|
||||||
spec:
|
spec:
|
||||||
containers:
|
containers:
|
||||||
|
- name: {{ .Chart.Name }}
|
||||||
|
image: "{{ .Values.deployment.image }}:{{ default .Chart.Version .Values.deployment.tag }}"
|
||||||
|
imagePullPolicy: {{ .Values.deployment.imagePullPolicy }}
|
||||||
- name: durpot
|
- name: durpot
|
||||||
image: registry.gitlab.com/developerdurp/durpot
|
image: registry.gitlab.com/developerdurp/durpot
|
||||||
imagePullPolicy: Always
|
imagePullPolicy: Always
|
||||||
|
|||||||
4
charts/durpot/values.yaml
Normal file
4
charts/durpot/values.yaml
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
deployment:
|
||||||
|
image: registry.gitlab.com/developerdurp/durpot
|
||||||
|
tag: ""
|
||||||
|
imagePullPolicy: Always
|
||||||
11
go.mod
11
go.mod
@@ -1,13 +1,14 @@
|
|||||||
module golang-discord-bot
|
module gitlab.com/DeveloperDurp/durpot
|
||||||
|
|
||||||
go 1.19
|
go 1.19
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/bwmarrin/discordgo v0.26.1 // indirect
|
github.com/bwmarrin/discordgo v0.26.1
|
||||||
|
github.com/sashabaranov/go-openai v1.5.7
|
||||||
|
)
|
||||||
|
|
||||||
|
require (
|
||||||
github.com/gorilla/websocket v1.4.2 // indirect
|
github.com/gorilla/websocket v1.4.2 // indirect
|
||||||
github.com/joho/godotenv v1.4.0 // indirect
|
|
||||||
github.com/sashabaranov/go-openai v1.5.7 // indirect
|
|
||||||
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b // indirect
|
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b // indirect
|
||||||
golang.org/x/exp v0.0.0-20221031165847-c99f073a8326 // indirect
|
|
||||||
golang.org/x/sys v0.1.0 // indirect
|
golang.org/x/sys v0.1.0 // indirect
|
||||||
)
|
)
|
||||||
|
|||||||
5
go.sum
5
go.sum
@@ -2,16 +2,11 @@ github.com/bwmarrin/discordgo v0.26.1 h1:AIrM+g3cl+iYBr4yBxCBp9tD9jR3K7upEjl0d89
|
|||||||
github.com/bwmarrin/discordgo v0.26.1/go.mod h1:NJZpH+1AfhIcyQsPeuBKsUtYrRnjkyu0kIVMCHkZtRY=
|
github.com/bwmarrin/discordgo v0.26.1/go.mod h1:NJZpH+1AfhIcyQsPeuBKsUtYrRnjkyu0kIVMCHkZtRY=
|
||||||
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
|
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
|
||||||
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||||
github.com/joho/godotenv v1.4.0 h1:3l4+N6zfMWnkbPEXKng2o2/MR5mSwTrBih4ZEkkz1lg=
|
|
||||||
github.com/joho/godotenv v1.4.0/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
|
|
||||||
github.com/sashabaranov/go-openai v1.5.7 h1:8DGgRG+P7yWixte5j720y6yiXgY3Hlgcd0gcpHdltfo=
|
github.com/sashabaranov/go-openai v1.5.7 h1:8DGgRG+P7yWixte5j720y6yiXgY3Hlgcd0gcpHdltfo=
|
||||||
github.com/sashabaranov/go-openai v1.5.7/go.mod h1:lj5b/K+zjTSFxVLijLSTDZuP7adOgerWeFyZLUhAKRg=
|
github.com/sashabaranov/go-openai v1.5.7/go.mod h1:lj5b/K+zjTSFxVLijLSTDZuP7adOgerWeFyZLUhAKRg=
|
||||||
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b h1:7mWr3k41Qtv8XlltBkDkl8LoP3mpSgBW8BUoxtEdbXg=
|
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b h1:7mWr3k41Qtv8XlltBkDkl8LoP3mpSgBW8BUoxtEdbXg=
|
||||||
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
|
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
|
||||||
golang.org/x/exp v0.0.0-20221031165847-c99f073a8326 h1:QfTh0HpN6hlw6D3vu8DAwC8pBIwikq0AI1evdm+FksE=
|
|
||||||
golang.org/x/exp v0.0.0-20221031165847-c99f073a8326/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
|
|
||||||
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
||||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 h1:nxC68pudNYkKU6jWhgrqdreuFiOQWj1Fs7T3VrH4Pjw=
|
|
||||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/sys v0.1.0 h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U=
|
golang.org/x/sys v0.1.0 h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U=
|
||||||
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
|||||||
30
handlers/members.go
Normal file
30
handlers/members.go
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
package handlers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
|
||||||
|
"github.com/bwmarrin/discordgo"
|
||||||
|
"gitlab.com/DeveloperDurp/durpot/model"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
ChannelID = model.ChannelID
|
||||||
|
)
|
||||||
|
|
||||||
|
func GuildMemberAdd(s *discordgo.Session, m *discordgo.GuildMemberAdd) {
|
||||||
|
message := fmt.Sprintf("Welcome <@%s> to our server!", m.Member.User.ID)
|
||||||
|
_, err := s.ChannelMessageSend(ChannelID, message)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Error sending welcome message: %v\n", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func GuildMemberRemove(s *discordgo.Session, m *discordgo.GuildMemberRemove) {
|
||||||
|
|
||||||
|
message := fmt.Sprintf("Goodbye %s", m.Member.User.Username)
|
||||||
|
_, err := s.ChannelMessageSend(ChannelID, message)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Error sending goodbye message: %v\n", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
30
handlers/messages.go
Normal file
30
handlers/messages.go
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
package handlers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/bwmarrin/discordgo"
|
||||||
|
"gitlab.com/DeveloperDurp/durpot/model"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
BotPrefix = model.BotPrefix
|
||||||
|
)
|
||||||
|
|
||||||
|
func MessageHandler(s *discordgo.Session, m *discordgo.MessageCreate) {
|
||||||
|
if m.Author.ID == s.State.User.ID {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
switch m.Content {
|
||||||
|
case BotPrefix + "ping":
|
||||||
|
_, err := s.ChannelMessageSend(m.ChannelID, "pong")
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Failed to send Message")
|
||||||
|
}
|
||||||
|
case BotPrefix + "unraid":
|
||||||
|
GetUnraidUsage(s, m)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
38
handlers/shared.go
Normal file
38
handlers/shared.go
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
package handlers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/base64"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
func CallDurpAPI(url string, username string, password string) []byte {
|
||||||
|
|
||||||
|
req, err := http.NewRequest("GET", url, nil)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Error creating request:", err)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
auth := username + ":" + password
|
||||||
|
basicAuth := "Basic " + base64.StdEncoding.EncodeToString([]byte(auth))
|
||||||
|
|
||||||
|
req.Header.Set("Authorization", basicAuth)
|
||||||
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
|
||||||
|
client := &http.Client{}
|
||||||
|
resp, err := client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Error sending request:", err)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
body, err := io.ReadAll(resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Error reading response:", err)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return body
|
||||||
|
}
|
||||||
27
handlers/start.go
Normal file
27
handlers/start.go
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
package handlers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/bwmarrin/discordgo"
|
||||||
|
"gitlab.com/DeveloperDurp/durpot/model"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Start() error {
|
||||||
|
goBot, err := discordgo.New("Bot " + model.Token)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return (err)
|
||||||
|
}
|
||||||
|
|
||||||
|
goBot.AddHandler(MessageHandler)
|
||||||
|
goBot.AddHandler(GuildMemberAdd)
|
||||||
|
goBot.AddHandler(GuildMemberRemove)
|
||||||
|
goBot.AddHandler(HandleTag)
|
||||||
|
|
||||||
|
err = goBot.Open()
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return (err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return (err)
|
||||||
|
}
|
||||||
57
handlers/tags.go
Normal file
57
handlers/tags.go
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
package handlers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/bwmarrin/discordgo"
|
||||||
|
"github.com/sashabaranov/go-openai"
|
||||||
|
"gitlab.com/DeveloperDurp/durpot/model"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
ApiKey = model.ApiKey
|
||||||
|
)
|
||||||
|
|
||||||
|
func HandleTag(s *discordgo.Session, m *discordgo.MessageCreate) {
|
||||||
|
if m.Author.ID == s.State.User.ID {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
client := openai.NewClient(ApiKey)
|
||||||
|
|
||||||
|
for _, mention := range m.Mentions {
|
||||||
|
if mention.ID == s.State.User.ID {
|
||||||
|
content := strings.Replace(m.ContentWithMentionsReplaced(), "<@"+s.State.User.ID+">", "", -1)
|
||||||
|
content = strings.Replace(content, "<@!"+s.State.User.ID+">", "", -1)
|
||||||
|
content = strings.TrimSpace(content)
|
||||||
|
|
||||||
|
resp, err := client.CreateChatCompletion(
|
||||||
|
context.Background(),
|
||||||
|
openai.ChatCompletionRequest{
|
||||||
|
Model: openai.GPT3Dot5Turbo,
|
||||||
|
Messages: []openai.ChatCompletionMessage{
|
||||||
|
{
|
||||||
|
Role: openai.ChatMessageRoleUser,
|
||||||
|
Content: content,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("ChatCompletion error: %v\n", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println(resp.Choices[0].Message.Content)
|
||||||
|
|
||||||
|
// Send generated response back to Discord
|
||||||
|
_, err = s.ChannelMessageSend(m.ChannelID, resp.Choices[0].Message.Content)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
128
handlers/unraid.go
Normal file
128
handlers/unraid.go
Normal file
@@ -0,0 +1,128 @@
|
|||||||
|
package handlers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/bwmarrin/discordgo"
|
||||||
|
"gitlab.com/DeveloperDurp/durpot/model"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
ClientID = model.ClientID
|
||||||
|
TokenURL = model.TokenURL
|
||||||
|
username = model.Username
|
||||||
|
password = model.Password
|
||||||
|
)
|
||||||
|
|
||||||
|
func GetUnraidUsage(s *discordgo.Session, m *discordgo.MessageCreate) {
|
||||||
|
|
||||||
|
url := "https://durpapi.durp.info/api/v1/unraid/powerusage"
|
||||||
|
|
||||||
|
body := CallDurpAPI(url, username, password)
|
||||||
|
|
||||||
|
var response model.PowerUsageResponse
|
||||||
|
err := json.Unmarshal(body, &response)
|
||||||
|
if err != nil {
|
||||||
|
_, err = s.ChannelMessageSend(m.ChannelID, "Failed to get Power Usage")
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Failed to send message")
|
||||||
|
}
|
||||||
|
fmt.Println("Error parsing response:", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
jsonData, _ := json.MarshalIndent(response, "", " ")
|
||||||
|
jsonStr := string(jsonData)
|
||||||
|
_, err = s.ChannelMessageSend(m.ChannelID, "Power Usage Response:\n```json\n"+jsonStr+"\n```")
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Failed to send message")
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
//func getSwansonQuote(s *discordgo.Session, m *discordgo.MessageCreate, url string) {
|
||||||
|
// resp, err := http.Get(url + "/ronswanson")
|
||||||
|
// if err != nil {
|
||||||
|
// errStr := err.Error()
|
||||||
|
// s.ChannelMessageSend(m.ChannelID, errStr)
|
||||||
|
// return
|
||||||
|
// }
|
||||||
|
// defer resp.Body.Close()
|
||||||
|
//
|
||||||
|
// var data []string
|
||||||
|
// err = json.NewDecoder(resp.Body).Decode(&data)
|
||||||
|
// if err != nil {
|
||||||
|
// errStr := err.Error()
|
||||||
|
// s.ChannelMessageSend(m.ChannelID, errStr)
|
||||||
|
// return
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// if len(data) == 0 {
|
||||||
|
// errStr := "No quotes found."
|
||||||
|
// s.ChannelMessageSend(m.ChannelID, errStr)
|
||||||
|
// return
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// s.ChannelMessageSend(m.ChannelID, data[0])
|
||||||
|
//}
|
||||||
|
|
||||||
|
//func sendAPIRequest(s *discordgo.Session, m *discordgo.MessageCreate, url string, endpoint string) {
|
||||||
|
// var response interface{}
|
||||||
|
// switch endpoint {
|
||||||
|
// case "dadjoke":
|
||||||
|
// url = url + "/dadjoke"
|
||||||
|
// var data model.DadJokeResponse
|
||||||
|
// response = &data
|
||||||
|
// case "jinglebells":
|
||||||
|
// url = url + "/foaas/jinglebells/durp"
|
||||||
|
// var data model.JingleBellsResponse
|
||||||
|
// response = &data
|
||||||
|
// case "yomama":
|
||||||
|
// url = url + "/yomama"
|
||||||
|
// var data model.YomamaJokeResponse
|
||||||
|
// response = &data
|
||||||
|
// case "swanson":
|
||||||
|
// getSwansonQuote(s, m, url)
|
||||||
|
// return
|
||||||
|
// default:
|
||||||
|
// s.ChannelMessageSend(m.ChannelID, "Invalid endpoint.")
|
||||||
|
// return
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// req, err := http.NewRequest("GET", url, nil)
|
||||||
|
// if err != nil {
|
||||||
|
// errStr := err.Error()
|
||||||
|
// s.ChannelMessageSend(m.ChannelID, errStr)
|
||||||
|
// return
|
||||||
|
// }
|
||||||
|
// req.Header.Set("Accept", "application/json")
|
||||||
|
//
|
||||||
|
// client := http.Client{}
|
||||||
|
// resp, err := client.Do(req)
|
||||||
|
// if err != nil {
|
||||||
|
// errStr := err.Error()
|
||||||
|
// s.ChannelMessageSend(m.ChannelID, errStr)
|
||||||
|
// return
|
||||||
|
// }
|
||||||
|
// defer resp.Body.Close()
|
||||||
|
//
|
||||||
|
// err = json.NewDecoder(resp.Body).Decode(response)
|
||||||
|
// if err != nil {
|
||||||
|
// errStr := err.Error()
|
||||||
|
// s.ChannelMessageSend(m.ChannelID, errStr)
|
||||||
|
// return
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// switch endpoint {
|
||||||
|
// case "dadjoke":
|
||||||
|
// data := response.(*model.DadJokeResponse)
|
||||||
|
// s.ChannelMessageSend(m.ChannelID, data.Joke)
|
||||||
|
// case "jinglebells":
|
||||||
|
// data := response.(*model.JingleBellsResponse)
|
||||||
|
// s.ChannelMessageSend(m.ChannelID, data.Message)
|
||||||
|
// case "yomama":
|
||||||
|
// data := response.(*model.YomamaJokeResponse)
|
||||||
|
// s.ChannelMessageSend(m.ChannelID, data.Joke)
|
||||||
|
// }
|
||||||
|
//}
|
||||||
281
main.go
281
main.go
@@ -1,293 +1,18 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
|
||||||
"net/http"
|
|
||||||
"os"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/bwmarrin/discordgo"
|
"gitlab.com/DeveloperDurp/durpot/handlers"
|
||||||
"github.com/joho/godotenv"
|
|
||||||
openai "github.com/sashabaranov/go-openai"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
func main() {
|
||||||
Token string
|
|
||||||
BotPrefix string
|
|
||||||
ChannelID string
|
|
||||||
BotId string
|
|
||||||
apiKey string
|
|
||||||
goBot *discordgo.Session
|
|
||||||
|
|
||||||
config *configStruct
|
|
||||||
)
|
|
||||||
|
|
||||||
type configStruct struct {
|
|
||||||
Token string `json : "Token"`
|
|
||||||
BotPrefix string `json : "BotPrefix"`
|
|
||||||
ChannelID string `json : "ChannelID"`
|
|
||||||
apiKey string `json : "OPEN_API_KEY"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type jingleBellsResponse struct {
|
|
||||||
Message string `json:"message"`
|
|
||||||
Subtitle string `json:"subtitle"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type dadJokeResponse struct {
|
|
||||||
ID string `json:"id"`
|
|
||||||
Joke string `json:"joke"`
|
|
||||||
Status int `json:"status"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type yomamaJokeResponse struct {
|
|
||||||
Joke string `json:"joke"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func ReadConfig() error {
|
|
||||||
|
|
||||||
err := godotenv.Load(".env")
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println(err.Error())
|
|
||||||
//return err
|
|
||||||
}
|
|
||||||
|
|
||||||
Token = os.Getenv("TOKEN")
|
|
||||||
BotPrefix = os.Getenv("BOTPREFIX")
|
|
||||||
ChannelID = os.Getenv("ChannelID")
|
|
||||||
apiKey = os.Getenv("OPENAI_API_KEY")
|
|
||||||
|
|
||||||
return nil
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
func Start() {
|
|
||||||
goBot, err := discordgo.New("Bot " + Token)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println(err.Error())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
u, err := goBot.User("@me")
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println(err.Error())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
BotId = u.ID
|
|
||||||
|
|
||||||
goBot.AddHandler(messageHandler)
|
|
||||||
goBot.AddHandler(handleGuildMemberAdd)
|
|
||||||
goBot.AddHandler(handleGuildMemberRemove)
|
|
||||||
goBot.AddHandler(handleTag)
|
|
||||||
|
|
||||||
err = goBot.Open()
|
|
||||||
|
|
||||||
|
err := handlers.Start()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err.Error())
|
fmt.Println(err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
fmt.Println("Bot is running!")
|
fmt.Println("Bot is running!")
|
||||||
}
|
|
||||||
|
|
||||||
func messageHandler(s *discordgo.Session, m *discordgo.MessageCreate) {
|
|
||||||
if m.Author.ID == BotId {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
baseurl := "https://kong.durp.info/"
|
|
||||||
switch m.Content {
|
|
||||||
case BotPrefix + "ping":
|
|
||||||
s.ChannelMessageSend(m.ChannelID, "pong")
|
|
||||||
//case BotPrefix + "meme":
|
|
||||||
// response = getJson(baseurl+"random-meme", "url")
|
|
||||||
// s.ChannelMessageSend(m.ChannelID, response)
|
|
||||||
//case BotPrefix + "catfact":
|
|
||||||
// response = getJson(baseurl+"cat-facts/fact", "fact")
|
|
||||||
// s.ChannelMessageSend(m.ChannelID, response)
|
|
||||||
//case BotPrefix + "cat":
|
|
||||||
// response = getJson(baseurl+"random-cats", "file")
|
|
||||||
// s.ChannelMessageSend(m.ChannelID, response)
|
|
||||||
case BotPrefix + "yomama":
|
|
||||||
sendAPIRequest(s, m, baseurl, "yomama")
|
|
||||||
case BotPrefix + "dadjoke":
|
|
||||||
sendAPIRequest(s, m, baseurl, "dadjoke")
|
|
||||||
//case BotPrefix + "dog":
|
|
||||||
// response = getJson(baseurl+"random-dogs", "message")
|
|
||||||
// s.ChannelMessageSend(m.ChannelID, response)
|
|
||||||
case BotPrefix + "jinglebells":
|
|
||||||
sendAPIRequest(s, m, baseurl, "jinglebells")
|
|
||||||
case BotPrefix + "swanson":
|
|
||||||
sendAPIRequest(s, m, baseurl, "swanson")
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
err := ReadConfig()
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println(err.Error())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
Start()
|
|
||||||
|
|
||||||
<-make(chan struct{})
|
<-make(chan struct{})
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func getSwansonQuote(s *discordgo.Session, m *discordgo.MessageCreate, url string) {
|
|
||||||
resp, err := http.Get(url + "/ronswanson")
|
|
||||||
if err != nil {
|
|
||||||
errStr := err.Error()
|
|
||||||
s.ChannelMessageSend(m.ChannelID, errStr)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
defer resp.Body.Close()
|
|
||||||
|
|
||||||
var data []string
|
|
||||||
err = json.NewDecoder(resp.Body).Decode(&data)
|
|
||||||
if err != nil {
|
|
||||||
errStr := err.Error()
|
|
||||||
s.ChannelMessageSend(m.ChannelID, errStr)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(data) == 0 {
|
|
||||||
errStr := "No quotes found."
|
|
||||||
s.ChannelMessageSend(m.ChannelID, errStr)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
s.ChannelMessageSend(m.ChannelID, data[0])
|
|
||||||
}
|
|
||||||
|
|
||||||
func sendAPIRequest(s *discordgo.Session, m *discordgo.MessageCreate, url string, endpoint string) {
|
|
||||||
var response interface{}
|
|
||||||
switch endpoint {
|
|
||||||
case "dadjoke":
|
|
||||||
url = url + "/dadjoke"
|
|
||||||
var data dadJokeResponse
|
|
||||||
response = &data
|
|
||||||
case "jinglebells":
|
|
||||||
url = url + "/foaas/jinglebells/durp"
|
|
||||||
var data jingleBellsResponse
|
|
||||||
response = &data
|
|
||||||
case "yomama":
|
|
||||||
url = url + "/yomama"
|
|
||||||
var data yomamaJokeResponse
|
|
||||||
response = &data
|
|
||||||
case "swanson":
|
|
||||||
getSwansonQuote(s, m, url)
|
|
||||||
return
|
|
||||||
default:
|
|
||||||
s.ChannelMessageSend(m.ChannelID, "Invalid endpoint.")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
req, err := http.NewRequest("GET", url, nil)
|
|
||||||
if err != nil {
|
|
||||||
errStr := err.Error()
|
|
||||||
s.ChannelMessageSend(m.ChannelID, errStr)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
req.Header.Set("Accept", "application/json")
|
|
||||||
|
|
||||||
client := http.Client{}
|
|
||||||
resp, err := client.Do(req)
|
|
||||||
if err != nil {
|
|
||||||
errStr := err.Error()
|
|
||||||
s.ChannelMessageSend(m.ChannelID, errStr)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
defer resp.Body.Close()
|
|
||||||
|
|
||||||
err = json.NewDecoder(resp.Body).Decode(response)
|
|
||||||
if err != nil {
|
|
||||||
errStr := err.Error()
|
|
||||||
s.ChannelMessageSend(m.ChannelID, errStr)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
switch endpoint {
|
|
||||||
case "dadjoke":
|
|
||||||
data := response.(*dadJokeResponse)
|
|
||||||
s.ChannelMessageSend(m.ChannelID, data.Joke)
|
|
||||||
case "jinglebells":
|
|
||||||
data := response.(*jingleBellsResponse)
|
|
||||||
s.ChannelMessageSend(m.ChannelID, data.Message)
|
|
||||||
case "yomama":
|
|
||||||
data := response.(*yomamaJokeResponse)
|
|
||||||
s.ChannelMessageSend(m.ChannelID, data.Joke)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func handleGuildMemberAdd(s *discordgo.Session, m *discordgo.GuildMemberAdd) {
|
|
||||||
message := fmt.Sprintf("Welcome <@%s> to our server!", m.Member.User.ID)
|
|
||||||
_, err := s.ChannelMessageSend(ChannelID, message)
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("Error sending welcome message: %v\n", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func handleGuildMemberRemove(s *discordgo.Session, m *discordgo.GuildMemberRemove) {
|
|
||||||
|
|
||||||
message := fmt.Sprintf("Goodbye %s", m.Member.User.Username)
|
|
||||||
_, err := s.ChannelMessageSend(ChannelID, message)
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("Error sending goodbye message: %v\n", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func handleTag(s *discordgo.Session, m *discordgo.MessageCreate) {
|
|
||||||
// Ignore messages sent by the bot itself
|
|
||||||
if m.Author.ID == s.State.User.ID {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
client := openai.NewClient(apiKey)
|
|
||||||
|
|
||||||
// Check if bot is mentioned in message
|
|
||||||
for _, mention := range m.Mentions {
|
|
||||||
if mention.ID == s.State.User.ID {
|
|
||||||
// Remove mention from message content
|
|
||||||
content := strings.Replace(m.ContentWithMentionsReplaced(), "<@"+s.State.User.ID+">", "", -1)
|
|
||||||
content = strings.Replace(content, "<@!"+s.State.User.ID+">", "", -1)
|
|
||||||
content = strings.TrimSpace(content)
|
|
||||||
|
|
||||||
resp, err := client.CreateChatCompletion(
|
|
||||||
context.Background(),
|
|
||||||
openai.ChatCompletionRequest{
|
|
||||||
Model: openai.GPT3Dot5Turbo,
|
|
||||||
Messages: []openai.ChatCompletionMessage{
|
|
||||||
{
|
|
||||||
Role: openai.ChatMessageRoleUser,
|
|
||||||
Content: content,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("ChatCompletion error: %v\n", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Println(resp.Choices[0].Message.Content)
|
|
||||||
|
|
||||||
// Send generated response back to Discord
|
|
||||||
_, err = s.ChannelMessageSend(m.ChannelID, resp.Choices[0].Message.Content)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
43
model/model.go
Normal file
43
model/model.go
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
package model
|
||||||
|
|
||||||
|
import "os"
|
||||||
|
|
||||||
|
var (
|
||||||
|
Token = os.Getenv("TOKEN")
|
||||||
|
BotPrefix = os.Getenv("BOTPREFIX")
|
||||||
|
ChannelID = os.Getenv("ChannelID")
|
||||||
|
ApiKey = os.Getenv("OPENAI_API_KEY")
|
||||||
|
ClientID = os.Getenv("ClientID")
|
||||||
|
TokenURL = os.Getenv("TokenURL")
|
||||||
|
Username = os.Getenv("Username")
|
||||||
|
Password = os.Getenv("Password")
|
||||||
|
)
|
||||||
|
|
||||||
|
type AccessTokenResponse struct {
|
||||||
|
AccessToken string `json:"access_token"`
|
||||||
|
TokenType string `json:"token_type"`
|
||||||
|
ExpiresIn int `json:"expires_in"`
|
||||||
|
IDToken string `json:"id_token"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type PowerUsageResponse struct {
|
||||||
|
Voltage12VLoad int `json:"12v_load"`
|
||||||
|
Voltage12VWatts float64 `json:"12v_watts"`
|
||||||
|
Voltage3VLoad int `json:"3v_load"`
|
||||||
|
Voltage3VWatts float64 `json:"3v_watts"`
|
||||||
|
Voltage5VLoad int `json:"5v_load"`
|
||||||
|
Voltage5VWatts float64 `json:"5v_watts"`
|
||||||
|
Capacity string `json:"capacity"`
|
||||||
|
Efficiency float64 `json:"efficiency"`
|
||||||
|
FanRPM int `json:"fan_rpm"`
|
||||||
|
Load int `json:"load"`
|
||||||
|
PoweredOn string `json:"poweredon"`
|
||||||
|
PoweredOnRaw string `json:"poweredon_raw"`
|
||||||
|
Product string `json:"product"`
|
||||||
|
Temperature1 float64 `json:"temp1"`
|
||||||
|
Temperature2 float64 `json:"temp2"`
|
||||||
|
Uptime string `json:"uptime"`
|
||||||
|
UptimeRaw string `json:"uptime_raw"`
|
||||||
|
Vendor string `json:"vendor"`
|
||||||
|
TotalWatts float64 `json:"watts"`
|
||||||
|
}
|
||||||
4
sonar-project.properties
Normal file
4
sonar-project.properties
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
sonar.projectKey=durpot
|
||||||
|
sonar.organization=developerdurp
|
||||||
|
sonar.sources=.
|
||||||
|
sonar.host.url=https://sonarcloud.io
|
||||||
Reference in New Issue
Block a user