From 10ba1af4056cc26b9e6209454e634468360d4a0c Mon Sep 17 00:00:00 2001 From: Cynthia Foxwell Date: Sat, 8 Jul 2023 14:51:26 -0600 Subject: [PATCH] basic command system --- commands/help.go | 36 ++++++++++++++++++++++++++++ commands/main.go | 33 ++++++++++++++++++++++++++ commands/quit.go | 12 ++++++++++ events/messageCreate.go | 9 ------- events/messages.go | 31 ++++++++++++++++++++++++ events/ready.go | 4 +++- go.mod | 2 ++ go.sum | 52 +++++++++++++++++++++++++++++++++++++++++ main.go | 30 ++++++++++++++++++------ state/main.go | 9 +++++++ 10 files changed, 201 insertions(+), 17 deletions(-) create mode 100644 commands/help.go create mode 100644 commands/main.go create mode 100644 commands/quit.go delete mode 100644 events/messageCreate.go create mode 100644 events/messages.go diff --git a/commands/help.go b/commands/help.go new file mode 100644 index 0000000..2ed4e10 --- /dev/null +++ b/commands/help.go @@ -0,0 +1,36 @@ +package commands + +import ( + "fmt" + "strings" + + "github.com/bwmarrin/discordgo" + "github.com/fatih/color" +) + +func HelpCommand(session *discordgo.Session) { + fmt.Println("\r\nCOMcord (c)left 2023\n\r") + + index := 0 + for key, cmd := range GetAllCommands() { + str := fmt.Sprintf(" %s - %s", key, cmd.Description) + length := len(str) + + fmt.Print(" ") + color.Set(color.FgYellow, color.Bold) + fmt.Print(key) + color.Unset() + fmt.Printf(" - %s", cmd.Description) + fmt.Print(strings.Repeat(" ", 25 - length)) + + index++ + if index % 3 == 0 { + fmt.Print("\n\r") + } + } + if index % 3 != 0 { + fmt.Print("\n\r") + } + + fmt.Println("\r\nTo begin TALK MODE, press [SPACE]\n\r") +} diff --git a/commands/main.go b/commands/main.go new file mode 100644 index 0000000..8665df0 --- /dev/null +++ b/commands/main.go @@ -0,0 +1,33 @@ +package commands + +import "github.com/bwmarrin/discordgo" + +var commandMap map[string]Command + +type Command struct { + Run func(*discordgo.Session) + Description string +} + +func Setup() { + commandMap = make(map[string]Command) + + commandMap["q"] = Command{ + Run: QuitCommand, + Description: "quit comcord", + } + + commandMap["h"] = Command{ + Run: HelpCommand, + Description: "command help", + } +} + +func GetCommand(key string) (Command, bool) { + command, has := commandMap[key] + return command, has +} + +func GetAllCommands() map[string]Command { + return commandMap +} diff --git a/commands/quit.go b/commands/quit.go new file mode 100644 index 0000000..eea1472 --- /dev/null +++ b/commands/quit.go @@ -0,0 +1,12 @@ +package commands + +import ( + "os" + + "github.com/bwmarrin/discordgo" +) + +func QuitCommand(session *discordgo.Session) { + session.Close() + os.Exit(0) +} diff --git a/events/messageCreate.go b/events/messageCreate.go deleted file mode 100644 index 35da9de..0000000 --- a/events/messageCreate.go +++ /dev/null @@ -1,9 +0,0 @@ -package events - -import "github.com/bwmarrin/discordgo" - -func MessageCreate(session *discordgo.Session, msg *discordgo.MessageCreate) { - if (msg.Author.ID == session.State.User.ID) { - return - } -} diff --git a/events/messages.go b/events/messages.go new file mode 100644 index 0000000..0b86b30 --- /dev/null +++ b/events/messages.go @@ -0,0 +1,31 @@ +package events + +import ( + "github.com/Cynosphere/comcord/state" + "github.com/bwmarrin/discordgo" +) + +func MessageCreate(session *discordgo.Session, msg *discordgo.MessageCreate) { + if (msg.Author.ID == session.State.User.ID) { + return + } + + channel, err := session.State.Channel(msg.ChannelID) + if err != nil { + return + } + + if state.IsInPrompt() { + state.AddMessageToQueue(*msg.Message) + } else { + // TODO + } + + if channel.Type == discordgo.ChannelTypeDM || channel.Type == discordgo.ChannelTypeGroupDM { + state.SetLastDM(msg.ChannelID) + } +} + +func MessageUpdate(session *discordgo.Session, msg *discordgo.MessageUpdate) { + +} diff --git a/events/ready.go b/events/ready.go index d69845d..04cbfd6 100644 --- a/events/ready.go +++ b/events/ready.go @@ -9,5 +9,7 @@ import ( func Ready(session *discordgo.Session, event *discordgo.Ready) { fmt.Print("Logged in as: ") - color.Yellow("%s (%s)", session.State.User.Username, session.State.User.ID) + color.Set(color.FgYellow) + fmt.Printf("%s (%s)\n", session.State.User.Username, session.State.User.ID) + color.Unset() } diff --git a/go.mod b/go.mod index 22e39d1..50095c3 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,9 @@ module github.com/Cynosphere/comcord go 1.20 require ( + atomicgo.dev/keyboard v0.2.9 // indirect github.com/bwmarrin/discordgo v0.27.1 // indirect + github.com/containerd/console v1.0.3 // indirect github.com/fatih/color v1.15.0 // indirect github.com/gorilla/websocket v1.4.2 // indirect github.com/mattn/go-colorable v0.1.13 // indirect diff --git a/go.sum b/go.sum index bba7236..61739b6 100644 --- a/go.sum +++ b/go.sum @@ -1,22 +1,74 @@ +atomicgo.dev/keyboard v0.2.9 h1:tOsIid3nlPLZ3lwgG8KZMp/SFmr7P0ssEN5JUsm78K8= +atomicgo.dev/keyboard v0.2.9/go.mod h1:BC4w9g00XkxH/f1HXhW2sXmJFOCWbKn9xrOunSFtExQ= +github.com/MarvinJWendt/testza v0.1.0/go.mod h1:7AxNvlfeHP7Z/hDQ5JtE3OKYT3XFUeLCDE2DQninSqs= +github.com/MarvinJWendt/testza v0.2.1/go.mod h1:God7bhG8n6uQxwdScay+gjm9/LnO4D3kkcZX4hv9Rp8= +github.com/MarvinJWendt/testza v0.2.8/go.mod h1:nwIcjmr0Zz+Rcwfh3/4UhBp7ePKVhuBExvZqnKYWlII= +github.com/MarvinJWendt/testza v0.2.10/go.mod h1:pd+VWsoGUiFtq+hRKSU1Bktnn+DMCSrDrXDpX2bG66k= +github.com/MarvinJWendt/testza v0.2.12/go.mod h1:JOIegYyV7rX+7VZ9r77L/eH6CfJHHzXjB69adAhzZkI= +github.com/MarvinJWendt/testza v0.3.0/go.mod h1:eFcL4I0idjtIx8P9C6KkAuLgATNKpX4/2oUqKc6bF2c= +github.com/MarvinJWendt/testza v0.4.2/go.mod h1:mSdhXiKH8sg/gQehJ63bINcCKp7RtYewEjXsvsVUPbE= +github.com/atomicgo/cursor v0.0.1/go.mod h1:cBON2QmmrysudxNBFthvMtN32r3jxVRIvzkUiF/RuIk= github.com/bwmarrin/discordgo v0.27.1 h1:ib9AIc/dom1E/fSIulrBwnez0CToJE113ZGt4HoliGY= github.com/bwmarrin/discordgo v0.27.1/go.mod h1:NJZpH+1AfhIcyQsPeuBKsUtYrRnjkyu0kIVMCHkZtRY= +github.com/containerd/console v1.0.3 h1:lIr7SlA5PxZyMV30bDW0MGbiOPXwc63yRuCP0ARubLw= +github.com/containerd/console v1.0.3/go.mod h1:7LqA/THxQ86k76b8c/EMSiaJ3h1eZkMkXar0TQ1gf3U= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs= github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBDUSsw= +github.com/gookit/color v1.4.2/go.mod h1:fqRyamkC1W8uxl+lxCQxOT09l/vYfZ+QeiX3rKQHCoQ= +github.com/gookit/color v1.5.0/go.mod h1:43aQb+Zerm/BWh2GnrgOQm7ffz7tvQXEKV6BFMl7wAo= 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/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= +github.com/klauspost/cpuid/v2 v2.0.10/go.mod h1:g2LTdtYhdyuGPqyWyv7qRAmj1WBqxuObKfj5c0PQa7c= +github.com/klauspost/cpuid/v2 v2.0.12/go.mod h1:g2LTdtYhdyuGPqyWyv7qRAmj1WBqxuObKfj5c0PQa7c= +github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng= github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= +github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/pterm/pterm v0.12.27/go.mod h1:PhQ89w4i95rhgE+xedAoqous6K9X+r6aSOI2eFF7DZI= +github.com/pterm/pterm v0.12.29/go.mod h1:WI3qxgvoQFFGKGjGnJR849gU0TsEOvKn5Q8LlY1U7lg= +github.com/pterm/pterm v0.12.30/go.mod h1:MOqLIyMOgmTDz9yorcYbcw+HsgoZo3BQfg2wtl3HEFE= +github.com/pterm/pterm v0.12.31/go.mod h1:32ZAWZVXD7ZfG0s8qqHXePte42kdz8ECtRyEejaWgXU= +github.com/pterm/pterm v0.12.33/go.mod h1:x+h2uL+n7CP/rel9+bImHD5lF3nM9vJj80k9ybiiTTE= +github.com/pterm/pterm v0.12.36/go.mod h1:NjiL09hFhT/vWjQHSj1athJpx6H8cjpHXNAK5bUw8T8= +github.com/pterm/pterm v0.12.40/go.mod h1:ffwPLwlbXxP+rxT0GsgDTzS3y3rmpAO1NMjUkGTYf8s= +github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= +github.com/sergi/go-diff v1.2.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778/go.mod h1:2MuV+tbUrU1zIOPMxZ5EncGwgmMJsa+9ucAQZXxsObs= 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/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-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20211013075003-97ac67df715c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220319134239-a9b59b0215f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/term v0.0.0-20210615171337-6886f2dfbf5b/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= +golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/main.go b/main.go index 042c9a0..abc776a 100644 --- a/main.go +++ b/main.go @@ -2,11 +2,12 @@ package main import ( "fmt" - "os" - "os/signal" +"os" "strings" - "syscall" + "atomicgo.dev/keyboard" + "atomicgo.dev/keyboard/keys" + "github.com/Cynosphere/comcord/commands" "github.com/Cynosphere/comcord/events" "github.com/Cynosphere/comcord/rcfile" "github.com/Cynosphere/comcord/state" @@ -48,6 +49,7 @@ func main() { } state.Setup() + commands.Setup() // TODO: user account support client, err := discordgo.New("Bot " + token) @@ -73,9 +75,23 @@ func main() { fmt.Println("COMcord (c)left 2023") fmt.Println("Type 'h' for Commands") - sc := make(chan os.Signal, 1) - signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt) - <-sc + keyboard.Listen(func(key keys.Key) (stop bool, err error) { + if !state.IsInPrompt() { + if key.Code == keys.CtrlC { + client.Close() + os.Exit(0) + return true, nil + } else { + command, has := commands.GetCommand(key.String()) + if has { + command.Run(client) + } else { - client.Close() + } + } + + } + + return false, nil + }) } diff --git a/state/main.go b/state/main.go index 5ef3b8f..cd23340 100644 --- a/state/main.go +++ b/state/main.go @@ -17,6 +17,7 @@ type ComcordState struct { AFK bool MessageQueue []discordgo.Message LastChannel map[string]string + LastDM string } var state ComcordState @@ -100,3 +101,11 @@ func GetMessageQueue() []discordgo.Message { func AddMessageToQueue(msg discordgo.Message) { state.MessageQueue = append(state.MessageQueue, msg) } + +func GetLastDM() string { + return state.LastDM +} + +func SetLastDM(value string) { + state.LastDM = value +}