updates
This commit is contained in:
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)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,23 +1,24 @@
|
|||||||
package handlers
|
package handlers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/base64"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"gitlab.com/DeveloperDurp/durpot/model"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func CallDurpAPI(url string, accesstoken string) []byte {
|
func CallDurpAPI(url string, username string, password string) []byte {
|
||||||
|
|
||||||
req, err := http.NewRequest("GET", url, nil)
|
req, err := http.NewRequest("GET", url, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Error creating request:", err)
|
fmt.Println("Error creating request:", err)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
req.Header.Set("Authorization", "Bearer "+accesstoken)
|
auth := username + ":" + password
|
||||||
|
basicAuth := "Basic " + base64.StdEncoding.EncodeToString([]byte(auth))
|
||||||
|
|
||||||
|
req.Header.Set("Authorization", basicAuth)
|
||||||
req.Header.Set("Content-Type", "application/json")
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
|
||||||
client := &http.Client{}
|
client := &http.Client{}
|
||||||
@@ -35,41 +36,3 @@ func CallDurpAPI(url string, accesstoken string) []byte {
|
|||||||
}
|
}
|
||||||
return body
|
return body
|
||||||
}
|
}
|
||||||
|
|
||||||
func GenerateToken(clientID string, grantType string, url string, username string, password string) model.AccessTokenResponse {
|
|
||||||
|
|
||||||
formData := fmt.Sprintf("grant_type=%s&client_id=%s&username=%s&password=%s",
|
|
||||||
grantType, clientID, username, password)
|
|
||||||
|
|
||||||
client := &http.Client{}
|
|
||||||
|
|
||||||
req, err := http.NewRequest("POST", url, strings.NewReader(formData))
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println("Error creating request:", err)
|
|
||||||
return model.AccessTokenResponse{}
|
|
||||||
}
|
|
||||||
|
|
||||||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
||||||
|
|
||||||
resp, err := client.Do(req)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println("Error sending request:", err)
|
|
||||||
return model.AccessTokenResponse{}
|
|
||||||
}
|
|
||||||
defer resp.Body.Close()
|
|
||||||
|
|
||||||
body, err := io.ReadAll(resp.Body)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println("Error reading response:", err)
|
|
||||||
return model.AccessTokenResponse{}
|
|
||||||
}
|
|
||||||
|
|
||||||
var response model.AccessTokenResponse
|
|
||||||
err = json.Unmarshal(body, &response)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println("Error parsing response:", err)
|
|
||||||
return model.AccessTokenResponse{}
|
|
||||||
}
|
|
||||||
|
|
||||||
return response
|
|
||||||
}
|
|
||||||
|
|||||||
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -11,22 +11,20 @@ import (
|
|||||||
var (
|
var (
|
||||||
ClientID = model.ClientID
|
ClientID = model.ClientID
|
||||||
TokenURL = model.TokenURL
|
TokenURL = model.TokenURL
|
||||||
Username = model.Username
|
username = model.Username
|
||||||
Password = model.Password
|
password = model.Password
|
||||||
)
|
)
|
||||||
|
|
||||||
func GetUnraidUsage(s *discordgo.Session, m *discordgo.MessageCreate) {
|
func GetUnraidUsage(s *discordgo.Session, m *discordgo.MessageCreate) {
|
||||||
|
|
||||||
token := GenerateToken(ClientID, "client_credentials", TokenURL, Username, Password)
|
|
||||||
|
|
||||||
url := "https://durpapi.durp.info/api/v1/unraid/powerusage"
|
url := "https://durpapi.durp.info/api/v1/unraid/powerusage"
|
||||||
accessToken := token.AccessToken
|
|
||||||
|
|
||||||
body := CallDurpAPI(url, accessToken)
|
body := CallDurpAPI(url, username, password)
|
||||||
|
|
||||||
var response model.PowerUsageResponse
|
var response model.PowerUsageResponse
|
||||||
err := json.Unmarshal(body, &response)
|
err := json.Unmarshal(body, &response)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
s.ChannelMessageSend(m.ChannelID, "Failed to get Power Usage")
|
||||||
fmt.Println("Error parsing response:", err)
|
fmt.Println("Error parsing response:", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
108
main.go
108
main.go
@@ -1,122 +1,18 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/bwmarrin/discordgo"
|
|
||||||
openai "github.com/sashabaranov/go-openai"
|
|
||||||
"gitlab.com/DeveloperDurp/durpot/handlers"
|
"gitlab.com/DeveloperDurp/durpot/handlers"
|
||||||
"gitlab.com/DeveloperDurp/durpot/model"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
func main() {
|
||||||
Token = model.Token
|
|
||||||
BotPrefix = model.BotPrefix
|
|
||||||
ChannelID = model.ChannelID
|
|
||||||
BotId string
|
|
||||||
ApiKey = model.ApiKey
|
|
||||||
)
|
|
||||||
|
|
||||||
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(handlers.GuildMemberAdd)
|
|
||||||
goBot.AddHandler(handlers.GuildMemberRemove)
|
|
||||||
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":
|
|
||||||
_, err := s.ChannelMessageSend(m.ChannelID, "pong")
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println("Failed to send Message")
|
|
||||||
}
|
|
||||||
case BotPrefix + "unraid":
|
|
||||||
handlers.GetUnraidUsage(s, m)
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
|
|
||||||
Start()
|
|
||||||
|
|
||||||
<-make(chan struct{})
|
<-make(chan struct{})
|
||||||
}
|
}
|
||||||
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user