2025-07-09 20:11:12 +00:00
|
|
|
package bolt
|
|
|
|
|
|
2025-08-17 20:37:22 -04:00
|
|
|
import (
|
|
|
|
|
"fmt"
|
2026-02-24 16:54:25 -05:00
|
|
|
"time"
|
2025-07-09 20:11:12 +00:00
|
|
|
|
2025-08-17 20:37:22 -04:00
|
|
|
dg "github.com/bwmarrin/discordgo"
|
|
|
|
|
)
|
|
|
|
|
|
2026-01-26 22:59:13 -05:00
|
|
|
const (
|
2026-02-24 16:54:25 -05:00
|
|
|
// the max length allowed for basic messages
|
2026-01-26 22:59:13 -05:00
|
|
|
MSG_MAX_LENGTH = 2000
|
|
|
|
|
)
|
|
|
|
|
|
2026-02-24 16:54:25 -05:00
|
|
|
// Message contains basic information about the messages received and provides a few methods
|
|
|
|
|
// for handling replies, bans, timeouts, reaction, and deletion. All Discord utilities will use
|
|
|
|
|
// a timeout to prevent hanging for too long, this timeout can be customized with the WithTimeout
|
|
|
|
|
// option.
|
2025-09-07 01:10:48 -04:00
|
|
|
type Message struct {
|
2026-02-24 16:54:25 -05:00
|
|
|
Author string //current username of the message author
|
|
|
|
|
authorID string //discord ID of message author
|
|
|
|
|
authorRoles []string
|
|
|
|
|
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
|
2025-09-07 01:10:48 -04:00
|
|
|
sesh *bolt
|
|
|
|
|
}
|
2025-07-09 20:11:12 +00:00
|
|
|
|
2026-02-24 16:54:25 -05:00
|
|
|
// React applies reaction to the message
|
2025-09-07 01:10:48 -04:00
|
|
|
func (m *Message) React(emoji Reaction) error {
|
2026-02-24 16:54:25 -05:00
|
|
|
return m.sesh.MessageReactionAdd(m.channelID, m.ID, fmt.Sprint(emoji))
|
2025-09-07 01:10:48 -04:00
|
|
|
}
|
|
|
|
|
|
2026-02-24 16:54:25 -05:00
|
|
|
// Respond sends a response to the message, handling chunking if the message exceeds max length
|
2025-09-07 01:10:48 -04:00
|
|
|
func (m *Message) Respond(res string) error {
|
2026-01-26 22:59:13 -05:00
|
|
|
if len(res) > MSG_MAX_LENGTH {
|
|
|
|
|
for len(res) > 0 {
|
|
|
|
|
//send full chunk size allowed by discord
|
|
|
|
|
sc := res[:MSG_MAX_LENGTH]
|
2026-02-24 16:54:25 -05:00
|
|
|
rep := m.sesh.createReply(sc, m.ID, m.channelID, m.serverID)
|
2026-01-26 22:59:13 -05:00
|
|
|
_, err := m.sesh.ChannelMessageSendComplex(m.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 {
|
2026-02-24 16:54:25 -05:00
|
|
|
final := m.sesh.createReply(res, m.ID, m.channelID, m.serverID)
|
2026-01-26 22:59:13 -05:00
|
|
|
_, err := m.sesh.ChannelMessageSendComplex(m.channelID, final)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-24 16:54:25 -05:00
|
|
|
//short enough message to send in one go
|
|
|
|
|
rep := m.sesh.createReply(res, m.ID, m.channelID, m.serverID)
|
2025-09-07 01:10:48 -04:00
|
|
|
_, err := m.sesh.ChannelMessageSendComplex(m.channelID, rep)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-24 16:54:25 -05:00
|
|
|
// Delete removes the message from the current channel
|
2025-09-07 01:10:48 -04:00
|
|
|
func (m *Message) Delete() error {
|
2026-02-24 16:54:25 -05:00
|
|
|
return m.sesh.ChannelMessageDelete(m.channelID, m.ID, nil)
|
2025-09-07 01:10:48 -04:00
|
|
|
}
|
2025-07-09 20:11:12 +00:00
|
|
|
|
2026-02-24 16:54:25 -05:00
|
|
|
// Timeout sets a timeout for the message author
|
|
|
|
|
func (m *Message) Timeout(duration time.Time) error {
|
|
|
|
|
return m.sesh.GuildMemberTimeout(m.serverID, m.authorID, &duration)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ClearTimeout removes all timeouts for the message author
|
|
|
|
|
func (m *Message) ClearTimeout() error {
|
|
|
|
|
return m.sesh.GuildMemberTimeout(m.serverID, m.authorID, nil)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Ban removes a user from the server, banning them and removing all messages within the range of
|
|
|
|
|
// the days parameter
|
|
|
|
|
func (m *Message) Ban(reason string, days int) error {
|
|
|
|
|
return m.sesh.GuildBanCreateWithReason(m.serverID, m.authorID, reason, days)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// unban user
|
|
|
|
|
|
|
|
|
|
// ClearBan deletes the ban on message Authors
|
|
|
|
|
|
|
|
|
|
// lol this won't work, they're banned, same with all clear*
|
|
|
|
|
func (m *Message) ClearBan() error {
|
|
|
|
|
return m.sesh.GuildBanDelete(m.serverID, m.authorID)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *Message) Mute(username string) error {
|
|
|
|
|
return m.sesh.GuildMemberMute(m.serverID, m.authorID, true)
|
2025-08-17 20:37:22 -04:00
|
|
|
}
|
|
|
|
|
|
2025-09-07 01:10:48 -04:00
|
|
|
// message attachment details
|
2025-07-09 20:11:12 +00:00
|
|
|
type MessageAttachment struct {
|
|
|
|
|
ID string
|
|
|
|
|
URL string
|
|
|
|
|
ProxyURL string
|
|
|
|
|
Filename string
|
|
|
|
|
ContentType string
|
|
|
|
|
Width int
|
|
|
|
|
Height int
|
|
|
|
|
Size int
|
|
|
|
|
DurationSecs float64
|
|
|
|
|
}
|