1
0
Fork 0
mirror of https://github.com/diamondburned/arikawa.git synced 2025-07-23 13:20:51 +00:00

Compare commits

..

3 commits

Author SHA1 Message Date
diamondburned 65d7b8765b Bot: Allow self-mentions; deprecate SanitizeMessage 2021-02-14 13:14:38 -08:00
diamondburned 74019dc909 Bot: Reply with reference to message 2021-02-14 12:39:39 -08:00
Scott abeaef8122
API: Added SendReply methods (#187)
* API: Added SendReply methods

* Grammar edit

* referenceID & adheres to 100 column limit
2021-02-14 12:29:41 -08:00
4 changed files with 85 additions and 19 deletions

View file

@ -191,7 +191,7 @@ func (c *Client) Message(channelID discord.ChannelID, messageID discord.MessageI
EndpointChannels+channelID.String()+"/messages/"+messageID.String())
}
// SendText posts a only-text message to a guild text or DM channel.
// SendText posts a text-only message to a guild text or DM channel.
//
// If operating on a guild channel, this endpoint requires the SEND_MESSAGES
// permission to be present on the current user.
@ -203,6 +203,23 @@ func (c *Client) SendText(channelID discord.ChannelID, content string) (*discord
})
}
// SendTextReply posts a text-only reply to a message ID in a guild text or DM channel
//
// If operating on a guild channel, this endpoint requires the SEND_MESSAGES
// permission to be present on the current user.
//
// Fires a Message Create Gateway event.
func (c *Client) SendTextReply(
channelID discord.ChannelID,
content string,
referenceID discord.MessageID) (*discord.Message, error) {
return c.SendMessageComplex(channelID, SendMessageData{
Content: content,
Reference: &discord.MessageReference{MessageID: referenceID},
})
}
// SendEmbed posts an Embed to a guild text or DM channel.
//
// If operating on a guild channel, this endpoint requires the SEND_MESSAGES
@ -217,6 +234,23 @@ func (c *Client) SendEmbed(
})
}
// SendEmbedReply posts an Embed reply to a message ID in a guild text or DM channel.
//
// If operating on a guild channel, this endpoint requires the SEND_MESSAGES
// permission to be present on the current user.
//
// Fires a Message Create Gateway event.
func (c *Client) SendEmbedReply(
channelID discord.ChannelID,
e discord.Embed,
referenceID discord.MessageID) (*discord.Message, error) {
return c.SendMessageComplex(channelID, SendMessageData{
Embed: &e,
Reference: &discord.MessageReference{MessageID: referenceID},
})
}
// SendMessage posts a message to a guild text or DM channel.
//
// If operating on a guild channel, this endpoint requires the SEND_MESSAGES
@ -232,6 +266,25 @@ func (c *Client) SendMessage(
})
}
// SendMessageReply posts a reply to a message ID in a guild text or DM channel.
//
// If operating on a guild channel, this endpoint requires the SEND_MESSAGES
// permission to be present on the current user.
//
// Fires a Message Create Gateway event.
func (c *Client) SendMessageReply(
channelID discord.ChannelID,
content string,
embed *discord.Embed,
referenceID discord.MessageID) (*discord.Message, error) {
return c.SendMessageComplex(channelID, SendMessageData{
Content: content,
Embed: embed,
Reference: &discord.MessageReference{MessageID: referenceID},
})
}
// https://discord.com/developers/docs/resources/channel#edit-message-json-params
type EditMessageData struct {
// Content is the new message contents (up to 2000 characters).

View file

@ -378,12 +378,9 @@ func (ctx *Context) Start() func() {
}
_, err = ctx.SendMessageComplex(mc.ChannelID, api.SendMessageData{
// Escape the error using the message sanitizer:
Content: ctx.SanitizeMessage(str),
AllowedMentions: &api.AllowedMentions{
Content: str,
// Don't allow mentions.
Parse: emptyMentionTypes,
},
AllowedMentions: &api.AllowedMentions{Parse: emptyMentionTypes},
})
if err != nil {

View file

@ -7,6 +7,7 @@ import (
"github.com/diamondburned/arikawa/v2/api"
"github.com/diamondburned/arikawa/v2/discord"
"github.com/diamondburned/arikawa/v2/gateway"
"github.com/diamondburned/arikawa/v2/utils/json/option"
"github.com/pkg/errors"
)
@ -303,23 +304,36 @@ func (ctx *Context) callMessageCreate(mc *gateway.MessageCreateEvent, value refl
Call:
// call the function and parse the error return value
v, err := cmd.call(value, argv...)
if err != nil {
if err != nil || v == nil {
return err
}
var data api.SendMessageData
switch v := v.(type) {
case string:
v = sub.SanitizeMessage(v)
_, err = ctx.SendMessage(mc.ChannelID, v, nil)
data.Content = v
case *discord.Embed:
_, err = ctx.SendMessage(mc.ChannelID, "", v)
data.Embed = v
case *api.SendMessageData:
if v.Content != "" {
v.Content = sub.SanitizeMessage(v.Content)
}
_, err = ctx.SendMessageComplex(mc.ChannelID, *v)
data = *v
default:
return nil
}
if data.Reference == nil {
data.Reference = &discord.MessageReference{MessageID: mc.ID}
}
if data.AllowedMentions == nil {
// Do not mention on reply by default. Only allow author mentions.
data.AllowedMentions = &api.AllowedMentions{
Users: []discord.UserID{mc.Author.ID},
RepliedUser: option.False,
}
}
_, err = ctx.SendMessageComplex(mc.ChannelID, data)
return err
}

View file

@ -76,12 +76,14 @@ type Subcommand struct {
// Aliases is alternative way to call this subcommand in Discord.
Aliases []string
// SanitizeMessage is executed on the message content if the method returns
// a string content or a SendMessageData.
// SanitizeMessage is currently no longer used automatically.
// AllowedMentions is used instead.
//
// This field is deprecated and will be removed eventually.
SanitizeMessage func(content string) string
// Commands can actually return either a string, an embed, or a
// SendMessageData, with error as the second argument.
// Commands can return either a string, a *discord.Embed, or an
// *api.SendMessageData, with error as the second argument.
// All registered method contexts:
Events []*MethodContext