Skip to content

Commit

Permalink
Add Discordrb::AllowedMentions builder
Browse files Browse the repository at this point in the history
  • Loading branch information
swarley committed Jul 19, 2020
1 parent a8e2575 commit ebaca68
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 13 deletions.
36 changes: 36 additions & 0 deletions lib/discordrb/allowed_mentions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# frozen_string_literal: true

require 'discordrb/id_object'

module Discordrb
# Builder class for `allowed_mentions` when sending messages.
class AllowedMentions
# @return [Array<"users", "roles", "everyone">, nil]
attr_accessor :parse

# @return [Array<String, Integer>, nil]
attr_accessor :users

# @return [Array<String, Integer>, nil]
attr_accessor :roles

# @param parse [Array<"users", "roles", "everyone">] Mention types that can be inferred from the message.
# `users` and `roles` allow for all mentions of the respective type to ping. `everyone` allows usage of `@everyone` and `@here`
# @param users [Array<User, String, Integer>] Users or user IDs that can be pinged. Cannot be used in conjunction with `"users"` in `parse`
# @param roles [Array<Role, String, Integer>] Roles or role IDs that can be pinged. Cannot be used in conjunction with `"roles"` in `parse`
def initialize(parse: nil, users: nil, roles: nil)
@parse = parse
@users = users
@roles = roles
end

# @!visibility private
def to_hash
{
parse: @parse,
users: @users&.map { |user| user.is_a?(IDObject) ? user.id : user },
roles: @roles&.map { |role| role.is_a?(IDObject) ? role.id : role }
}.compact
end
end
end
4 changes: 0 additions & 4 deletions lib/discordrb/api/channel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,7 @@ def message(token, channel_id, message_id)

# Send a message to a channel
# https://discordapp.com/developers/docs/resources/channel#create-message
<<<<<<< HEAD
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)
>>>>>>> c8f772db... Initial allowed_mentions implementation
Discordrb::API.request(
:channels_cid_messages_mid,
channel_id,
Expand Down
7 changes: 4 additions & 3 deletions lib/discordrb/bot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -363,13 +363,14 @@ 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)
# @param allowed_mentions [Hash, Discordrb::AllowedMentions, false, nil] Mentions that are allowed to ping on this message. `false` disables all pings
# @return [Message] The message that was sent.
def send_message(channel, content, tts = false, embed = nil, allowed_mentions = nil)
channel = channel.resolve_id
debug("Sending message to #{channel} with content '#{content}'")
allowed_mentions = { parse: [] } if allowed_mentions == false

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

Expand All @@ -380,7 +381,7 @@ def send_message(channel, content, tts = false, embed = nil, allowed_mentions =
# @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.
# @param allowed_mentions [Hash, nil] [Allowed Mentions object](https://discordapp.com/developers/docs/resources/channel#allowed-mentions-object)
# @param allowed_mentions [Hash, Discordrb::AllowedMentions, false, nil] Mentions that are allowed to ping on this message. `false` disables all pings
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"
Expand Down
6 changes: 3 additions & 3 deletions lib/discordrb/data/channel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ 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)
# @param allowed_mentions [Hash, Discordrb::AllowedMentions, false, nil] Mentions that are allowed to ping on this message. `false` disables all pings
# @return [Message] the message that was sent.
def send_message(content, tts = false, embed = nil, allowed_mentions = nil)
@bot.send_message(@id, content, tts, embed, allowed_mentions)
Expand All @@ -351,7 +351,7 @@ def send_message(content, tts = false, embed = nil, allowed_mentions = 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.
# @param allowed_mentions [Hash, nil] [Allowed Mentions object](https://discordapp.com/developers/docs/resources/channel#allowed-mentions-object)
# @param allowed_mentions [Hash, Discordrb::AllowedMentions, false, nil] Mentions that are allowed to ping on this message. `false` disables all pings
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
Expand All @@ -365,7 +365,7 @@ def send_temporary_message(content, timeout, tts = false, embed = nil, allowed_m
# @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)
# @param allowed_mentions [Hash, Discordrb::AllowedMentions, false, nil] Mentions that are allowed to ping on this message. `false` disables all pings
# @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.
Expand Down
6 changes: 3 additions & 3 deletions lib/discordrb/events/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ 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)
# @param allowed_mentions [Hash, Discordrb::AllowedMentions, false, nil] Mentions that are allowed to ping on this message. `false` disables all pings
# @return [Discordrb::Message] the message that was sent
def send_message(content, tts = false, embed = nil, allowed_mentions = nil)
channel.send_message(content, tts, embed, allowed_mentions)
Expand All @@ -25,7 +25,7 @@ def send_message(content, tts = false, embed = nil, allowed_mentions = nil)
# @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)
# @param allowed_mentions [Hash, Discordrb::AllowedMentions, false, nil] Mentions that are allowed to ping on this message. `false` disables all pings
# @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.
Expand All @@ -38,7 +38,7 @@ def send_embed(message = '', embed = nil, tts = false, allowed_mentions = 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.
# @param allowed_mentions [Hash, nil] [Allowed Mentions object](https://discordapp.com/developers/docs/resources/channel#allowed-mentions-object)
# @param allowed_mentions [Hash, Discordrb::AllowedMentions, false, nil] Mentions that are allowed to ping on this message. `false` disables all pings
def send_temporary_message(content, timeout, tts = false, embed = nil, allowed_mentions = nil)
channel.send_temporary_message(content, timeout, tts, embed, allowed_mentions)
end
Expand Down

0 comments on commit ebaca68

Please sign in to comment.