-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
better option handling, new
f
builders
- Loading branch information
Karitham
committed
Dec 25, 2021
1 parent
43126b8
commit 1140fe9
Showing
9 changed files
with
170 additions
and
35 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 |
---|---|---|
@@ -1,17 +1,45 @@ | ||
# corde | ||
|
||
corde is a discord webhook callback API wrapper. | ||
It's nowhere near prod-ready, but it's honestly usable, and it's open to contributions. | ||
[](https://goreportcard.com/report/github.com/Karitham/corde) | ||
[](https://pkg.go.dev/github.com/Karitham/corde) | ||
|
||
--- | ||
|
||
corde is a discord webhook callback API wrapper. It aims to have an high level API to simplify the user's life as much as possible. | ||
|
||
It contains many functions to help write as clean and concise code as possible, such as `f` suffixed functions which act like `Sprintf`. | ||
|
||
It also has builders of some kinds, such as embed and response builders, which make error handling and responding a breeze. | ||
|
||
All those don't hinder the fact that you can use corde with as much control as you want. | ||
|
||
Be aware that breaking changes will happen as of now, at least until v1. | ||
|
||
## Examples | ||
|
||
The most basic one is [**bongo**](_example/bongo/main.go) | ||
|
||
Then comes [**todo**](_example/todo/) that shows off subcommands routing and options | ||
|
||
And then we have [**moderate-myself**](_example/moderate-myself/main.go) which is able to show and delete currently available commands. | ||
It demonstrates the usage of components such as buttons. | ||
|
||
Corde is also actively used to rewrite and develop another discord bot of mine called [**WaifuBot**](https://github.com/Karitham/WaifuBot/) (for now see the corde branch) | ||
|
||
## Install | ||
|
||
```sh | ||
go get github.com/Karitham/corde | ||
``` | ||
|
||
## Why | ||
|
||
Having used most go discord libs available, I wanted something very lightweight, that could just run on a lambda. | ||
The common libs out there didn't really fit that purpose, and neither did they implement the webhook callback API available. | ||
Having used most go discord libs available, I wanted something very lightweight yet high-level, that could just run on a lambda without state, caching or long start-up times. | ||
|
||
Corde has a single dependency as of yet, and it's just a codegen utility. | ||
|
||
It's from that usecase that Corde is born. | ||
The common libs out there didn't really fit that purpose, and neither did they implement the webhook callback API available. | ||
|
||
## Usage | ||
It's from that usecase that corde is born. | ||
|
||
See [todo](_example/todo/) for an example usage. | ||
Corde means *rope* in french, because discord's API inspires exactly that /s |
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
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
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
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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
module github.com/Karitham/corde | ||
|
||
go 1.18 | ||
|
||
require github.com/karitham/go-genial v0.2.0 |
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,2 @@ | ||
github.com/karitham/go-genial v0.2.0 h1:AHb2iBbzOBS8kc3E0QiZR3Z6txdBS9u0IDhGGketskE= | ||
github.com/karitham/go-genial v0.2.0/go.mod h1:HBN0CvV0hsPCMegF7wvmUK3l8OnNzs8XVxt350NI5eE= |
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,56 @@ | ||
// retrieve options from interaction data | ||
package corde | ||
|
||
// String returns the option with key k of type string | ||
func (o OptionsInteractions) String(k string) string { | ||
v, _ := o[k].(string) | ||
return v | ||
} | ||
|
||
// Int returns the option with key k of type int | ||
func (o OptionsInteractions) Int(k string) int { | ||
v, _ := o[k].(int) | ||
return v | ||
} | ||
|
||
// Int64 returns the option with key k of type int64 | ||
func (o OptionsInteractions) Int64(k string) int64 { | ||
v, _ := o[k].(int64) | ||
return v | ||
} | ||
|
||
// Uint returns the option with key k of type uint | ||
func (o OptionsInteractions) Uint(k string) uint { | ||
v, _ := o[k].(uint) | ||
return v | ||
} | ||
|
||
// Uint64 returns the option with key k of type uint64 | ||
func (o OptionsInteractions) Uint64(k string) uint64 { | ||
v, _ := o[k].(uint64) | ||
return v | ||
} | ||
|
||
// Float32 returns the option with key k of type float32 | ||
func (o OptionsInteractions) Float32(k string) float32 { | ||
v, _ := o[k].(float32) | ||
return v | ||
} | ||
|
||
// Float64 returns the option with key k of type float64 | ||
func (o OptionsInteractions) Float64(k string) float64 { | ||
v, _ := o[k].(float64) | ||
return v | ||
} | ||
|
||
// Bool returns the option with key k of type bool | ||
func (o OptionsInteractions) Bool(k string) bool { | ||
v, _ := o[k].(bool) | ||
return v | ||
} | ||
|
||
// Snowflake returns the option with key k of type Snowflake | ||
func (o OptionsInteractions) Snowflake(k string) Snowflake { | ||
v, _ := o[k].(Snowflake) | ||
return v | ||
} |
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
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,59 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
genial "github.com/karitham/go-genial" | ||
) | ||
|
||
var types = []string{ | ||
"string", | ||
"int", | ||
"int64", | ||
"uint", | ||
"uint64", | ||
"float32", | ||
"float64", | ||
"bool", | ||
"Snowflake", | ||
} | ||
|
||
var body string = ` v, _ := o[k].(%s) | ||
return v | ||
` | ||
|
||
var file = flag.String("file", "../../../interaction-opt.gen.go", "output file") | ||
|
||
//go:generate go run . | ||
func main() { | ||
flag.Parse() | ||
|
||
p := &genial.PackageB{} | ||
p.Name("corde").Comment("retrieve options from interaction data") | ||
|
||
for _, t := range types { | ||
f := &genial.FuncB{} | ||
|
||
name := strings.Title(t) | ||
|
||
f.Receiver(genial.Parameter{Name: "o", Type: "OptionsInteractions"}). | ||
Comment(fmt.Sprintf("%s returns the option with key k of type %s", name, t)). | ||
Name(name). | ||
Parameters(genial.Parameter{ | ||
Name: "k", | ||
Type: "string", | ||
}). | ||
ReturnTypes(genial.Parameter{ | ||
Type: t, | ||
}) | ||
|
||
f.Write([]byte(fmt.Sprintf(body, t))) | ||
|
||
p.Declarations(f) | ||
} | ||
|
||
os.WriteFile(*file, []byte(p.String()), 0644) | ||
} |