Skip to content

Commit

Permalink
Merge branch 'master' into threads
Browse files Browse the repository at this point in the history
  • Loading branch information
FedorLap2006 committed Feb 17, 2022
2 parents 9b24b3d + 6015eed commit fd22e06
Show file tree
Hide file tree
Showing 16 changed files with 893 additions and 149 deletions.
60 changes: 60 additions & 0 deletions .github/workflows/ci.yml
@@ -0,0 +1,60 @@
on:
push:
pull_request:

name: CI

jobs:
format:
runs-on: ubuntu-latest
steps:
- name: Install Go
uses: actions/setup-go@v2
with:
go-version: 1.17
- name: Code
uses: actions/checkout@v2
- name: Check diff between gofmt and code
run: diff <(gofmt -d .) <(echo -n)

test:
runs-on: ubuntu-latest
strategy:
matrix:
go-version: [1.13, 1.14, 1.15, 1.16, 1.17]
steps:
- name: Install Go
uses: actions/setup-go@v2
with:
go-version: ${{ matrix.go-version }}
- name: Code
uses: actions/checkout@v2
- run: go test -v -race ./...

lint:
runs-on: ubuntu-latest
steps:
- name: Install Go
uses: actions/setup-go@v2
with:
go-version: 1.17
- name: Code
uses: actions/checkout@v2
- name: Go vet
run: go vet -x ./...

- name: GolangCI-Lint
uses: golangci/golangci-lint-action@v2.5.2
if: github.event.name == 'pull_request'
with:
only-new-issues: true
skip-pkg-cache: true
skip-build-cache: true

- name: GolangCI-Lint
if: github.event.name != 'pull_request' # See https://github.com/golangci/golangci-lint-action/issues/362
run: |
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.43.0
$(go env GOPATH)/bin/golangci-lint run
19 changes: 19 additions & 0 deletions .golangci.yml
@@ -0,0 +1,19 @@
linters:
disable-all: true
enable:
# - staticcheck
# - unused
- golint

linters-settings:
staticcheck:
go: "1.13"

checks: ["all"]

unused:
go: "1.13"

issues:
include:
- EXC0002
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -2,7 +2,7 @@

[![Go Reference](https://pkg.go.dev/badge/github.com/bwmarrin/discordgo.svg)](https://pkg.go.dev/github.com/bwmarrin/discordgo) [![Go Report Card](https://goreportcard.com/badge/github.com/bwmarrin/discordgo)](https://goreportcard.com/report/github.com/bwmarrin/discordgo) [![Build Status](https://travis-ci.com/bwmarrin/discordgo.svg?branch=master)](https://travis-ci.com/bwmarrin/discordgo) [![Discord Gophers](https://img.shields.io/badge/Discord%20Gophers-%23discordgo-blue.svg)](https://discord.gg/golang) [![Discord API](https://img.shields.io/badge/Discord%20API-%23go_discordgo-blue.svg)](https://discord.com/invite/discord-api)

<img align="right" src="https://github.com/bwmarrin/discordgo/blob/master/docs/img/discordgo.png">
<img align="right" alt="DiscordGo logo" src="docs/img/discordgo.svg" width="400">

DiscordGo is a [Go](https://golang.org/) package that provides low level
bindings to the [Discord](https://discord.com/) chat client API. DiscordGo
Expand Down
43 changes: 43 additions & 0 deletions components.go
Expand Up @@ -13,6 +13,7 @@ const (
ActionsRowComponent ComponentType = 1
ButtonComponent ComponentType = 2
SelectMenuComponent ComponentType = 3
TextInputComponent ComponentType = 4
)

// MessageComponent is a base interface for all message components.
Expand Down Expand Up @@ -42,12 +43,15 @@ func (umc *unmarshalableMessageComponent) UnmarshalJSON(src []byte) error {
umc.MessageComponent = &Button{}
case SelectMenuComponent:
umc.MessageComponent = &SelectMenu{}
case TextInputComponent:
umc.MessageComponent = &TextInput{}
default:
return fmt.Errorf("unknown component type: %d", v.Type)
}
return json.Unmarshal(src, umc.MessageComponent)
}

// MessageComponentFromJSON is a helper function for unmarshaling message components
func MessageComponentFromJSON(b []byte) (MessageComponent, error) {
var u unmarshalableMessageComponent
err := u.UnmarshalJSON(b)
Expand Down Expand Up @@ -195,3 +199,42 @@ func (m SelectMenu) MarshalJSON() ([]byte, error) {
Type: m.Type(),
})
}

// TextInput represents text input component.
type TextInput struct {
CustomID string `json:"custom_id"`
Label string `json:"label"`
Style TextInputStyle `json:"style"`
Placeholder string `json:"placeholder,omitempty"`
Value string `json:"value,omitempty"`
Required bool `json:"required,omitempty"`
MinLength int `json:"min_length,omitempty"`
MaxLength int `json:"max_length,omitempty"`
}

// Type is a method to get the type of a component.
func (TextInput) Type() ComponentType {
return TextInputComponent
}

// MarshalJSON is a method for marshaling TextInput to a JSON object.
func (m TextInput) MarshalJSON() ([]byte, error) {
type inputText TextInput

return json.Marshal(struct {
inputText
Type ComponentType `json:"type"`
}{
inputText: inputText(m),
Type: m.Type(),
})
}

// TextInputStyle is style of text in TextInput component.
type TextInputStyle uint

// Text styles
const (
TextInputShort TextInputStyle = 1
TextInputParagraph TextInputStyle = 2
)
34 changes: 20 additions & 14 deletions discord_test.go
Expand Up @@ -15,26 +15,32 @@ var (
dg *Session // Stores a global discordgo user session
dgBot *Session // Stores a global discordgo bot session

envToken = os.Getenv("DGU_TOKEN") // Token to use when authenticating the user account
envBotToken = os.Getenv("DGB_TOKEN") // Token to use when authenticating the bot account
envGuild = os.Getenv("DG_GUILD") // Guild ID to use for tests
envChannel = os.Getenv("DG_CHANNEL") // Channel ID to use for tests
envAdmin = os.Getenv("DG_ADMIN") // User ID of admin user to use for tests
envOAuth2Token = os.Getenv("DG_OAUTH2_TOKEN") // Token to use when authenticating using OAuth2 token
envBotToken = os.Getenv("DGB_TOKEN") // Token to use when authenticating the bot account
envGuild = os.Getenv("DG_GUILD") // Guild ID to use for tests
envChannel = os.Getenv("DG_CHANNEL") // Channel ID to use for tests
envAdmin = os.Getenv("DG_ADMIN") // User ID of admin user to use for tests
)

func init() {
func TestMain(m *testing.M) {
fmt.Println("Init is being called.")
if envBotToken != "" {
if d, err := New(envBotToken); err == nil {
dgBot = d
}
}

if d, err := New(envToken); err == nil {
dg = d
} else {
fmt.Println("dg is nil, error", err)
if envOAuth2Token == "" {
envOAuth2Token = os.Getenv("DGU_TOKEN")
}

if envOAuth2Token != "" {
if d, err := New(envOAuth2Token); err == nil {
dg = d
}
}

os.Exit(m.Run())
}

//////////////////////////////////////////////////////////////////////////////
Expand All @@ -43,11 +49,11 @@ func init() {
// TestNewToken tests the New() function with a Token.
func TestNewToken(t *testing.T) {

if envToken == "" {
if envOAuth2Token == "" {
t.Skip("Skipping New(token), DGU_TOKEN not set")
}

d, err := New(envToken)
d, err := New(envOAuth2Token)
if err != nil {
t.Fatalf("New(envToken) returned error: %+v", err)
}
Expand All @@ -62,11 +68,11 @@ func TestNewToken(t *testing.T) {
}

func TestOpenClose(t *testing.T) {
if envToken == "" {
if envOAuth2Token == "" {
t.Skip("Skipping TestClose, DGU_TOKEN not set")
}

d, err := New(envToken)
d, err := New(envOAuth2Token)
if err != nil {
t.Fatalf("TestClose, New(envToken) returned error: %+v", err)
}
Expand Down
Binary file removed docs/img/discordgo.png
Binary file not shown.
45 changes: 45 additions & 0 deletions docs/img/discordgo.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions endpoints.go
Expand Up @@ -108,6 +108,7 @@ var (
EndpointThreadMembers = func(tID string) string { return EndpointChannel(tID) + "/thread-members" }
EndpointThreadMember = func(tID, mID string) string { return EndpointThreadMembers(tID) + "/" + mID }


EndpointGroupIcon = func(cID, hash string) string { return EndpointCDNChannelIcons + cID + "/" + hash + ".png" }

EndpointSticker = func(sID string) string { return EndpointStickers + sID }
Expand Down Expand Up @@ -143,6 +144,12 @@ var (
EndpointApplicationGuildCommand = func(aID, gID, cID string) string {
return EndpointApplicationGuildCommands(aID, gID) + "/" + cID
}
EndpointApplicationCommandPermissions = func(aID, gID, cID string) string {
return EndpointApplicationGuildCommand(aID, gID, cID) + "/permissions"
}
EndpointApplicationCommandsGuildPermissions = func(aID, gID string) string {
return EndpointApplicationGuildCommands(aID, gID) + "/permissions"
}
EndpointInteraction = func(aID, iToken string) string {
return EndpointAPI + "interactions/" + aID + "/" + iToken
}
Expand Down
4 changes: 2 additions & 2 deletions examples/autocomplete/main.go
Expand Up @@ -239,8 +239,8 @@ func main() {
log.Fatalf("Cannot register commands: %v", err)
}

stop := make(chan os.Signal)
signal.Notify(stop, os.Interrupt) //nolint: staticcheck
stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt)
<-stop
log.Println("Gracefully shutting down")

Expand Down
2 changes: 1 addition & 1 deletion examples/avatar/main.go
Expand Up @@ -82,7 +82,7 @@ func main() {
// Now lets format our base64 image into the proper format Discord wants
// and then call UserUpdate to set it as our user's Avatar.
avatar := fmt.Sprintf("data:%s;base64,%s", contentType, base64img)
_, err = dg.UserUpdate("", "", "", avatar, "")
_, err = dg.UserUpdate("", avatar)
if err != nil {
fmt.Println(err)
}
Expand Down

0 comments on commit fd22e06

Please sign in to comment.