github.com/starshine-sys/bcr@v0.21.0/message_send.go (about)

     1  package bcr
     2  
     3  import (
     4  	"io"
     5  
     6  	"github.com/diamondburned/arikawa/v3/api"
     7  	"github.com/diamondburned/arikawa/v3/discord"
     8  	"github.com/diamondburned/arikawa/v3/utils/sendpart"
     9  )
    10  
    11  // MessageSend is a helper struct for sending messages.
    12  // By default, it will send a message to the current channel, and check permissions (unless the target channel is the current channel and is a DM channel).
    13  // These can be overridden with the Channel(id) and TogglePermCheck() methods.
    14  // Alternatively, you can get the base SendMessageData struct and use that manually.
    15  type MessageSend struct {
    16  	Data api.SendMessageData
    17  
    18  	user    discord.UserID
    19  	channel discord.ChannelID
    20  
    21  	checkPerms bool
    22  	ctx        *Context
    23  }
    24  
    25  // NewMessage creates a new MessageSend object.
    26  // Only the *first* channel argument is used, if omitted it's set to the current channel.
    27  func (ctx *Context) NewMessage(channel ...discord.ChannelID) *MessageSend {
    28  	ch := ctx.Channel.ID
    29  	if len(channel) > 0 {
    30  		ch = channel[0]
    31  	}
    32  
    33  	return &MessageSend{
    34  		Data:       api.SendMessageData{},
    35  		ctx:        ctx,
    36  		checkPerms: true,
    37  		channel:    ch,
    38  	}
    39  }
    40  
    41  // NewDM creates a new MessageSend object for the given user.
    42  // If the user has closed DMs, this will not error until the Send() call.
    43  func (ctx *Context) NewDM(user discord.UserID) *MessageSend {
    44  	return &MessageSend{
    45  		Data:       api.SendMessageData{},
    46  		ctx:        ctx,
    47  		checkPerms: false,
    48  		user:       user,
    49  	}
    50  }
    51  
    52  // Channel sets the channel to send the message to
    53  func (m *MessageSend) Channel(c discord.ChannelID) *MessageSend {
    54  	m.channel = c
    55  	return m
    56  }
    57  
    58  // Content sets the message content
    59  func (m *MessageSend) Content(c string) *MessageSend {
    60  	m.Data.Content = c
    61  	return m
    62  }
    63  
    64  // Embeds sets the message embeds
    65  func (m *MessageSend) Embeds(e ...discord.Embed) *MessageSend {
    66  	m.Data.Embeds = e
    67  	return m
    68  }
    69  
    70  // AddFile adds a file to the message
    71  func (m *MessageSend) AddFile(name string, reader io.Reader) *MessageSend {
    72  	m.Data.Files = append(m.Data.Files, sendpart.File{
    73  		Name:   name,
    74  		Reader: reader,
    75  	})
    76  	return m
    77  }
    78  
    79  // BlockMentions blocks all mentions from this message
    80  func (m *MessageSend) BlockMentions() *MessageSend {
    81  	m.Data.AllowedMentions = &api.AllowedMentions{Parse: nil}
    82  	return m
    83  }
    84  
    85  // AllowedMentions sets the message's allowed mentions
    86  func (m *MessageSend) AllowedMentions(a *api.AllowedMentions) *MessageSend {
    87  	m.Data.AllowedMentions = a
    88  	return m
    89  }
    90  
    91  // TogglePermCheck toggles whether or not to check permissions for the destination channel
    92  func (m *MessageSend) TogglePermCheck() *MessageSend {
    93  	if m.checkPerms {
    94  		m.checkPerms = false
    95  	} else {
    96  		m.checkPerms = true
    97  	}
    98  	return m
    99  }
   100  
   101  // Reference sets the message this message will reply to
   102  func (m *MessageSend) Reference(id discord.MessageID) *MessageSend {
   103  	m.Data.Reference = &discord.MessageReference{
   104  		MessageID: id,
   105  	}
   106  	return m
   107  }
   108  
   109  // Send sends the message
   110  func (m *MessageSend) Send() (msg *discord.Message, err error) {
   111  	// if it's a user, send a DM
   112  	if m.user != 0 {
   113  		return m.sendDM()
   114  	}
   115  	// otherwise send the message normally
   116  	return m.send()
   117  }
   118  
   119  func (m *MessageSend) send() (msg *discord.Message, err error) {
   120  	return m.ctx.State.SendMessageComplex(m.channel, m.Data)
   121  }
   122  
   123  func (m *MessageSend) sendDM() (msg *discord.Message, err error) {
   124  	ch, err := m.ctx.State.CreatePrivateChannel(m.user)
   125  	if err != nil {
   126  		return nil, err
   127  	}
   128  
   129  	return m.ctx.State.SendMessageComplex(ch.ID, m.Data)
   130  }