siscodeorg / sisbase

An easy to use discord bot base for D#+. Discord Link :
https://discord.gg/ycvm6xJ
MIT License
1 stars 0 forks source link

Menu Api #2

Open alikindsys opened 4 years ago

alikindsys commented 4 years ago

Adds a Menu abstraction in order to add create menus with embeds.

Said api must provide support for a discordembed to be used as a display and buttons for the user interaction.

Buttons must be dynamically added and have a limit of 20 buttons (due to discord api limitations).

Proposed usage:

var testMenu = new Menu {
    Embed = EmbedBase.OutputEmbed("Test"),
    Buttons = new Dictionary<DiscordEmoji, Func<Task>> {
        [emojiA] = emojiAClicked,
        [emojiB] = emojiBClicked
    },
    Behaviour = ButtonBehaviour.Action
};

Proposed sisbase Implementation:

class MenuIconActions : IDisposable {
    Dictionary<DiscordEmoji, Func<Task>> actions;
    DiscordMessage menu;
    DiscordClient client;

    MenuActionIcons(DiscordClient c, DiscordMessage menumsg) { 
        menu = menumsg;
        client = c;
        client.ReactionAddedEvent += Dispatch;
    }

    async Task Dispatch(ReactionAddedEvent e) {
        if(e.Message != menu) return;
        var action = actions.TryGetValue(e.Emoji);
        if (action = null) return;
        await action();
    }

    void Register(DiscordEmoji emoji, Func<Task> action) => actions[emoji] = action;

    void Dispose() {
        client.ReactionAddedEvent -= Dispatch;
    }
}