Skip to content

Commit

Permalink
Initial allowed_mentions implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
swarley committed Apr 9, 2020
1 parent 2c2b50e commit ba4e4e7
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 18 deletions.
4 changes: 2 additions & 2 deletions lib/discordrb/api/channel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,13 @@ def message(token, channel_id, message_id)

# Send a message to a channel
# https://discordapp.com/developers/docs/resources/channel#create-message
def create_message(token, channel_id, message, tts = false, embed = nil, nonce = nil)
def create_message(token, channel_id, message, tts = false, embed = nil, nonce = nil, allowed_mentions = nil)
Discordrb::API.request(
:channels_cid_messages_mid,
channel_id,
:post,
"#{Discordrb::API.api_base}/channels/#{channel_id}/messages",
{ content: message, tts: tts, embed: embed, nonce: nonce }.to_json,
{ content: message, tts: tts, embed: embed, nonce: nonce, allowed_mentions: allowed_mentions }.to_json,
Authorization: token,
content_type: :json
)
Expand Down
10 changes: 6 additions & 4 deletions lib/discordrb/bot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -363,12 +363,13 @@ def delete_invite(code)
# @param content [String] The text that should be sent as a message. It is limited to 2000 characters (Discord imposed).
# @param tts [true, false] Whether or not this message should be sent using Discord text-to-speech.
# @param embed [Hash, Discordrb::Webhooks::Embed, nil] The rich embed to append to this message.
# @param allowed_mentions [Hash, nil] [Allowed Mentions object](https://discordapp.com/developers/docs/resources/channel#allowed-mentions-object)
# @return [Message] The message that was sent.
def send_message(channel, content, tts = false, embed = nil)
def send_message(channel, content, tts = false, embed = nil, allowed_mentions = nil)
channel = channel.resolve_id
debug("Sending message to #{channel} with content '#{content}'")

response = API::Channel.create_message(token, channel, content, tts, embed ? embed.to_hash : nil)
response = API::Channel.create_message(token, channel, content, tts, embed ? embed.to_hash : nil, nil, allowed_mentions)
Message.new(JSON.parse(response), self)
end

Expand All @@ -379,11 +380,12 @@ def send_message(channel, content, tts = false, embed = nil)
# @param timeout [Float] The amount of time in seconds after which the message sent will be deleted.
# @param tts [true, false] Whether or not this message should be sent using Discord text-to-speech.
# @param embed [Hash, Discordrb::Webhooks::Embed, nil] The rich embed to append to this message.
def send_temporary_message(channel, content, timeout, tts = false, embed = nil)
# @param allowed_mentions [Hash, nil] [Allowed Mentions object](https://discordapp.com/developers/docs/resources/channel#allowed-mentions-object)
def send_temporary_message(channel, content, timeout, tts = false, embed = nil, allowed_mentions = nil)
Thread.new do
Thread.current[:discordrb_name] = "#{@current_thread}-temp-msg"

message = send_message(channel, content, tts, embed)
message = send_message(channel, content, tts, embed, allowed_mentions)
sleep(timeout)
message.delete
end
Expand Down
16 changes: 10 additions & 6 deletions lib/discordrb/data/channel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -338,9 +338,10 @@ def slowmode?
# @param content [String] The content to send. Should not be longer than 2000 characters or it will result in an error.
# @param tts [true, false] Whether or not this message should be sent using Discord text-to-speech.
# @param embed [Hash, Discordrb::Webhooks::Embed, nil] The rich embed to append to this message.
# @param allowed_mentions [Hash, nil] [Allowed Mentions object](https://discordapp.com/developers/docs/resources/channel#allowed-mentions-object)
# @return [Message] the message that was sent.
def send_message(content, tts = false, embed = nil)
@bot.send_message(@id, content, tts, embed)
def send_message(content, tts = false, embed = nil, allowed_mentions = nil)
@bot.send_message(@id, content, tts, embed, allowed_mentions)
end

alias_method :send, :send_message
Expand All @@ -350,8 +351,9 @@ def send_message(content, tts = false, embed = nil)
# @param timeout [Float] The amount of time in seconds after which the message sent will be deleted.
# @param tts [true, false] Whether or not this message should be sent using Discord text-to-speech.
# @param embed [Hash, Discordrb::Webhooks::Embed, nil] The rich embed to append to this message.
def send_temporary_message(content, timeout, tts = false, embed = nil)
@bot.send_temporary_message(@id, content, timeout, tts, embed)
# @param allowed_mentions [Hash, nil] [Allowed Mentions object](https://discordapp.com/developers/docs/resources/channel#allowed-mentions-object)
def send_temporary_message(content, timeout, tts = false, embed = nil, allowed_mentions = nil)
@bot.send_temporary_message(@id, content, timeout, tts, embed, allowed_mentions)
end

# Convenience method to send a message with an embed.
Expand All @@ -362,13 +364,15 @@ def send_temporary_message(content, timeout, tts = false, embed = nil)
# end
# @param message [String] The message that should be sent along with the embed. If this is the empty string, only the embed will be shown.
# @param embed [Discordrb::Webhooks::Embed, nil] The embed to start the building process with, or nil if one should be created anew.
# @param tts [true, false] Whether or not this message should be sent using Discord text-to-speech.
# @param allowed_mentions [Hash, nil] [Allowed Mentions object](https://discordapp.com/developers/docs/resources/channel#allowed-mentions-object)
# @yield [embed] Yields the embed to allow for easy building inside a block.
# @yieldparam embed [Discordrb::Webhooks::Embed] The embed from the parameters, or a new one.
# @return [Message] The resulting message.
def send_embed(message = '', embed = nil)
def send_embed(message = '', embed = nil, tts = false, allowed_mentions = nil)
embed ||= Discordrb::Webhooks::Embed.new
yield(embed) if block_given?
send_message(message, false, embed)
send_message(message, tts, embed, allowed_mentions)
end

# Sends multiple messages to a channel
Expand Down
18 changes: 12 additions & 6 deletions lib/discordrb/events/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,33 @@ module Respondable
# @param content [String] The message to send to the channel
# @param tts [true, false] Whether or not this message should be sent using Discord text-to-speech.
# @param embed [Hash, Discordrb::Webhooks::Embed, nil] The rich embed to append to this message.
# @param allowed_mentions [Hash, nil] [Allowed Mentions object](https://discordapp.com/developers/docs/resources/channel#allowed-mentions-object)
# @return [Discordrb::Message] the message that was sent
def send_message(content, tts = false, embed = nil)
channel.send_message(content, tts, embed)
def send_message(content, tts = false, embed = nil, allowed_mentions = nil)
channel.send_message(content, tts, embed, allowed_mentions)
end

# The same as {#send_message}, but yields a {Webhooks::Embed} for easy building of embedded content inside a block.
# @see Channel#send_embed
# @param message [String] The message that should be sent along with the embed. If this is the empty string, only the embed will be shown.
# @param embed [Discordrb::Webhooks::Embed, nil] The embed to start the building process with, or nil if one should be created anew.
# @param tts [true, false] Whether or not this message should be sent using Discord text-to-speech.
# @param allowed_mentions [Hash, nil] [Allowed Mentions object](https://discordapp.com/developers/docs/resources/channel#allowed-mentions-object)
# @yield [embed] Yields the embed to allow for easy building inside a block.
# @yieldparam embed [Discordrb::Webhooks::Embed] The embed from the parameters, or a new one.
# @return [Message] The resulting message.
def send_embed(message = '', embed = nil, &block)
channel.send_embed(message, embed, &block)
def send_embed(message = '', embed = nil, tts = false, allowed_mentions = nil, &block)
channel.send_embed(message, embed, tts, allowed_mentions, &block)
end

# Sends a temporary message to the channel this message was sent in, right now.
# @param content [String] The content to send. Should not be longer than 2000 characters or it will result in an error.
# @param timeout [Float] The amount of time in seconds after which the message sent will be deleted.
def send_temporary_message(content, timeout)
channel.send_temporary_message(content, timeout)
# @param tts [true, false] Whether or not this message should be sent using Discord text-to-speech.
# @param embed [Hash, Discordrb::Webhooks::Embed, nil] The rich embed to append to this message.
# @param allowed_mentions [Hash, nil] [Allowed Mentions object](https://discordapp.com/developers/docs/resources/channel#allowed-mentions-object)
def send_temporary_message(content, timeout, tts = false, embed = nil, allowed_mentions = nil)
channel.send_temporary_message(content, timeout, tts, embed, allowed_mentions)
end

# Adds a string to be sent after the event has finished execution. Avoids problems with rate limiting because only
Expand Down

0 comments on commit ba4e4e7

Please sign in to comment.