Files
bolt/message.go
jake 01dd3633ef adding Context
- converting messages and methods into one context per event
2026-02-24 18:45:00 -05:00

114 lines
3.1 KiB
Go

package bolt
import (
"fmt"
"time"
dg "github.com/bwmarrin/discordgo"
)
const (
// the max length allowed for basic messages
MSG_MAX_LENGTH = 2000
)
// command payload functions, any strings returned are sent as a response to the command
type Payload func(c *Context) error
// message attachment details
type MessageAttachment struct {
ID string
URL string
ProxyURL string
Filename string
ContentType string
Width int
Height int
Size int
DurationSecs float64
}
type Author struct {
Name string
ID string
Roles []string
}
type Message struct {
Author Author
ID string //message ID
Words []string //message data split on whitespaces
Content string //entire message data string
Channel string //name of channel message was sent in
ChannelID string //ID of channel message was sent in
Server string //name of guild message was sent in
ServerID string //ID of guild message was sent in
Attachments []MessageAttachment //any attachments bound to the message
Mentions []*dg.User
}
type Context struct {
Message *Message
bolt *bolt
}
// React applies reaction to the message
func (c *Context) React(emoji Reaction) error {
return c.bolt.MessageReactionAdd(c.Message.ChannelID, c.Message.ID, fmt.Sprint(emoji))
}
// Respond sends a response to the message, handling chunking if the message exceeds max length
func (c *Context) Respond(res string) error {
if len(res) > MSG_MAX_LENGTH {
for len(res) > 0 {
//send full chunk size allowed by discord
sc := res[:MSG_MAX_LENGTH]
rep := c.bolt.createReply(sc, c.Message.ID, c.Message.ChannelID, c.Message.ServerID)
_, err := c.bolt.ChannelMessageSendComplex(c.Message.ChannelID, rep)
if err != nil {
return err
}
res = res[MSG_MAX_LENGTH:]
//if we have left than a full chunk send the rest and break the loop
if len(res) < MSG_MAX_LENGTH {
final := c.bolt.createReply(res, c.Message.ID, c.Message.ChannelID, c.Message.ServerID)
_, err := c.bolt.ChannelMessageSendComplex(c.Message.ChannelID, final)
if err != nil {
return err
}
break
}
}
return nil
}
//short enough message to send in one go
rep := c.bolt.createReply(res, c.Message.ID, c.Message.ChannelID, c.Message.ServerID)
_, err := c.bolt.ChannelMessageSendComplex(c.Message.ChannelID, rep)
return err
}
// Delete removes the message from the current channel
func (c *Context) Delete() error {
return c.bolt.ChannelMessageDelete(c.Message.ChannelID, c.Message.ID, nil)
}
func (c *Context) Timeout(userId string, duration time.Time) error {
return c.bolt.GuildMemberTimeout(c.Message.ServerID, userId, &duration)
}
func (c *Context) ClearTimeout(userId string) error {
return c.bolt.GuildMemberTimeout(c.Message.ServerID, userId, nil)
}
func (c *Context) Mute(userId string) error {
return c.bolt.GuildMemberMute(c.Message.ServerID, userId, true)
}
func (c *Context) Unmute(userId string) error {
return c.bolt.GuildMemberMute(c.Message.ServerID, userId, false)
}