Skip to content

Commit

Permalink
feat: add temporary hack to support slash commands
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Théo Vidal committed Jan 6, 2021
1 parent 5f45464 commit 6b2b083
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
15 changes: 15 additions & 0 deletions 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)
}
}
24 changes: 20 additions & 4 deletions main.go
@@ -1,6 +1,7 @@
package main

import (
"encoding/json"
"github.com/bwmarrin/discordgo"
"github.com/theovidal/onyxcord"

Expand All @@ -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)
})

Expand Down

0 comments on commit 6b2b083

Please sign in to comment.