add command and reply to when mentionned
This commit is contained in:
parent
78e6a35b03
commit
560fc41df0
97
main.go
97
main.go
|
|
@ -1,21 +1,44 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
"github.com/bwmarrin/discordgo"
|
"github.com/bwmarrin/discordgo"
|
||||||
"os"
|
|
||||||
"log"
|
|
||||||
"os/signal"
|
|
||||||
"syscall"
|
|
||||||
"math/rand"
|
|
||||||
"github.com/conneroisu/groq-go"
|
"github.com/conneroisu/groq-go"
|
||||||
"github.com/joho/godotenv"
|
"github.com/joho/godotenv"
|
||||||
|
"log"
|
||||||
|
"math/rand"
|
||||||
|
"os"
|
||||||
|
"os/signal"
|
||||||
|
"syscall"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
Token string
|
Token string
|
||||||
GroqKey string
|
GroqKey string
|
||||||
|
defaultThreshold = 0.1
|
||||||
|
defaultMaxTokens = 100
|
||||||
|
defaultTemperature float32 = 0.5
|
||||||
|
defaultMessagesCount = 100
|
||||||
|
|
||||||
|
commands = []*discordgo.ApplicationCommand{
|
||||||
|
{
|
||||||
|
Name: "ping",
|
||||||
|
Description: "Replies with Pong!",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
commandHandlers = map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate){
|
||||||
|
"ping": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
|
log.Println("ping command")
|
||||||
|
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||||
|
Data: &discordgo.InteractionResponseData{
|
||||||
|
Content: "Pong!",
|
||||||
|
},
|
||||||
|
})
|
||||||
|
},
|
||||||
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
|
@ -44,8 +67,12 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
dg.AddHandler(messageCreate)
|
dg.AddHandler(messageCreate)
|
||||||
|
dg.AddHandler(joiningGuild)
|
||||||
|
dg.AddHandler(leavingGuild)
|
||||||
|
|
||||||
dg.Identify.Intents = discordgo.IntentsGuildMessages
|
dg.AddHandler(userCommand)
|
||||||
|
|
||||||
|
dg.Identify.Intents = discordgo.IntentsGuildMessages | discordgo.IntentsGuilds
|
||||||
|
|
||||||
err = dg.Open()
|
err = dg.Open()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -53,6 +80,8 @@ func main() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
checkRegisteredCommands(dg)
|
||||||
|
|
||||||
log.Println("Bot is now running. Press CTRL-C to exit.")
|
log.Println("Bot is now running. Press CTRL-C to exit.")
|
||||||
sc := make(chan os.Signal, 1)
|
sc := make(chan os.Signal, 1)
|
||||||
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
|
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
|
||||||
|
|
@ -61,8 +90,46 @@ func main() {
|
||||||
dg.Close()
|
dg.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func checkRegisteredCommands(s *discordgo.Session) {
|
||||||
|
for _, v := range s.State.Guilds {
|
||||||
|
registerCommands(s, v.ID)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func registerCommands(s *discordgo.Session, guildID string) {
|
||||||
|
registeredCommands := make([]*discordgo.ApplicationCommand, len(commands))
|
||||||
|
for i, v := range commands {
|
||||||
|
cmd, err := s.ApplicationCommandCreate(s.State.User.ID, guildID, v)
|
||||||
|
if err != nil {
|
||||||
|
log.Panicf("Cannot create '%v' command: %v", v.Name, err)
|
||||||
|
}
|
||||||
|
registeredCommands[i] = cmd
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func userCommand(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
|
handler, ok := commandHandlers[i.ApplicationCommandData().Name]
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
handler(s, i)
|
||||||
|
}
|
||||||
|
|
||||||
|
func joiningGuild(s *discordgo.Session, m *discordgo.GuildCreate) {
|
||||||
|
registerCommands(s, m.ID)
|
||||||
|
}
|
||||||
|
|
||||||
|
func leavingGuild(s *discordgo.Session, m *discordgo.GuildDelete) {
|
||||||
|
for _, v := range commands {
|
||||||
|
err := s.ApplicationCommandDelete(s.State.User.ID, m.ID, v.ID)
|
||||||
|
if err != nil {
|
||||||
|
log.Panicf("Cannot delete '%v' command: %v", v.Name, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func getMessages(s *discordgo.Session, channelID string, num int) ([]*discordgo.Message, error) {
|
func getMessages(s *discordgo.Session, channelID string, num int) ([]*discordgo.Message, error) {
|
||||||
if (num <= 100) {
|
if num <= 100 {
|
||||||
messages, err := s.ChannelMessages(channelID, num, "", "", "")
|
messages, err := s.ChannelMessages(channelID, num, "", "", "")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("error getting messages,", err)
|
log.Println("error getting messages,", err)
|
||||||
|
|
@ -108,9 +175,9 @@ func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
rand := rand.Intn(100)
|
rand := rand.Float32()
|
||||||
|
|
||||||
if rand > 10 && !botMentioned(s, m){
|
if rand > float32(defaultThreshold) && !botMentioned(s, m) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -131,10 +198,14 @@ func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
|
||||||
question += "<messages>\n" + messagesFormatted + "\n</messages>"
|
question += "<messages>\n" + messagesFormatted + "\n</messages>"
|
||||||
response, err := askGroq(context.Background(), question)
|
response, err := askGroq(context.Background(), question)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
if botMentioned(s, m) {
|
||||||
|
s.ChannelMessageSendReply(m.ChannelID, "There was an error getting the response.", m.Reference())
|
||||||
|
} else {
|
||||||
s.ChannelMessageSend(m.ChannelID, "There was an error getting the response.")
|
s.ChannelMessageSend(m.ChannelID, "There was an error getting the response.")
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
s.ChannelMessageSend(m.ChannelID, response)
|
s.ChannelMessageSendReply(m.ChannelID, response, m.Reference())
|
||||||
}
|
}
|
||||||
|
|
||||||
func askGroq(ctx context.Context, message string) (string, error) {
|
func askGroq(ctx context.Context, message string) (string, error) {
|
||||||
|
|
@ -152,13 +223,13 @@ func askGroq(ctx context.Context, message string) (string,error) {
|
||||||
Content: message,
|
Content: message,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
MaxTokens: 100,
|
MaxTokens: defaultMaxTokens,
|
||||||
|
Temperature: defaultTemperature,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("error creating Groq completion,", err)
|
fmt.Println("error creating Groq completion,", err)
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
return string(resp.Choices[0].Message.Content), nil
|
return string(resp.Choices[0].Message.Content), nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue