This repository has been archived by the owner on Aug 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
56b7238
commit aa51214
Showing
4 changed files
with
115 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# Application Command Manager | ||
[![Go Reference](https://pkg.go.dev/badge/github.com/blue0moon02/appcmdmanager.svg)](https://pkg.go.dev/github.com/blue0moon02/appcmdmanager) | ||
![Golang Version](https://img.shields.io/badge/golang-1.18-00ADD8?style=flat-square&logo=go&logoColor=white) | ||
![Discordgo Version](https://img.shields.io/github/v/tag/bwmarrin/discordgo?color=00ADD8&label=discordgo&logo=discord&logoColor=white&style=flat-square) | ||
![License](https://img.shields.io/github/license/blue0moon02/application-command-manager?style=flat-square&logo=github&logoColor=white) | ||
|
||
discordgo application command manager | ||
|
||
## Getting Started | ||
### Installing | ||
``` | ||
go get github.com/blue0moon02/appcmdmanager | ||
``` | ||
|
||
### Usage | ||
```go | ||
import "github.com/blue0moon02/appcmdmanager" | ||
``` | ||
|
||
## Collaborators | ||
| [<p align="center"> <img src="https://avatars.githubusercontent.com/u/58559207?v=4" width="150"/> <p/> BlueMoon2 <br/> (blue0moon02)](https://github.com/blue0moon02) | [<p align="center"> <img src="https://avatars.githubusercontent.com/u/29603389?v=4" width="150"/> <p/> Numberer <br/> (ybs1164)](https://github.com/ybs1164) | | ||
|-|-| |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package appcmdmanager | ||
|
||
import ( | ||
"github.com/bwmarrin/discordgo" | ||
"log" | ||
"sync" | ||
) | ||
|
||
type AppCmdInterface interface { | ||
ApplicationCommandStruct() *discordgo.ApplicationCommand | ||
ApplicationCommandHandler(s *discordgo.Session, i *discordgo.InteractionCreate) | ||
MessageComponentHandler() map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate) | ||
ModalSubmitHandler() map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate) | ||
} | ||
|
||
func RegisterApplicationCommands(cmds []AppCmdInterface, s *discordgo.Session, guildID string) (appCmds []*discordgo.ApplicationCommand) { | ||
var ( | ||
mutex sync.Mutex | ||
wg sync.WaitGroup | ||
) | ||
|
||
for _, command := range cmds { | ||
wg.Add(1) | ||
|
||
go func(cmd AppCmdInterface) { | ||
appCmd, err := s.ApplicationCommandCreate(s.State.User.ID, guildID, cmd.ApplicationCommandStruct()) | ||
if err != nil { | ||
log.Panicf("Cannot create '%v' command: %v", cmd.ApplicationCommandStruct().Name, err) | ||
} | ||
|
||
mutex.Lock() | ||
appCmds = append(appCmds, appCmd) | ||
mutex.Unlock() | ||
|
||
wg.Done() | ||
}(command) | ||
} | ||
wg.Wait() | ||
|
||
return appCmds | ||
} | ||
|
||
func RegisterHandler(apps []AppCmdInterface) func(s *discordgo.Session, i *discordgo.InteractionCreate) { | ||
return func(s *discordgo.Session, i *discordgo.InteractionCreate) { | ||
switch i.Type { | ||
case discordgo.InteractionApplicationCommand: | ||
for _, app := range apps { | ||
if app.ApplicationCommandStruct().Name == i.ApplicationCommandData().Name { | ||
app.ApplicationCommandHandler(s, i) | ||
} | ||
} | ||
|
||
case discordgo.InteractionMessageComponent: | ||
for _, app := range apps { | ||
if handler, ok := app.MessageComponentHandler()[i.MessageComponentData().CustomID]; ok { | ||
handler(s, i) | ||
} | ||
} | ||
|
||
case discordgo.InteractionModalSubmit: | ||
for _, app := range apps { | ||
if handler, ok := app.ModalSubmitHandler()[i.ModalSubmitData().CustomID]; ok { | ||
handler(s, i) | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
module github.com/blue0moon02/appcmdmanager | ||
|
||
go 1.19 | ||
|
||
require github.com/bwmarrin/discordgo v0.26.1 | ||
|
||
require ( | ||
github.com/gorilla/websocket v1.4.2 // indirect | ||
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b // indirect | ||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
github.com/bwmarrin/discordgo v0.26.0 h1:/AdFmxHXSHInYAZ7K0O3VEIXlVjGpztk/nuCr9o+JCs= | ||
github.com/bwmarrin/discordgo v0.26.0/go.mod h1:NJZpH+1AfhIcyQsPeuBKsUtYrRnjkyu0kIVMCHkZtRY= | ||
github.com/bwmarrin/discordgo v0.26.1 h1:AIrM+g3cl+iYBr4yBxCBp9tD9jR3K7upEjl0d89FRkE= | ||
github.com/bwmarrin/discordgo v0.26.1/go.mod h1:NJZpH+1AfhIcyQsPeuBKsUtYrRnjkyu0kIVMCHkZtRY= | ||
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc= | ||
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= | ||
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/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= |