andersfylling / disgord

Go module for interacting with the documented Discord's bot interface; Gateway, REST requests and voice
BSD 3-Clause "New" or "Revised" License
502 stars 71 forks source link

feat: Added application commands (slash commands) #429

Closed Acnologla closed 2 years ago

Acnologla commented 2 years ago

Description

This pull request adds discord application commands structures and functions.

Example:

package main

import (
    "context"
    "fmt"

    "github.com/andersfylling/disgord"
)

//listen to interactions
func interaction(session disgord.Session, evt *disgord.InteractionCreate) {
    if evt.Type == disgord.InteractionApplicationCommand {
        args := evt.Data.Options[0].Value.(string)
        txt := fmt.Sprintf("User %s used ping command with args %s", evt.Member.User.Username, args)
        session.SendInteractionResponse(context.Background(), evt, &disgord.InteractionResponse{
            Type: disgord.ChannelMessageWithSource,
            Data: &disgord.InteractionApplicationCommandCallbackData{
                Content: txt,
            },
        })
    }
}
func main() {
    client := disgord.New(disgord.Config{
        RejectEvents: []string{disgord.EvtPresenceUpdate, disgord.EvtTypingStart},
        BotToken:     "token",
    })
        //register new global command
    client.CreateGlobalCommand(context.Background(), appID, &disgord.ApplicationCommand{
        Name:        "ping2",
        Description: "Ping testing",
        Type:        disgord.ApplicationCommandChatInput,
        Options: []*disgord.ApplicationCommandOption{
            {
                Name:        "content",
                Description: "content",
                Type:        disgord.STRING,
            },
        },
    })
    defer client.Gateway().StayConnectedUntilInterrupted()
    client.Gateway().InteractionCreate(interaction)

}

Breaking Change?

no

Benchmarks

Checklist:

andersfylling commented 2 years ago

PR title does not match the new requirements: https://github.com/andersfylling/disgord/blob/master/CONTRIBUTING.md#pr-title

andersfylling commented 2 years ago

Minor changes in addition to the resource design and update methods. Really nice contribution though, needed this for a long time.

andersfylling commented 2 years ago

Also, I switched to the master branch now. So please adjust the PR towards master. (right under the title). Might have to do a fast forward merge, but shouldn't be any serious conflicts.

andersfylling commented 2 years ago

Hey, I thought a bit about the httd.Method* consts and figured they're a bit silly. I removed them here: https://github.com/andersfylling/disgord/pull/430

so please use http.Method* instead :+1:

andersfylling commented 2 years ago

You see the WithContext method at the end? you need one as well for WithFlags. image

Then you remove the flags parameters from the rest endpoints image

Acnologla commented 2 years ago

Why are the tests failing? the function receives a []Flag not a Flag

image

andersfylling commented 2 years ago

It's a single Flag not a slice. It's a binary flag essentially, so in your WithFlags method you should do

c.flags = mergeFlags(flags) and update the struct definition to only hold flags Flag.

andersfylling commented 2 years ago

I wondwr if your code is perhaps a little outdated? Or i forgot to adjust a func signature.

I remember i removed those flag slices a while back.

On Thu, 26 Aug 2021, 18:44 Acnologla, @.***> wrote:

Why are the tests failing? the function receives a []Flag not a Flag

[image: image] https://user-images.githubusercontent.com/35548489/131002525-46112e88-f4c6-4e25-81a5-cbaf438a9221.png

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/andersfylling/disgord/pull/429#issuecomment-906572584, or unsubscribe https://github.com/notifications/unsubscribe-auth/AB346VBXIGPRXHMRSGPJ44DT6ZVPLANCNFSM5CTFKVNA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&utm_campaign=notification-email .

Acnologla commented 2 years ago

i think it's all right now

paulhobbel commented 2 years ago

I wondwr if your code is perhaps a little outdated? Or i forgot to adjust a func signature. I remember i removed those flag slices a while back. On Thu, 26 Aug 2021, 18:44 Acnologla, @.***> wrote: Why are the tests failing? the function receives a []Flag not a Flag [image: image] https://user-images.githubusercontent.com/35548489/131002525-46112e88-f4c6-4e25-81a5-cbaf438a9221.png — You are receiving this because you commented. Reply to this email directly, view it on GitHub <#429 (comment)>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AB346VBXIGPRXHMRSGPJ44DT6ZVPLANCNFSM5CTFKVNA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&utm_campaign=notification-email .

The PR is based on the develop branch, I believe develop is not up to date with master

andersfylling commented 2 years ago

I wondwr if your code is perhaps a little outdated? Or i forgot to adjust a func signature. I remember i removed those flag slices a while back.

The PR is based on the develop branch, I believe develop is not up to date with master

This is true. I'm eventually removing the develop branch. Master is now main branch of this repository.

This is just a decision that was done when I started trying to make this project more predictable / simpler.

Hope this helps: https://github.com/andersfylling/disgord/commit/d0894f42da544f0f1e661d9075da7e836081be82

codecov[bot] commented 2 years ago

Codecov Report

Merging #429 (d0894f4) into master (931a8b4) will decrease coverage by 0.64%. The diff coverage is n/a.

:exclamation: Current head d0894f4 differs from pull request most recent head 96522c7. Consider uploading reports for the commit 96522c7 to get more accurate results Impacted file tree graph

@@            Coverage Diff             @@
##           master     #429      +/-   ##
==========================================
- Coverage   39.06%   38.42%   -0.65%     
==========================================
  Files          54       56       +2     
  Lines        6236     6317      +81     
==========================================
- Hits         2436     2427       -9     
- Misses       3536     3625      +89     
- Partials      264      265       +1     
Flag Coverage Δ
integration 29.98% <ø> (-0.90%) :arrow_down:
unit 30.33% <ø> (-0.30%) :arrow_down:

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
member.go 21.51% <0.00%> (-2.88%) :arrow_down:
user.go 26.38% <0.00%> (-2.62%) :arrow_down:
rest.go 29.94% <0.00%> (-0.45%) :arrow_down:
guild.go 18.80% <0.00%> (-0.38%) :arrow_down:
emoji.go 0.00% <0.00%> (ø)
invite.go 0.00% <0.00%> (ø)
struct.go 32.07% <0.00%> (ø)
gateway.go 41.32% <0.00%> (ø)
webhook.go 0.00% <0.00%> (ø)
reaction.go 0.00% <0.00%> (ø)
... and 8 more

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 931a8b4...96522c7. Read the comment docs.

andersfylling commented 2 years ago

@Acnologla you have to run "go generate" to get the code to compile. image

andersfylling commented 2 years ago

nice! the CI here should show you what the remaining issue is.