structure, state and connecting to discord

This commit is contained in:
Cynthia Foxwell 2023-07-06 16:21:11 -06:00
parent 7152ea59fa
commit c70359d412
7 changed files with 189 additions and 15 deletions

9
events/messageCreate.go Normal file
View file

@ -0,0 +1,9 @@
package events
import "github.com/bwmarrin/discordgo"
func MessageCreate(session *discordgo.Session, msg *discordgo.MessageCreate) {
if (msg.Author.ID == session.State.User.ID) {
return
}
}

13
events/ready.go Normal file
View file

@ -0,0 +1,13 @@
package events
import (
"fmt"
"github.com/bwmarrin/discordgo"
"github.com/fatih/color"
)
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)
}

5
go.mod
View file

@ -4,7 +4,10 @@ go 1.20
require (
github.com/bwmarrin/discordgo v0.27.1 // 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
github.com/mattn/go-isatty v0.0.17 // indirect
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b // indirect
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 // indirect
golang.org/x/sys v0.6.0 // indirect
)

10
go.sum
View file

@ -1,12 +1,22 @@
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/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs=
github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBDUSsw=
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/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=
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-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/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=

53
main.go
View file

@ -1,11 +1,16 @@
package main
import (
"fmt"
"os"
"strings"
"fmt"
"os"
"os/signal"
"strings"
"syscall"
"github.com/bwmarrin/discordgo"
"github.com/Cynosphere/comcord/events"
"github.com/Cynosphere/comcord/rcfile"
"github.com/Cynosphere/comcord/state"
"github.com/bwmarrin/discordgo"
)
func main() {
@ -16,12 +21,12 @@ func main() {
if homeErr != nil {
panic(homeErr)
}
RCPATH := GetRCPath()
RCPATH := rcfile.GetPath()
_, rcErr := os.Stat(RCPATH)
if !os.IsNotExist(rcErr) {
fmt.Printf("%% Reading %s ...\n", strings.Replace(RCPATH, homeDir, "~", 1))
config = LoadRCFile()
config = rcfile.Load()
}
if len(os.Args) > 1 {
@ -29,7 +34,7 @@ func main() {
if os.IsNotExist(rcErr) {
fmt.Println("% Writing token to ~/.comcordrc")
config["token"] = token
SaveRCFile(config)
rcfile.Save(config)
}
} else {
configToken, tokenInConfig := config["token"]
@ -38,7 +43,39 @@ func main() {
} else {
fmt.Println("No token provided.")
os.Exit(1)
return
}
}
fmt.Println(token)
state.Setup()
// TODO: user account support
client, err := discordgo.New("Bot " + token)
if err != nil {
fmt.Println("% Failed to create client:", err)
os.Exit(1)
return
}
// TODO: dont set for user accounts(? never really tested if it matters)
client.Identify.Intents = discordgo.IntentsAll
client.AddHandlerOnce(events.Ready)
client.AddHandler(events.MessageCreate)
err = client.Open()
if err != nil {
fmt.Println("% Failed to connect to Discord:", err)
os.Exit(1)
return
}
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
client.Close()
}

View file

@ -1,4 +1,4 @@
package main
package rcfile
import (
"os"
@ -6,7 +6,7 @@ import (
"strings"
)
func GetRCPath() string {
func GetPath() string {
homeDir, err := os.UserHomeDir()
if err != nil {
panic(err)
@ -15,9 +15,9 @@ func GetRCPath() string {
return filepath.Join(homeDir, ".comcordrc")
}
func LoadRCFile() map[string]string {
func Load() map[string]string {
config := make(map[string]string)
file, err := os.ReadFile(GetRCPath())
file, err := os.ReadFile(GetPath())
if err != nil {
panic(err)
}
@ -33,14 +33,14 @@ func LoadRCFile() map[string]string {
return config
}
func SaveRCFile(config map[string]string) {
func Save(config map[string]string) {
out := ""
for key, value := range config {
out = out + key + "=" + value + "\n"
}
err := os.WriteFile(GetRCPath(), []byte(out), 0644)
err := os.WriteFile(GetPath(), []byte(out), 0644)
if err != nil {
panic(err)
}

102
state/main.go Normal file
View file

@ -0,0 +1,102 @@
package state
import (
"time"
"github.com/bwmarrin/discordgo"
)
type ComcordState struct {
Connected bool
RPCConnected bool
StartTime int64
CurrentGuild string
CurrentChannel string
NameLength int32
InPrompt bool
AFK bool
MessageQueue []discordgo.Message
LastChannel map[string]string
}
var state ComcordState
func Setup() {
state = ComcordState{}
state.Connected = true
state.RPCConnected = false
state.StartTime = time.Now().Unix()
state.NameLength = 2
state.InPrompt = false
state.AFK = false
state.MessageQueue = make([]discordgo.Message, 0)
state.LastChannel = make(map[string]string)
}
func IsConnected() bool {
return state.Connected
}
func SetConnected(value bool) {
state.Connected = value
}
func IsRPCConnected() bool {
return state.RPCConnected
}
func SetRPCConnected(value bool) {
state.RPCConnected = value
}
func GetStartTime() int64 {
return state.StartTime
}
func GetCurrentGuild() string {
return state.CurrentGuild
}
func SetCurrentGuild(value string) {
state.CurrentGuild = value
}
func GetCurrentChannel() string {
return state.CurrentChannel
}
func SetCurrentChannel(value string) {
state.CurrentChannel = value
}
func GetNameLength() int32 {
return state.NameLength
}
func SetNameLength(value int32) {
state.NameLength = value
}
func IsInPrompt() bool {
return state.InPrompt
}
func SetInPrompt(value bool) {
state.InPrompt = value
}
func IsAFK() bool {
return state.AFK
}
func SetAFK(value bool) {
state.AFK = value
}
func GetMessageQueue() []discordgo.Message {
return state.MessageQueue
}
func AddMessageToQueue(msg discordgo.Message) {
state.MessageQueue = append(state.MessageQueue, msg)
}