From 6b2b08374f6413141e14dbc7688523550fda56ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20Vidal?= Date: Wed, 6 Jan 2021 19:10:06 +0100 Subject: [PATCH] feat: add temporary hack to support slash commands Added support for new slash commands in a very dirty manner. Waiting for bwmarrin/discordgo#856 to be merged so they'll be supported in the official library. --- games/irregular_verbs/interaction.go | 15 +++++++++++++++ main.go | 24 ++++++++++++++++++++---- 2 files changed, 35 insertions(+), 4 deletions(-) create mode 100644 games/irregular_verbs/interaction.go diff --git a/games/irregular_verbs/interaction.go b/games/irregular_verbs/interaction.go new file mode 100644 index 0000000..a698028 --- /dev/null +++ b/games/irregular_verbs/interaction.go @@ -0,0 +1,15 @@ +package irregular_verbs + +import ( + "context" + + "github.com/bwmarrin/discordgo" + "github.com/theovidal/onyxcord" +) + +func HandleInteraction(bot *onyxcord.Bot, message *discordgo.Message) { + verbsPlayer := bot.Cache.Exists(context.Background(), "verbs:"+message.ChannelID).Val() + if verbsPlayer == 1 { + HandleAnswer(bot, message, "verbs:"+message.ChannelID) + } +} diff --git a/main.go b/main.go index d3625ef..2d4f49f 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + "encoding/json" "github.com/bwmarrin/discordgo" "github.com/theovidal/onyxcord" @@ -13,11 +14,26 @@ func main() { bot.RegisterCommand("verbs", irregular_verbs.Command()) irregular_verbs.VerbsPlayers.Initialize() - bot.Client.AddHandler(func(session *discordgo.Session, message *discordgo.MessageCreate) { - verbsPlayer, exists := irregular_verbs.VerbsPlayers.GetPlayer(message.ChannelID) - if exists { - irregular_verbs.HandleAnswer(&bot, message.Message, verbsPlayer) + bot.Client.AddHandler(func(session *discordgo.Session, event *discordgo.Event) { + if event.Type == "INTERACTION_CREATE" { + data := make(map[string]interface{}) + json.Unmarshal(event.RawData, &data) + command := data["data"].(map[string]interface{})["name"].(string) + if command == "verbs" { + channelID := data["channel_id"].(string) + event := discordgo.MessageCreate{ + Message: &discordgo.Message{ + ChannelID: channelID, + Author: &discordgo.User{}, + }, + } + irregular_verbs.Command().Execute([]string{}, bot, &event) + } } + }) + + bot.Client.AddHandler(func(session *discordgo.Session, message *discordgo.MessageCreate) { + irregular_verbs.HandleInteraction(&bot, message.Message) bot.OnCommand(session, message) })