2023-06-01 02:16:58 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-07-06 22:21:11 +00:00
|
|
|
"fmt"
|
2023-07-09 01:17:47 +00:00
|
|
|
"os"
|
2023-07-09 20:00:49 +00:00
|
|
|
"runtime"
|
2023-07-06 22:21:11 +00:00
|
|
|
"strings"
|
2023-06-01 02:16:58 +00:00
|
|
|
|
2023-07-08 20:51:26 +00:00
|
|
|
"atomicgo.dev/keyboard"
|
|
|
|
"atomicgo.dev/keyboard/keys"
|
|
|
|
"github.com/Cynosphere/comcord/commands"
|
2023-07-06 22:21:11 +00:00
|
|
|
"github.com/Cynosphere/comcord/events"
|
|
|
|
"github.com/Cynosphere/comcord/rcfile"
|
|
|
|
"github.com/Cynosphere/comcord/state"
|
|
|
|
"github.com/bwmarrin/discordgo"
|
2023-07-09 01:17:47 +00:00
|
|
|
"golang.org/x/term"
|
2023-06-01 02:16:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2023-07-09 01:17:47 +00:00
|
|
|
oldState, err := term.MakeRaw(int(os.Stdin.Fd()))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer term.Restore(int(os.Stdin.Fd()), oldState)
|
|
|
|
|
2023-06-01 02:16:58 +00:00
|
|
|
var config map[string]string = make(map[string]string)
|
|
|
|
var token string
|
|
|
|
|
|
|
|
homeDir, homeErr := os.UserHomeDir()
|
|
|
|
if homeErr != nil {
|
|
|
|
panic(homeErr)
|
|
|
|
}
|
2023-07-06 22:21:11 +00:00
|
|
|
RCPATH := rcfile.GetPath()
|
2023-06-01 02:16:58 +00:00
|
|
|
|
|
|
|
_, rcErr := os.Stat(RCPATH)
|
|
|
|
if !os.IsNotExist(rcErr) {
|
|
|
|
fmt.Printf("%% Reading %s ...\n", strings.Replace(RCPATH, homeDir, "~", 1))
|
2023-07-06 22:21:11 +00:00
|
|
|
config = rcfile.Load()
|
2023-06-01 02:16:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(os.Args) > 1 {
|
|
|
|
token = os.Args[1]
|
|
|
|
if os.IsNotExist(rcErr) {
|
|
|
|
fmt.Println("% Writing token to ~/.comcordrc")
|
|
|
|
config["token"] = token
|
2023-07-06 22:21:11 +00:00
|
|
|
rcfile.Save(config)
|
2023-06-01 02:16:58 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
configToken, tokenInConfig := config["token"]
|
|
|
|
if tokenInConfig {
|
|
|
|
token = configToken
|
|
|
|
} else {
|
|
|
|
fmt.Println("No token provided.")
|
|
|
|
os.Exit(1)
|
2023-07-06 22:21:11 +00:00
|
|
|
return
|
2023-06-01 02:16:58 +00:00
|
|
|
}
|
|
|
|
}
|
2023-07-06 22:21:11 +00:00
|
|
|
|
2023-07-09 01:17:47 +00:00
|
|
|
fmt.Println("\rCOMcord (c)left 2023")
|
|
|
|
fmt.Println("\rType 'h' for Commands")
|
|
|
|
fmt.Print("\r")
|
|
|
|
|
|
|
|
state.Setup(config)
|
2023-07-08 20:51:26 +00:00
|
|
|
commands.Setup()
|
2023-07-06 22:21:11 +00:00
|
|
|
|
2023-07-17 02:36:46 +00:00
|
|
|
allowUserAccounts := config["allowUserAccounts"] == "true"
|
|
|
|
tokenPrefix := "Bot "
|
|
|
|
if allowUserAccounts {
|
|
|
|
tokenPrefix = ""
|
|
|
|
}
|
|
|
|
|
|
|
|
fullToken := tokenPrefix + token
|
|
|
|
|
|
|
|
client, err := discordgo.New(fullToken)
|
2023-07-06 22:21:11 +00:00
|
|
|
if err != nil {
|
2023-07-10 19:04:40 +00:00
|
|
|
fmt.Println("% Failed to create client:", err)
|
|
|
|
fmt.Print("\r")
|
2023-07-06 22:21:11 +00:00
|
|
|
os.Exit(1)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-07-26 22:51:25 +00:00
|
|
|
//client.LogLevel = -1
|
|
|
|
client.LogLevel = discordgo.LogDebug
|
2023-07-17 02:36:46 +00:00
|
|
|
|
2023-07-06 22:21:11 +00:00
|
|
|
client.Identify.Intents = discordgo.IntentsAll
|
|
|
|
|
2023-07-17 04:27:50 +00:00
|
|
|
client.Identify.Properties = discordgo.IdentifyProperties{
|
|
|
|
OS: runtime.GOOS,
|
|
|
|
}
|
|
|
|
statusType := config["statusType"]
|
|
|
|
if statusType == "mobile" {
|
|
|
|
client.Identify.Properties.Browser = "Discord Android"
|
|
|
|
} else if statusType == "embedded" {
|
|
|
|
client.Identify.Properties.Browser = "Discord Embedded"
|
|
|
|
} else if statusType == "desktop" {
|
|
|
|
client.Identify.Properties.Browser = "Discord Client"
|
2023-07-09 20:00:49 +00:00
|
|
|
} else {
|
2023-07-17 04:27:50 +00:00
|
|
|
client.Identify.Properties.Browser = "comcord"
|
2023-07-09 20:00:49 +00:00
|
|
|
}
|
|
|
|
|
2023-07-17 00:05:04 +00:00
|
|
|
status := "online"
|
|
|
|
defaultStatus := config["defaultStatus"]
|
|
|
|
if defaultStatus != "" {
|
|
|
|
status = defaultStatus
|
|
|
|
}
|
|
|
|
startTime := state.GetStartTime()
|
2023-07-17 02:36:46 +00:00
|
|
|
|
2023-07-17 00:05:04 +00:00
|
|
|
client.Identify.Presence = discordgo.GatewayStatusUpdate{
|
|
|
|
Since: 0,
|
|
|
|
Status: status,
|
|
|
|
AFK: false,
|
|
|
|
Game: discordgo.Activity{
|
2023-07-17 02:36:46 +00:00
|
|
|
Type: 0,
|
2023-07-17 00:05:04 +00:00
|
|
|
Name: "comcord",
|
|
|
|
ApplicationID: "1026163285877325874",
|
|
|
|
CreatedAt: startTime,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-07-09 20:00:49 +00:00
|
|
|
events.Setup(client)
|
2023-07-06 22:21:11 +00:00
|
|
|
|
|
|
|
err = client.Open()
|
|
|
|
if err != nil {
|
2023-07-10 19:04:40 +00:00
|
|
|
fmt.Println("% Failed to connect to Discord:", err)
|
|
|
|
fmt.Print("\r")
|
2023-07-06 22:21:11 +00:00
|
|
|
os.Exit(1)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-07-08 20:51:26 +00:00
|
|
|
keyboard.Listen(func(key keys.Key) (stop bool, err error) {
|
|
|
|
if !state.IsInPrompt() {
|
|
|
|
if key.Code == keys.CtrlC {
|
2023-07-10 19:04:40 +00:00
|
|
|
commands.QuitCommand(client)
|
2023-07-08 20:51:26 +00:00
|
|
|
return true, nil
|
|
|
|
} else {
|
|
|
|
command, has := commands.GetCommand(key.String())
|
|
|
|
if has {
|
|
|
|
command.Run(client)
|
|
|
|
} else {
|
2023-07-09 01:17:47 +00:00
|
|
|
commands.SendMode(client)
|
2023-07-08 20:51:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, nil
|
|
|
|
})
|
2023-07-09 01:17:47 +00:00
|
|
|
|
|
|
|
/*sc := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
|
|
|
|
<-sc
|
|
|
|
|
|
|
|
client.Close()*/
|
2023-06-01 02:16:58 +00:00
|
|
|
}
|