Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ go run ./cmd/frodo-bot

### 3. Supported Commands

nothing yet
| Command | Description |
| ------------- | ------------------------------------------------------------------- |
| /morgul-blade | Stab Frodo with the cursed Morgul blade to silence him indefinitely |

Press `Ctrl+C` to stop.

Expand Down
8 changes: 4 additions & 4 deletions cmd/frodo-bot/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,22 @@ func run() error {
return fmt.Errorf("config failed: %w", err)
}

discord, err := discordgo.New("Bot " + cfg.Token)
session, err := discordgo.New("Bot " + cfg.Token)
if err != nil {
return fmt.Errorf("bot failed to init: %w", err)
}

b := bot.New(cfg)
b.Register(discord)
b.Register(session)

// open session
err = discord.Open()
err = session.Open()
if err != nil {
return fmt.Errorf("failed to open discord session: %w", err)
}

// executes after run returns
defer discord.Close()
defer session.Close()

fmt.Println("bot is running")
c := make(chan os.Signal, 1)
Expand Down
Binary file added internal/bot/assets/morgul-blade.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 12 additions & 1 deletion internal/bot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,35 @@ import (
"log"

"github.com/SimonKimDev/frodo-bot/internal/config"
"github.com/SimonKimDev/frodo-bot/internal/wound"
"github.com/bwmarrin/discordgo"
)

type Bot struct {
gifURL string
wounds *wound.Tracker
}

func New(cfg config.Config) *Bot {
return &Bot{gifURL: cfg.GIFURL}
return &Bot{
wounds: wound.New(),
gifURL: cfg.GIFURL,
}
}

// Register adds the bot's event handlers and intents to the current Session.
// Should be called before calling session.Open
func (b *Bot) Register(s *discordgo.Session) {
s.AddHandler(b.onReady)
s.AddHandler(b.onMessage)
s.AddHandler(b.onInteraction)
s.Identify.Intents = discordgo.IntentsMessageContent | discordgo.IntentsGuildMessages
}

func (b *Bot) onReady(s *discordgo.Session, r *discordgo.Ready) {
user := r.User
log.Printf("logged in as %v", user.Username)
if _, err := s.ApplicationCommandCreate(user.ID, "", morgulBladeCommand()); err != nil {
log.Printf("failed to create command %v", err)
}
}
5 changes: 5 additions & 0 deletions internal/bot/secret.go → internal/bot/messagehandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ func (b *Bot) onMessage(s *discordgo.Session, m *discordgo.MessageCreate) {
return
}

if b.wounds.IsStabbed(m.GuildID) {
return
}

if !shouldReply(m.Content) {
return
}
Expand All @@ -21,6 +25,7 @@ func (b *Bot) onMessage(s *discordgo.Session, m *discordgo.MessageCreate) {
}
}

// shouldReply reports whether to post the meme or not.
func shouldReply(content string) bool {
return strings.Contains(strings.ToLower(content), "secret")
}
61 changes: 61 additions & 0 deletions internal/bot/morgulblade.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package bot

import (
"bytes"
_ "embed"
"log"

"github.com/bwmarrin/discordgo"
)

const morgulBladeCmd = "morgul-blade"

//go:embed assets/morgul-blade.png
var morgulBladePNG []byte

func morgulBladeCommand() *discordgo.ApplicationCommand {
return &discordgo.ApplicationCommand{
Name: morgulBladeCmd,
Description: "Stabs Frodo with the Morgul Blade to silence him indefinitely",
Contexts: &[]discordgo.InteractionContextType{discordgo.InteractionContextGuild},
}
}

func (b *Bot) onInteraction(s *discordgo.Session, i *discordgo.InteractionCreate) {
if i.Type != discordgo.InteractionApplicationCommand {
return
}

if i.ApplicationCommandData().Name != morgulBladeCmd {
return
}

if !b.wounds.Stab(i.GuildID) {
respond(s, i, &discordgo.InteractionResponseData{
Content: "You awful person, Frodo has already been stabbed.",
Flags: discordgo.MessageFlagsEphemeral,
})
return
}

respond(s, i, &discordgo.InteractionResponseData{
Content: "DEUUEAUGH!!",
Files: []*discordgo.File{
{
Name: "morgul-blade.png",
ContentType: "image/png",
Reader: bytes.NewReader(morgulBladePNG),
},
},
})
}

func respond(s *discordgo.Session, i *discordgo.InteractionCreate, data *discordgo.InteractionResponseData) {
err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: data,
})
if err != nil {
log.Printf("respond to /%s: %v", i.ApplicationCommandData().Name, err)
}
}
38 changes: 38 additions & 0 deletions internal/wound/wound.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package wound

import (
"sync"
)

// Tracker records which server (guild) have Frodo stabbed. Monsters...
// It's safe for concurrent use.
type Tracker struct {
mu sync.Mutex
stabbed map[string]bool
}

func New() *Tracker {
return &Tracker{stabbed: make(map[string]bool)}
}

// Stab marks guildID as stabbed return false if already stabbed
// Because it wouldn't make sense to stab Frodo multiple times.
// Come on, he's already wounded.
func (t *Tracker) Stab(guildID string) bool {
t.mu.Lock()
defer t.mu.Unlock()

if t.stabbed[guildID] {
return false
}

t.stabbed[guildID] = true
return true
}

// IsStabbed reports whether Frodo was already stabbed or not
func (t *Tracker) IsStabbed(guildID string) bool {
t.mu.Lock()
defer t.mu.Unlock()
return t.stabbed[guildID]
}
Loading