krisppurg / dimscord

A Discord Bot & REST Library for Nim.
https://krisppurg.github.io/dimscord/
MIT License
222 stars 22 forks source link

Add autocomplete #68

Closed ire4ever1190 closed 2 years ago

ire4ever1190 commented 2 years ago

Implements autocomplete for interactions

basic example

proc interactionCreate(s: Shard, i: Interaction) {.event(discord).} =
    if i.kind == itAutoComplete:
        let things = [
            " and self",
            " and friends",
            " and co"
        ]
        if i.data.isSome:
            for key, option in i.data.get().options:
                if option.focused.get(false):
                    var choices: seq[ApplicationCommandOptionChoice] = @[]
                    for thing in things:
                        choices &= ApplicationCommandOptionChoice(
                            name: option.str & thing,
                            value: (some (option.str & thing), none int)
                        )
                    await discord.api.createInteractionResponse(i.id, i.token, InteractionResponse(
                        kind: irtAutoCompleteResult,
                        choices: choices
                    ))
    else:
        # Handle interactions normally

I went with variant objects over generics in the end so that existing code didn't break