Skip to content

Commit

Permalink
feat: adding discord slash commands
Browse files Browse the repository at this point in the history
  • Loading branch information
kehiy committed Jan 27, 2024
1 parent b6f84ef commit a288692
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 5 deletions.
2 changes: 1 addition & 1 deletion client/client_mgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ func (cm *Mgr) GetTransactionData(txID string) (*pactus.GetTransactionResponse,
return nil, errors.New("unable to get transaction data")
}

func (cm *Mgr) Close() {
func (cm *Mgr) Stop() {
for addr, c := range cm.clients {
if err := c.Close(); err != nil {
log.Error("could not close connection to RPC node", "err", err, "RPCAddr", addr)
Expand Down
15 changes: 12 additions & 3 deletions cmd/commands/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/kehiy/RoboPac/client"
"github.com/kehiy/RoboPac/config"
"github.com/kehiy/RoboPac/discord"
"github.com/kehiy/RoboPac/engine"
"github.com/kehiy/RoboPac/log"
"github.com/kehiy/RoboPac/store"
Expand All @@ -21,7 +22,7 @@ func RunCommand(parentCmd *cobra.Command) {
}
parentCmd.AddCommand(run)

run.Run = func(cmd *cobra.Command, args []string) {
run.Run = func(_ *cobra.Command, _ []string) {
// load configuration.
config, err := config.Load()
if err != nil {
Expand Down Expand Up @@ -72,16 +73,24 @@ func RunCommand(parentCmd *cobra.Command) {
// starting botEngine.
botEngine, err := engine.NewBotEngine(eSl, cm, wallet, store)
if err != nil {
log.Panic("could not start discord bot", "err", err)
log.Panic("could not start bot engine", "err", err)
}
botEngine.Start()

discordBot, err := discord.NewDiscordBot(botEngine, config.DiscordBotCfg.DiscordToken,
config.DiscordBotCfg.DiscordGuildID)
if err != nil {
log.Panic("could not start discord bot", "err", err)
}
discordBot.Start()

sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
<-sigChan

// gracefully shutdown the bot.
discordBot.Stop()
cm.Stop()
botEngine.Stop()
cm.Close()
}
}
14 changes: 14 additions & 0 deletions discord/commands.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package discord

import "github.com/bwmarrin/discordgo"

var commands = []*discordgo.ApplicationCommand{
{
Name: "help",
Description: "Help command for RoboPac",
},
}

var commandHandlers = map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate){
"help": helpCommandHandler,
}
17 changes: 16 additions & 1 deletion discord/discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type DiscordBot struct {
}

func NewDiscordBot(botEngine engine.Engine, token, guildID string) (*DiscordBot, error) {
s, err := discordgo.New(token)
s, err := discordgo.New("Bot " + token)
if err != nil {
return nil, err
}
Expand All @@ -28,10 +28,25 @@ func NewDiscordBot(botEngine engine.Engine, token, guildID string) (*DiscordBot,
func (db *DiscordBot) Start() {
log.Info("starting Discord Bot...")

db.Session.AddHandler(func(s *discordgo.Session, i *discordgo.InteractionCreate) {
if h, ok := commandHandlers[i.ApplicationCommandData().Name]; ok {
h(s, i)
}
})

err := db.Session.Open()
if err != nil {
log.Panic("can't open discord session", "err", err)
}

registeredCommands := make([]*discordgo.ApplicationCommand, len(commands))
for i, v := range commands {
cmd, err := db.Session.ApplicationCommandCreate(db.Session.State.User.ID, db.GuildID, v)
if err != nil {
log.Panic("can not register discord command", "name", v.Name, "err", err)
}
registeredCommands[i] = cmd
}
}

func (db *DiscordBot) Stop() {
Expand Down
16 changes: 16 additions & 0 deletions discord/embeds.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package discord

import "github.com/bwmarrin/discordgo"

func helpEmbed(s *discordgo.Session) *discordgo.MessageEmbed {
return &discordgo.MessageEmbed{
Title: "RoboPac Help",
URL: "https://pactus.org",

Check failure on line 8 in discord/embeds.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed (gofumpt)
Author: &discordgo.MessageEmbedAuthor{
URL: "https://pactus.org",
IconURL: s.State.User.AvatarURL(""),
Name: s.State.User.Username,
},
Description: "RoboPac is a robot that provides support and information about the Pactus Blockchain.",
}
}
7 changes: 7 additions & 0 deletions discord/handlers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package discord

import "github.com/bwmarrin/discordgo"

func helpCommandHandler(s *discordgo.Session, i *discordgo.InteractionCreate) {
s.ChannelMessageSendEmbedReply(i.ChannelID, helpEmbed(s), i.Message.Reference())

Check failure on line 6 in discord/handlers.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `s.ChannelMessageSendEmbedReply` is not checked (errcheck)
}

0 comments on commit a288692

Please sign in to comment.