diff --git a/Dockerfile b/Dockerfile index 892511e..331cd6c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -4,10 +4,10 @@ WORKDIR /corn-util COPY go.mod go.sum ./ RUN go mod download COPY . . -RUN go build -v -trimpath ./ +RUN go build -trimpath ./ -FROM alpine:latest@sha256:eece025e432126ce23f223450a0326fbebde39cdf496a85d8c016293fc851978 AS runner +FROM alpine:latest@sha256:eece025e432126ce23f223450a0326fbebde39cdf496a85d8c016293fc851978 WORKDIR /home/corn-util COPY --from=builder /corn-util/bot ./ diff --git a/commandStore.go b/commandStore.go new file mode 100644 index 0000000..37fc3d5 --- /dev/null +++ b/commandStore.go @@ -0,0 +1,27 @@ +package main + +import "github.com/disgoorg/disgo/discord" + +/* + This file is used to store the commands for the bot + and will be sent to Discord API on startup + + If you came here expecting to see code for the commands, + they are in events/interaction.go +*/ + +var FALSE = false + +func commandsJSON() (commandList []discord.ApplicationCommandCreate) { + commandList = []discord.ApplicationCommandCreate{ + discord.SlashCommandCreate{ + Name: "host-stats", + Description: "Returns host statistics like OS, CPU Usage, etc", + }, + discord.SlashCommandCreate{ + Name: "invite", + Description: "Add the bot to your community server", + }, + } + return commandList +} diff --git a/docker-compose.yml b/docker-compose.yml index c3f3269..a8619bd 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -4,7 +4,7 @@ services: container_name: Corn-Utility image: 'git.toast-server.net/toast/corn-utility:main' volumes: - - ../tokens.json:/home/corn-util/tokens.json:ro + - ./tokens.json:/home/corn-util/tokens.json:ro expose: - 23242:23242 restart: unless-stopped diff --git a/events/interaction.go b/events/interaction.go new file mode 100644 index 0000000..4fad155 --- /dev/null +++ b/events/interaction.go @@ -0,0 +1,141 @@ +package botEvents + +import ( + "corn-util/bot/toolbox" + "fmt" + "runtime" + "strings" + "time" + + "github.com/disgoorg/disgo" + "github.com/disgoorg/disgo/discord" + "github.com/disgoorg/disgo/events" + "github.com/disgoorg/log" + "github.com/dustin/go-humanize" + "github.com/mackerelio/go-osstat/cpu" + "github.com/shirou/gopsutil/v3/mem" + "github.com/zcalusic/sysinfo" +) + +var ( + mainEmbedColor = 0xff69b4 //0xf9c62c - main embed color for the bot (saffron / yellow) + noPermText = "You need to have a role with `Manage Server` permission to use this command." +) + +func ListenForCommand(e *events.ApplicationCommandInteractionCreate) { + TRUE := true + switch name := e.Data.CommandName(); name { + case "host-stats": + before, err := cpu.Get() + if err != nil { + DumpErrToConsole(err) + return + } + time.Sleep(time.Duration(1) * time.Second) + after, err := cpu.Get() + if err != nil { + DumpErrToConsole(err) + return + } + total := float64(after.Total - before.Total) + + memInfo, _ := mem.VirtualMemory() + var si sysinfo.SysInfo + si.GetSysInfo() + if err := e.CreateMessage(discord.MessageCreate{ + Embeds: []discord.Embed{ + { + Description: fmt.Sprintf("**OS:** %s\n**CPU:** %s", si.OS.Name, si.CPU.Model), + Fields: []discord.EmbedField{ + { + Name: "CPU Usage", + Value: fmt.Sprintf("User: %.1f %%\nSys: %.1f %%", float64(after.User-before.User)/total*100, float64(after.System-before.System)/total*100), + Inline: &TRUE, + }, + { + Name: "Memory", + Value: fmt.Sprintf("Used: %v\nTotal: %v", humanize.IBytes(memInfo.Used), humanize.IBytes(memInfo.Total)), + Inline: &TRUE, + }, + { + Name: "\u200b", + Value: "\u200b", + Inline: &TRUE, + }, + { + Name: "Version", + Value: fmt.Sprintf("Disgo %v\nGo %v", disgo.Version, strings.TrimPrefix(runtime.Version(), "go")), + Inline: &TRUE, + }, + { + Name: "Goroutines", + Value: fmt.Sprintf("%v", runtime.NumGoroutine()), + Inline: &TRUE, + }, + }, + Footer: &discord.EmbedFooter{ + Text: fmt.Sprintf("Uptime: %v", toolbox.GetUptime()), + }, + Color: mainEmbedColor, + }, + }, + }); err != nil { + DumpErrToConsole(err) + } + break + case "invite": + e.CreateMessage(discord.MessageCreate{ + Components: []discord.ContainerComponent{ + discord.ActionRowComponent{ + discord.ButtonComponent{ + Label: "Invite me!", + Style: discord.ButtonStyleLink, + URL: fmt.Sprintf("https://discord.com/api/oauth2/authorize?client_id=%v&permissions=19456&scope=bot", e.Client().ApplicationID()), + }, + }, + }, + }) + break + } +} + +func DumpErrToConsole(err error) { + log.Errorf("failed to send interaction response: %v", err.Error()) +} + +func DumpErrToInteraction(e *events.ApplicationCommandInteractionCreate, err error) { + if err := e.CreateMessage(discord.MessageCreate{ + Embeds: []discord.Embed{ + { + Title: "There was an attempt...", + Description: fmt.Sprintf("```%v```", err.Error()), + Color: 0x560000, + }, + }, + }); err != nil { + DumpErrToConsole(err) + } +} + +func DumpErrToChannel(e *events.ApplicationCommandInteractionCreate, err error) { + if _, err := e.Client().Rest().CreateMessage(e.Channel().ID(), discord.MessageCreate{ + Embeds: []discord.Embed{ + { + Title: "There was an attempt...", + Description: fmt.Sprintf("```%v```", err.Error()), + Color: 0x560000, + }, + }, + }); err != nil { + DumpErrToConsole(err) + } +} + +func isGuildManager(e *events.ApplicationCommandInteractionCreate) bool { + member := e.Member() + if member.Permissions.Has(discord.PermissionManageGuild) { + return true + } else { + return false + } +} diff --git a/go.mod b/go.mod index 0be3905..9bcae5a 100644 --- a/go.mod +++ b/go.mod @@ -10,29 +10,19 @@ require ( github.com/mackerelio/go-osstat v0.2.4 github.com/shirou/gopsutil/v3 v3.23.9 github.com/zcalusic/sysinfo v1.0.1 - go.mongodb.org/mongo-driver v1.12.1 ) require ( github.com/disgoorg/json v1.1.0 // indirect github.com/go-ole/go-ole v1.3.0 // indirect - github.com/golang/snappy v0.0.4 // indirect github.com/gorilla/websocket v1.5.0 // indirect - github.com/klauspost/compress v1.17.1 // indirect github.com/lufia/plan9stats v0.0.0-20231016141302-07b5767bb0ed // indirect - github.com/montanaflynn/stats v0.7.1 // indirect github.com/power-devops/perfstat v0.0.0-20221212215047-62379fc7944b // indirect github.com/sasha-s/go-csync v0.0.0-20210812194225-61421b77c44b // indirect github.com/tklauser/go-sysconf v0.3.12 // indirect github.com/tklauser/numcpus v0.6.1 // indirect - github.com/xdg-go/pbkdf2 v1.0.0 // indirect - github.com/xdg-go/scram v1.1.2 // indirect - github.com/xdg-go/stringprep v1.0.4 // indirect - github.com/youmark/pkcs8 v0.0.0-20201027041543-1326539a0a0a // indirect github.com/yusufpapurcu/wmi v1.2.3 // indirect golang.org/x/crypto v0.14.0 // indirect golang.org/x/exp v0.0.0-20231006140011-7918f672742d // indirect - golang.org/x/sync v0.4.0 // indirect golang.org/x/sys v0.13.0 // indirect - golang.org/x/text v0.13.0 // indirect ) diff --git a/go.sum b/go.sum index b293e98..e2e7308 100644 --- a/go.sum +++ b/go.sum @@ -14,27 +14,17 @@ github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+m github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0= github.com/go-ole/go-ole v1.3.0 h1:Dt6ye7+vXGIKZ7Xtk4s6/xVdGDQynvom7xCFEdWr6uE= github.com/go-ole/go-ole v1.3.0/go.mod h1:5LS6F96DhAwUc7C+1HLexzMXY1xGRSryjyPPKW6zv78= -github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= -github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM= -github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= -github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc= github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= -github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk= -github.com/klauspost/compress v1.17.1 h1:NE3C767s2ak2bweCZo3+rdP4U/HoyVXLv/X9f2gPS5g= -github.com/klauspost/compress v1.17.1/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE= github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0/go.mod h1:zJYVVT2jmtg6P3p1VtQj7WsuWi/y4VnjVBn7F8KPB3I= github.com/lufia/plan9stats v0.0.0-20231016141302-07b5767bb0ed h1:036IscGBfJsFIgJQzlui7nK1Ncm0tp2ktmPj8xO4N/0= github.com/lufia/plan9stats v0.0.0-20231016141302-07b5767bb0ed/go.mod h1:ilwx/Dta8jXAgpFYFvSWEMwxmbWXyiUHkd5FwyKhb5k= github.com/mackerelio/go-osstat v0.2.4 h1:qxGbdPkFo65PXOb/F/nhDKpF2nGmGaCFDLXoZjJTtUs= github.com/mackerelio/go-osstat v0.2.4/go.mod h1:Zy+qzGdZs3A9cuIqmgbJvwbmLQH9dJvtio5ZjJTbdlQ= -github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe/go.mod h1:wL8QJuTMNUDYhXwkmfOly8iTdp5TEcJFWZD2D7SIkUc= -github.com/montanaflynn/stats v0.7.1 h1:etflOAAHORrCC44V+aR6Ftzort912ZU+YLiSTuV8eaE= -github.com/montanaflynn/stats v0.7.1/go.mod h1:etXPPgVO6n31NxCd9KQUMvCM+ve0ruNzt6R8Bnaayow= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c/go.mod h1:OmDBASR4679mdNQnz2pUhc2G8CO2JrUAVFDRBDP/hJE= @@ -57,69 +47,22 @@ github.com/tklauser/go-sysconf v0.3.12 h1:0QaGUFOdQaIVdPgfITYzaTegZvdCjmYO52cSFA github.com/tklauser/go-sysconf v0.3.12/go.mod h1:Ho14jnntGE1fpdOqQEEaiKRpvIavV0hSfmBq8nJbHYI= github.com/tklauser/numcpus v0.6.1 h1:ng9scYS7az0Bk4OZLvrNXNSAO2Pxr1XXRAPyjhIx+Fk= github.com/tklauser/numcpus v0.6.1/go.mod h1:1XfjsgE2zo8GVw7POkMbHENHzVg3GzmoZ9fESEdAacY= -github.com/xdg-go/pbkdf2 v1.0.0 h1:Su7DPu48wXMwC3bs7MCNG+z4FhcyEuz5dlvchbq0B0c= -github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI= -github.com/xdg-go/scram v1.1.2 h1:FHX5I5B4i4hKRVRBCFRxq1iQRej7WO3hhBuJf+UUySY= -github.com/xdg-go/scram v1.1.2/go.mod h1:RT/sEzTbU5y00aCK8UOx6R7YryM0iF1N2MOmC3kKLN4= -github.com/xdg-go/stringprep v1.0.4 h1:XLI/Ng3O1Atzq0oBs3TWm+5ZVgkq2aqdlvP9JtoZ6c8= -github.com/xdg-go/stringprep v1.0.4/go.mod h1:mPGuuIYwz7CmR2bT9j4GbQqutWS1zV24gijq1dTyGkM= -github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d/go.mod h1:rHwXgn7JulP+udvsHwJoVG1YGAP6VLg4y9I5dyZdqmA= -github.com/youmark/pkcs8 v0.0.0-20201027041543-1326539a0a0a h1:fZHgsYlfvtyqToslyjUt3VOPF4J7aK/3MPcK7xp3PDk= -github.com/youmark/pkcs8 v0.0.0-20201027041543-1326539a0a0a/go.mod h1:ul22v+Nro/R083muKhosV54bj5niojjWZvU8xrevuH4= -github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= github.com/yusufpapurcu/wmi v1.2.3 h1:E1ctvB7uKFMOJw3fdOW32DwGE9I7t++CRUEMKvFoFiw= github.com/yusufpapurcu/wmi v1.2.3/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0= github.com/zcalusic/sysinfo v1.0.1 h1:cVh8q3codjh43AGRTa54dJ2Zq+qPejv8n2VWpxKViwc= github.com/zcalusic/sysinfo v1.0.1/go.mod h1:LxwKwtQdbTIQc65drhjQzYzt0o7jfB80LrrZm7SWn8o= -go.mongodb.org/mongo-driver v1.12.1 h1:nLkghSU8fQNaK7oUmDhQFsnrtcoNy7Z6LVFKsEecqgE= -go.mongodb.org/mongo-driver v1.12.1/go.mod h1:/rGBTebI3XYboVmgz+Wv3Bcbl3aD0QF9zl6kDDw18rQ= -golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= -golang.org/x/crypto v0.0.0-20200302210943-78000ba7a073/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= -golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= -golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc= golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4= golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM8rJBtfilJ2qTU199MI= golang.org/x/exp v0.0.0-20231006140011-7918f672742d/go.mod h1:ldy0pHrwJyGW56pPQzzkH36rKxoZW1tw7ZJpeKx+hdo= -golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= -golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= -golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= -golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= -golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= -golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= -golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.4.0 h1:zxkM55ReGkDlKSM+Fu41A+zmbZuaPVbGMzvvdUPznYQ= -golang.org/x/sync v0.4.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y= -golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201204225414-ed752295db88/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210423082822-04245dca01da/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-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE= golang.org/x/sys v0.13.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-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= -golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= -golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ= -golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= -golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k= -golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= -golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= -golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= -golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= -golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/loaders/json.go b/loaders/json.go new file mode 100644 index 0000000..47e8ae7 --- /dev/null +++ b/loaders/json.go @@ -0,0 +1,61 @@ +package loaders + +import ( + "encoding/json" + "fmt" + "os" +) + +type Tokens struct { + DeployCommands bool `json:"deployCommands"` + Bot string `json:"bot"` + HookID string `json:"hookId"` + HookToken string `json:"hookToken"` + BotPublicKey string `json:"botPublicKey"` +} + +var ( + tokens Tokens + invalidToken = "Token not found; Please setup a \"tokens.json\" file in root directory." +) + +func LoadJSON(path string, v interface{}) error { + var file, err = os.Open(path) + if err != nil { + fmt.Println(err.Error()) + } + defer file.Close() + + decoder := json.NewDecoder(file) + err = decoder.Decode(&v) + if err != nil { + fmt.Println(err.Error()) + } + return err +} + +func TokenLoader(token string) string { + err := LoadJSON("tokens.json", &tokens) + if err != nil { + return invalidToken + } + switch token { + case "bot": + return tokens.Bot + case "hookId": + return tokens.HookID + case "hookToken": + return tokens.HookToken + case "botPublicKey": + return tokens.BotPublicKey + } + return invalidToken +} + +func IsCmdsDeployable() bool { + err := LoadJSON("tokens.json", &tokens) + if err != nil { + return false + } + return tokens.DeployCommands +} diff --git a/main.go b/main.go new file mode 100644 index 0000000..0035f80 --- /dev/null +++ b/main.go @@ -0,0 +1,56 @@ +package main + +import ( + botEvents "corn-util/bot/events" + "corn-util/bot/loaders" + "fmt" + "os" + "os/signal" + "runtime" + "strings" + "syscall" + + "github.com/disgoorg/disgo" + "github.com/disgoorg/disgo/bot" + "github.com/disgoorg/disgo/discord" + "github.com/disgoorg/disgo/events" + "github.com/disgoorg/disgo/httpserver" + "github.com/disgoorg/log" + "github.com/disgoorg/snowflake/v2" +) + +func main() { + client, err := disgo.New(loaders.TokenLoader("bot"), + bot.WithHTTPServerConfigOpts(loaders.TokenLoader("botPublicKey"), + httpserver.WithAddress(":23242"), + httpserver.WithURL("/"), + ), + bot.WithEventListeners(&events.ListenerAdapter{OnApplicationCommandInteraction: botEvents.ListenForCommand}), + ) + if err != nil { + panic(err) + } + + if err = client.OpenHTTPServer(); err != nil { + panic(err) + } + + if loaders.IsCmdsDeployable() { + log.Infof("Commands deployment is enabled, deploying...") + if _, err := client.Rest().SetGlobalCommands(client.ApplicationID(), commandsJSON()); err != nil { + log.Errorf("failed to set global commands: %v", err) + } + } else { + log.Infof("Commands deployment is disabled") + } + + fmt.Printf("Client ready!\n") + fmt.Printf("Running Disgo %v & Go %v\n", disgo.Version, strings.TrimPrefix(runtime.Version(), "go")) + client.Rest().CreateWebhookMessage(snowflake.MustParse(loaders.TokenLoader("hookId")), loaders.TokenLoader("hookToken"), discord.WebhookMessageCreate{ + Content: "Container has been reloaded.", + }, true, 0) + + s := make(chan os.Signal, 1) + signal.Notify(s, syscall.SIGINT, syscall.SIGTERM, os.Interrupt) + <-s +} diff --git a/toolbox/guildManager.go b/toolbox/guildManager.go new file mode 100644 index 0000000..ae08e32 --- /dev/null +++ b/toolbox/guildManager.go @@ -0,0 +1,11 @@ +package toolbox + +import ( + "github.com/disgoorg/disgo/rest" + "github.com/disgoorg/snowflake/v2" +) + +func RESTGuild_Name(ID snowflake.ID, BotToken string) string { + guild, _ := rest.Guilds.GetGuild(rest.NewGuilds(rest.NewClient(BotToken)), ID, false) + return guild.Name +} diff --git a/toolbox/system.go b/toolbox/system.go new file mode 100644 index 0000000..934d585 --- /dev/null +++ b/toolbox/system.go @@ -0,0 +1,10 @@ +package toolbox + +import "time" + +var timerStart = time.Now() + +func GetUptime() string { + uptime := time.Since(timerStart) + return uptime.Round(time.Second).String() +}