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

Discord interactions #397

Closed Acnologla closed 3 years ago

Acnologla commented 3 years ago

Description

This pull request adds discord interactions gateway interface.

Example:

func interaction(session disgord.Session, evt *disgord.InteractionCreate) {
    if evt.Type == disgord.InteractionMessageComponent {
        if evt.Data.CustomID == "click_one" {
            txt := fmt.Sprintf("User %s clicked in the button %s", evt.Member.User.Username, evt.Message.Components[0].Components[0].Label)
            err := disgord.SendInteractionResponse(evt, &disgord.InteractionResponse{
                Type: disgord.ChannelMessageWithSource,
                Data: &disgord.InteractionApplicationCommandCallbackData{
                    Content: txt,
                },
            })
            fmt.Println(err)
        }
    }
}
func main() {
    client := disgord.New(disgord.Config{
        BotToken:     "token",
    })
    defer client.Gateway().StayConnectedUntilInterrupted()
    client.Gateway().InteractionCreate(interaction)
}

Type of change

Please delete options that are not relevant.

Benchmarks

If this PR requires benchmarks (say it is an very dependent component or takes a lot of resources/use, use pprof if you need to) then the benchmarks are provided before and after such that we can make logical decisions. Note that if you add a benchmark and find your solution to run slower, the code might still be valuable so your results are welcomed anyways! If no benchmarks are needed, feel free to delete til paragraph.

Checklist:

andersfylling commented 3 years ago

Looks good! I have one request that needs to adjusted, but besides that it's all clean.

andersfylling commented 3 years ago

While you might see methods looking like this, some can easily be converted. image

REFACTORED - I guess something like this is how it would look for your case image