WIP
started the facelift of the repo, adding in bans and timeouts. That comes with some restructure. Also implementing a goroutine limit for command handlers and options around that, as well as moving the intents to options to allow stronger restrictions
This commit is contained in:
90
message.go
90
message.go
@@ -2,44 +2,48 @@ package bolt
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
dg "github.com/bwmarrin/discordgo"
|
||||
)
|
||||
|
||||
const (
|
||||
// the max discord allows for basic messages
|
||||
// the max length allowed for basic messages
|
||||
MSG_MAX_LENGTH = 2000
|
||||
)
|
||||
|
||||
// the message struct is passed to the command payload providing basic
|
||||
// message information and needed methods
|
||||
// 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.
|
||||
type Message struct {
|
||||
Author string //username of message author
|
||||
ID string //discord ID of message author
|
||||
msgID string //id string of message
|
||||
Words []string //words from message split on whitespace
|
||||
Content string //entire message content
|
||||
Channel string //message channel
|
||||
channelID string //id of channel message was sent in
|
||||
Server string //message guild
|
||||
serverID string //id of guild message was sent in
|
||||
Attachments []MessageAttachment
|
||||
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
|
||||
sesh *bolt
|
||||
}
|
||||
|
||||
// applies reaction to message
|
||||
// React applies reaction to the message
|
||||
func (m *Message) React(emoji Reaction) error {
|
||||
return m.sesh.MessageReactionAdd(m.channelID, m.msgID, fmt.Sprint(emoji))
|
||||
return m.sesh.MessageReactionAdd(m.channelID, m.ID, fmt.Sprint(emoji))
|
||||
}
|
||||
|
||||
// sends response to message, if the response length is greater than 2000 characters the
|
||||
// messages are split and sent seperatly
|
||||
// Respond sends a response to the message, handling chunking if the message exceeds max length
|
||||
func (m *Message) 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 := m.sesh.createReply(sc, m.msgID, m.channelID, m.serverID)
|
||||
rep := m.sesh.createReply(sc, m.ID, m.channelID, m.serverID)
|
||||
_, err := m.sesh.ChannelMessageSendComplex(m.channelID, rep)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -48,7 +52,7 @@ func (m *Message) Respond(res string) error {
|
||||
|
||||
//if we have left than a full chunk send the rest and break the loop
|
||||
if len(res) < MSG_MAX_LENGTH {
|
||||
final := m.sesh.createReply(res, m.msgID, m.channelID, m.serverID)
|
||||
final := m.sesh.createReply(res, m.ID, m.channelID, m.serverID)
|
||||
_, err := m.sesh.ChannelMessageSendComplex(m.channelID, final)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -61,30 +65,44 @@ func (m *Message) Respond(res string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
//short enough message to send in one message
|
||||
rep := m.sesh.createReply(res, m.msgID, m.channelID, m.serverID)
|
||||
//short enough message to send in one go
|
||||
rep := m.sesh.createReply(res, m.ID, m.channelID, m.serverID)
|
||||
_, err := m.sesh.ChannelMessageSendComplex(m.channelID, rep)
|
||||
return err
|
||||
}
|
||||
|
||||
// deletes the message from the channel
|
||||
// Delete removes the message from the current channel
|
||||
func (m *Message) Delete() error {
|
||||
return m.sesh.ChannelMessageDelete(m.channelID, m.msgID, nil)
|
||||
return m.sesh.ChannelMessageDelete(m.channelID, m.ID, nil)
|
||||
}
|
||||
|
||||
// this struct has all of the needed information from the messageCreate event so that
|
||||
// commands can be run asynchronously. Passing the messageCreate to payloads can block routines
|
||||
type MessageCreateEvent struct {
|
||||
AuthorUsername string
|
||||
AuthorID string
|
||||
AuthorRoles []string
|
||||
MsgID string
|
||||
Msg string
|
||||
MsgChanID string
|
||||
MsgChanName string
|
||||
MsgGuildID string
|
||||
MsgGuildName string
|
||||
MsgAttachments []*dg.MessageAttachment
|
||||
// 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)
|
||||
}
|
||||
|
||||
// message attachment details
|
||||
|
||||
Reference in New Issue
Block a user