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) })