onyxcord 2.0.13 → 3.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/onyxcord/application_commands/command.rb +4 -97
- data/lib/onyxcord/application_commands/context.rb +4 -94
- data/lib/onyxcord/application_commands/option.rb +4 -75
- data/lib/onyxcord/application_commands/registry.rb +4 -45
- data/lib/onyxcord/application_commands.rb +8 -5
- data/lib/onyxcord/bot.rb +24 -1759
- data/lib/onyxcord/{cache.rb → cache/manager.rb} +16 -14
- data/lib/onyxcord/cache/stores/gateway.rb +272 -0
- data/lib/onyxcord/commands/bot/channels.rb +48 -0
- data/lib/onyxcord/commands/bot/execution.rb +174 -0
- data/lib/onyxcord/commands/bot/message_dispatch.rb +58 -0
- data/lib/onyxcord/commands/bot/permissions.rb +72 -0
- data/lib/onyxcord/commands/bot.rb +209 -0
- data/lib/onyxcord/commands/container.rb +21 -2
- data/lib/onyxcord/commands/parser.rb +30 -1
- data/lib/onyxcord/container.rb +16 -960
- data/lib/onyxcord/core/bot/application_commands.rb +210 -0
- data/lib/onyxcord/core/bot/awaits.rb +112 -0
- data/lib/onyxcord/core/bot/invites.rb +42 -0
- data/lib/onyxcord/core/bot/messaging.rb +161 -0
- data/lib/onyxcord/core/bot/oauth.rb +27 -0
- data/lib/onyxcord/core/bot/presence.rb +103 -0
- data/lib/onyxcord/core/bot/runtime.rb +183 -0
- data/lib/onyxcord/core/bot/voice.rb +69 -0
- data/lib/onyxcord/{errors.rb → core/errors.rb} +18 -3
- data/lib/onyxcord/{logger.rb → core/logger.rb} +5 -0
- data/lib/onyxcord/core/version.rb +5 -0
- data/lib/onyxcord/events/await/base.rb +48 -0
- data/lib/onyxcord/events/await.rb +1 -46
- data/lib/onyxcord/events/ban/base.rb +54 -0
- data/lib/onyxcord/events/ban/remove.rb +11 -0
- data/lib/onyxcord/events/bans.rb +2 -58
- data/lib/onyxcord/events/channels/base.rb +91 -0
- data/lib/onyxcord/events/channels/delete.rb +73 -0
- data/lib/onyxcord/events/channels/pins.rb +44 -0
- data/lib/onyxcord/events/channels/recipients.rb +77 -0
- data/lib/onyxcord/events/channels.rb +4 -239
- data/lib/onyxcord/events/guilds/audit_logs.rb +79 -0
- data/lib/onyxcord/events/guilds/base.rb +97 -0
- data/lib/onyxcord/events/guilds/emoji.rb +147 -0
- data/lib/onyxcord/events/guilds.rb +3 -267
- data/lib/onyxcord/events/handlers/channels.rb +133 -0
- data/lib/onyxcord/events/handlers/core.rb +146 -0
- data/lib/onyxcord/events/handlers/guilds.rb +375 -0
- data/lib/onyxcord/events/handlers/interactions.rb +238 -0
- data/lib/onyxcord/events/handlers/lifetime.rb +71 -0
- data/lib/onyxcord/events/handlers/messages.rb +218 -0
- data/lib/onyxcord/events/handlers/presence.rb +56 -0
- data/lib/onyxcord/events/handlers/raw.rb +39 -0
- data/lib/onyxcord/events/handlers/reactions.rb +98 -0
- data/lib/onyxcord/events/handlers/scheduled_events.rb +243 -0
- data/lib/onyxcord/events/handlers/voice.rb +46 -0
- data/lib/onyxcord/events/integration/base.rb +45 -0
- data/lib/onyxcord/events/integration/create.rb +11 -0
- data/lib/onyxcord/events/integration/delete.rb +48 -0
- data/lib/onyxcord/events/integration/update.rb +11 -0
- data/lib/onyxcord/events/integrations.rb +4 -98
- data/lib/onyxcord/events/interactions/application_commands.rb +185 -0
- data/lib/onyxcord/events/interactions/autocomplete.rb +62 -0
- data/lib/onyxcord/events/interactions/base.rb +175 -0
- data/lib/onyxcord/events/interactions/components.rb +221 -0
- data/lib/onyxcord/events/interactions/permissions.rb +53 -0
- data/lib/onyxcord/events/interactions.rb +5 -622
- data/lib/onyxcord/events/invite/create.rb +79 -0
- data/lib/onyxcord/events/invite/delete.rb +55 -0
- data/lib/onyxcord/events/invites.rb +2 -125
- data/lib/onyxcord/events/lifetime/base.rb +31 -0
- data/lib/onyxcord/events/lifetime.rb +1 -29
- data/lib/onyxcord/events/member/add.rb +12 -0
- data/lib/onyxcord/events/member/base.rb +67 -0
- data/lib/onyxcord/events/member/delete.rb +21 -0
- data/lib/onyxcord/events/member/update.rb +25 -0
- data/lib/onyxcord/events/members.rb +4 -108
- data/lib/onyxcord/events/message/base.rb +295 -0
- data/lib/onyxcord/events/message/ids.rb +71 -0
- data/lib/onyxcord/events/message/mentions.rb +55 -0
- data/lib/onyxcord/events/message/updates.rb +27 -0
- data/lib/onyxcord/events/message.rb +4 -397
- data/lib/onyxcord/events/poll/add.rb +11 -0
- data/lib/onyxcord/events/poll/base.rb +106 -0
- data/lib/onyxcord/events/poll/remove.rb +11 -0
- data/lib/onyxcord/events/polls.rb +3 -116
- data/lib/onyxcord/events/presence/playing.rb +77 -0
- data/lib/onyxcord/events/presence/status.rb +60 -0
- data/lib/onyxcord/events/presence.rb +2 -129
- data/lib/onyxcord/events/raw/base.rb +74 -0
- data/lib/onyxcord/events/raw.rb +1 -72
- data/lib/onyxcord/events/reactions/base.rb +143 -0
- data/lib/onyxcord/events/reactions/clear.rb +99 -0
- data/lib/onyxcord/events/reactions.rb +2 -216
- data/lib/onyxcord/events/role/create.rb +46 -0
- data/lib/onyxcord/events/role/delete.rb +40 -0
- data/lib/onyxcord/events/role/update.rb +11 -0
- data/lib/onyxcord/events/roles.rb +3 -85
- data/lib/onyxcord/events/scheduled_events/events.rb +112 -0
- data/lib/onyxcord/events/scheduled_events/users.rb +91 -0
- data/lib/onyxcord/events/scheduled_events.rb +2 -169
- data/lib/onyxcord/events/thread/base.rb +49 -0
- data/lib/onyxcord/events/thread/members.rb +44 -0
- data/lib/onyxcord/events/thread/update.rb +18 -0
- data/lib/onyxcord/events/threads.rb +3 -98
- data/lib/onyxcord/events/typing/base.rb +73 -0
- data/lib/onyxcord/events/typing.rb +1 -71
- data/lib/onyxcord/events/voice/server_update.rb +48 -0
- data/lib/onyxcord/events/voice/state_update.rb +137 -0
- data/lib/onyxcord/events/voice_server_update.rb +2 -46
- data/lib/onyxcord/events/voice_state_update.rb +2 -135
- data/lib/onyxcord/events/webhook/update.rb +65 -0
- data/lib/onyxcord/events/webhooks.rb +1 -63
- data/lib/onyxcord/gateway/client.rb +460 -0
- data/lib/onyxcord/interactions/command.rb +120 -0
- data/lib/onyxcord/interactions/context.rb +109 -0
- data/lib/onyxcord/interactions/internal/application_command.rb +203 -0
- data/lib/onyxcord/interactions/internal/message.rb +152 -0
- data/lib/onyxcord/interactions/internal/metadata.rb +109 -0
- data/lib/onyxcord/interactions/internal/option_builder.rb +230 -0
- data/lib/onyxcord/interactions/internal/permission_builder.rb +90 -0
- data/lib/onyxcord/interactions/option.rb +88 -0
- data/lib/onyxcord/interactions/registry.rb +50 -0
- data/lib/onyxcord/interactions.rb +16 -0
- data/lib/onyxcord/internal/async_runtime.rb +32 -0
- data/lib/onyxcord/internal/event_bus.rb +591 -0
- data/lib/onyxcord/internal/event_executor.rb +155 -0
- data/lib/onyxcord/internal/gateway/opcodes.rb +22 -0
- data/lib/onyxcord/internal/gateway/session.rb +45 -0
- data/lib/onyxcord/internal/http.rb +189 -0
- data/lib/onyxcord/internal/json.rb +51 -0
- data/lib/onyxcord/internal/message_payload.rb +78 -0
- data/lib/onyxcord/internal/rate_limiter/async_rest.rb +151 -0
- data/lib/onyxcord/internal/rate_limiter/gateway.rb +44 -0
- data/lib/onyxcord/internal/rate_limiter/rest.rb +144 -0
- data/lib/onyxcord/internal/upload.rb +39 -0
- data/lib/onyxcord/internal/websocket.rb +79 -0
- data/lib/onyxcord/light/data.rb +1 -1
- data/lib/onyxcord/light/integrations.rb +1 -1
- data/lib/onyxcord/light/light_bot.rb +7 -7
- data/lib/onyxcord/{data → models}/activity.rb +2 -2
- data/lib/onyxcord/{data → models}/application.rb +3 -3
- data/lib/onyxcord/{data → models}/avatar_decoration.rb +1 -1
- data/lib/onyxcord/{data → models}/channel.rb +23 -23
- data/lib/onyxcord/{data → models}/collectibles.rb +1 -1
- data/lib/onyxcord/{data → models}/emoji.rb +1 -1
- data/lib/onyxcord/models/interaction.rb +450 -0
- data/lib/onyxcord/{data → models}/invite.rb +1 -1
- data/lib/onyxcord/{data → models}/member.rb +11 -11
- data/lib/onyxcord/{data → models}/message.rb +22 -14
- data/lib/onyxcord/{data → models}/poll.rb +2 -2
- data/lib/onyxcord/{data → models}/primary_server.rb +1 -1
- data/lib/onyxcord/{data → models}/profile.rb +4 -4
- data/lib/onyxcord/{data → models}/role.rb +6 -12
- data/lib/onyxcord/{data → models}/scheduled_event.rb +5 -5
- data/lib/onyxcord/{data → models}/server.rb +41 -41
- data/lib/onyxcord/{data → models}/server_preview.rb +2 -2
- data/lib/onyxcord/{data → models}/team.rb +1 -1
- data/lib/onyxcord/{data → models}/user.rb +5 -5
- data/lib/onyxcord/{data → models}/webhook.rb +13 -11
- data/lib/onyxcord/models.rb +57 -0
- data/lib/onyxcord/{api.rb → rest/client.rb} +83 -27
- data/lib/onyxcord/{api → rest/routes}/application.rb +79 -57
- data/lib/onyxcord/rest/routes/channel/base.rb +73 -0
- data/lib/onyxcord/rest/routes/channel/groups.rb +78 -0
- data/lib/onyxcord/rest/routes/channel/messages.rb +130 -0
- data/lib/onyxcord/rest/routes/channel/permissions.rb +73 -0
- data/lib/onyxcord/rest/routes/channel/pins.rb +45 -0
- data/lib/onyxcord/rest/routes/channel/polls.rb +32 -0
- data/lib/onyxcord/rest/routes/channel/reactions.rb +86 -0
- data/lib/onyxcord/rest/routes/channel/threads.rb +176 -0
- data/lib/onyxcord/rest/routes/channel/webhooks.rb +47 -0
- data/lib/onyxcord/rest/routes/channel.rb +12 -0
- data/lib/onyxcord/{api/interaction.rb → rest/routes/interaction/base.rb} +10 -33
- data/lib/onyxcord/rest/routes/interaction/response.rb +24 -0
- data/lib/onyxcord/rest/routes/interaction.rb +4 -0
- data/lib/onyxcord/{api → rest/routes}/invite.rb +7 -7
- data/lib/onyxcord/rest/routes/server/admin.rb +174 -0
- data/lib/onyxcord/rest/routes/server/assets.rb +25 -0
- data/lib/onyxcord/rest/routes/server/bans.rb +67 -0
- data/lib/onyxcord/rest/routes/server/base.rb +77 -0
- data/lib/onyxcord/rest/routes/server/channels.rb +58 -0
- data/lib/onyxcord/rest/routes/server/emoji.rb +48 -0
- data/lib/onyxcord/rest/routes/server/members.rb +109 -0
- data/lib/onyxcord/rest/routes/server/messages.rb +19 -0
- data/lib/onyxcord/rest/routes/server/roles.rb +156 -0
- data/lib/onyxcord/rest/routes/server/scheduled_events.rb +86 -0
- data/lib/onyxcord/rest/routes/server.rb +12 -0
- data/lib/onyxcord/{api → rest/routes}/user.rb +23 -23
- data/lib/onyxcord/{api → rest/routes}/webhook.rb +30 -32
- data/lib/onyxcord/{allowed_mentions.rb → utils/allowed_mentions.rb} +9 -1
- data/lib/onyxcord/utils/timestamp.rb +69 -0
- data/lib/onyxcord/voice/{voice_bot.rb → client.rb} +4 -4
- data/lib/onyxcord/voice/encoder.rb +1 -1
- data/lib/onyxcord/voice/network/udp.rb +135 -0
- data/lib/onyxcord/voice/network/websocket.rb +225 -0
- data/lib/onyxcord/voice/network.rb +5 -357
- data/lib/onyxcord/webhooks/builder.rb +1 -1
- data/lib/onyxcord/webhooks/client.rb +13 -13
- data/lib/onyxcord/webhooks/modal/group_builder.rb +55 -0
- data/lib/onyxcord/webhooks/modal/label_builder.rb +183 -0
- data/lib/onyxcord/webhooks/modal.rb +2 -239
- data/lib/onyxcord/webhooks/version.rb +3 -3
- data/lib/onyxcord/webhooks/view/container_builder.rb +80 -0
- data/lib/onyxcord/webhooks/view/file_builder.rb +24 -0
- data/lib/onyxcord/webhooks/view/media_gallery_builder.rb +50 -0
- data/lib/onyxcord/webhooks/view/row_builder.rb +120 -0
- data/lib/onyxcord/webhooks/view/section_builder.rb +61 -0
- data/lib/onyxcord/webhooks/view/select_menu_builder.rb +77 -0
- data/lib/onyxcord/webhooks/view/separator_builder.rb +22 -0
- data/lib/onyxcord/webhooks/view/text_display_builder.rb +20 -0
- data/lib/onyxcord/webhooks/view.rb +9 -425
- data/lib/onyxcord.rb +106 -40
- metadata +208 -91
- data/.github/CONTRIBUTING.md +0 -13
- data/.github/ISSUE_TEMPLATE/bug_report.md +0 -38
- data/.github/ISSUE_TEMPLATE/feature_request.md +0 -24
- data/.github/pull_request_template.md +0 -37
- data/.github/workflows/ci.yml +0 -78
- data/.github/workflows/codeql.yml +0 -65
- data/.github/workflows/deploy.yml +0 -54
- data/.github/workflows/release.yml +0 -51
- data/.gitignore +0 -17
- data/.markdownlint.json +0 -4
- data/.overcommit.yml +0 -7
- data/.rspec +0 -2
- data/.rubocop.yml +0 -129
- data/.yardopts +0 -1
- data/CHANGELOG.md +0 -202
- data/Gemfile +0 -6
- data/LICENSE.txt +0 -21
- data/README.md +0 -448
- data/Rakefile +0 -13
- data/lib/onyxcord/api/channel.rb +0 -705
- data/lib/onyxcord/api/server.rb +0 -775
- data/lib/onyxcord/async/runtime.rb +0 -30
- data/lib/onyxcord/commands/command_bot.rb +0 -505
- data/lib/onyxcord/data/interaction.rb +0 -1143
- data/lib/onyxcord/data.rb +0 -57
- data/lib/onyxcord/event_executor.rb +0 -153
- data/lib/onyxcord/gateway.rb +0 -500
- data/lib/onyxcord/http.rb +0 -115
- data/lib/onyxcord/json.rb +0 -49
- data/lib/onyxcord/rate_limiter/async_rest.rb +0 -149
- data/lib/onyxcord/rate_limiter/gateway.rb +0 -42
- data/lib/onyxcord/rate_limiter/rest.rb +0 -142
- data/lib/onyxcord/version.rb +0 -7
- data/lib/onyxcord/websocket.rb +0 -77
- data/onyxcord.gemspec +0 -63
- /data/lib/onyxcord/{configuration.rb → core/configuration.rb} +0 -0
- /data/lib/onyxcord/{data → models}/attachment.rb +0 -0
- /data/lib/onyxcord/{data → models}/audit_logs.rb +0 -0
- /data/lib/onyxcord/{data → models}/call.rb +0 -0
- /data/lib/onyxcord/{data → models}/channel_tag.rb +0 -0
- /data/lib/onyxcord/{data → models}/component.rb +0 -0
- /data/lib/onyxcord/{data → models}/embed.rb +0 -0
- /data/lib/onyxcord/{data → models}/install_params.rb +0 -0
- /data/lib/onyxcord/{data → models}/integration.rb +0 -0
- /data/lib/onyxcord/{data → models}/message_activity.rb +0 -0
- /data/lib/onyxcord/{data → models}/overwrite.rb +0 -0
- /data/lib/onyxcord/{data → models}/reaction.rb +0 -0
- /data/lib/onyxcord/{data → models}/recipient.rb +0 -0
- /data/lib/onyxcord/{data → models}/role_connection_data.rb +0 -0
- /data/lib/onyxcord/{data → models}/role_subscription.rb +0 -0
- /data/lib/onyxcord/{data → models}/snapshot.rb +0 -0
- /data/lib/onyxcord/{data → models}/timestamp.rb +0 -0
- /data/lib/onyxcord/{data → models}/voice_region.rb +0 -0
- /data/lib/onyxcord/{data → models}/voice_state.rb +0 -0
- /data/lib/onyxcord/{colour_rgb.rb → utils/colour_rgb.rb} +0 -0
- /data/lib/onyxcord/{id_object.rb → utils/id_object.rb} +0 -0
- /data/lib/onyxcord/{message_components.rb → utils/message_components.rb} +0 -0
- /data/lib/onyxcord/{paginator.rb → utils/paginator.rb} +0 -0
- /data/lib/onyxcord/{permissions.rb → utils/permissions.rb} +0 -0
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module OnyxCord
|
|
4
|
+
class Bot
|
|
5
|
+
module ApplicationCommands
|
|
6
|
+
# Get all application commands.
|
|
7
|
+
# @param server_id [String, Integer, nil] The ID of the server to get the commands from. Global if `nil`.
|
|
8
|
+
# @return [Array<ApplicationCommand>]
|
|
9
|
+
def get_application_commands(server_id: nil)
|
|
10
|
+
resp = if server_id
|
|
11
|
+
REST::Application.get_guild_commands(@token, profile.id, server_id)
|
|
12
|
+
else
|
|
13
|
+
REST::Application.get_global_commands(@token, profile.id)
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
JSON.parse(resp).map do |command_data|
|
|
17
|
+
ApplicationCommand.new(command_data, self, server_id)
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
# Get an application command by ID.
|
|
22
|
+
# @param command_id [String, Integer]
|
|
23
|
+
# @param server_id [String, Integer, nil] The ID of the server to get the command from. Global if `nil`.
|
|
24
|
+
def get_application_command(command_id, server_id: nil)
|
|
25
|
+
resp = if server_id
|
|
26
|
+
REST::Application.get_guild_command(@token, profile.id, server_id, command_id)
|
|
27
|
+
else
|
|
28
|
+
REST::Application.get_global_command(@token, profile.id, command_id)
|
|
29
|
+
end
|
|
30
|
+
ApplicationCommand.new(JSON.parse(resp), self, server_id)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
# @return [OnyxCord::Interactions::Registry]
|
|
34
|
+
def commands
|
|
35
|
+
@commands ||= ApplicationCommands::Registry.new(self)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def slash(name, description: nil, **attributes, &block)
|
|
39
|
+
commands.slash(name, description: description, **attributes, &block)
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def user_command(name, **attributes, &block)
|
|
43
|
+
commands.user(name, **attributes, &block)
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def message_command(name, **attributes, &block)
|
|
47
|
+
commands.message(name, **attributes, &block)
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def sync_application_commands!(server_id: nil, delete_unknown: false)
|
|
51
|
+
commands.sync!(server_id: server_id, delete_unknown: delete_unknown)
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def bulk_overwrite_global_application_commands(commands)
|
|
55
|
+
response = REST::Application.bulk_overwrite_global_commands(@token, profile.id, commands)
|
|
56
|
+
JSON.parse(response).map { |data| ApplicationCommand.new(data, self) }
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def bulk_overwrite_guild_application_commands(server_id, commands)
|
|
60
|
+
response = REST::Application.bulk_overwrite_guild_commands(@token, profile.id, server_id.resolve_id, commands)
|
|
61
|
+
JSON.parse(response).map { |data| ApplicationCommand.new(data, self, server_id) }
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
# @yieldparam [OptionBuilder]
|
|
65
|
+
# @yieldparam [PermissionBuilder]
|
|
66
|
+
# @example
|
|
67
|
+
# bot.register_application_command(:reddit, 'Reddit Commands') do |cmd|
|
|
68
|
+
# cmd.subcommand_group(:subreddit, 'Subreddit Commands') do |group|
|
|
69
|
+
# group.subcommand(:hot, "What's trending") do |sub|
|
|
70
|
+
# sub.string(:subreddit, 'Subreddit to search')
|
|
71
|
+
# end
|
|
72
|
+
# group.subcommand(:new, "What's new") do |sub|
|
|
73
|
+
# sub.string(:since, 'How long ago', choices: ['this hour', 'today', 'this week', 'this month', 'this year', 'all time'])
|
|
74
|
+
# sub.string(:subreddit, 'Subreddit to search')
|
|
75
|
+
# end
|
|
76
|
+
# end
|
|
77
|
+
# end
|
|
78
|
+
def register_application_command(name, description, server_id: nil, default_permission: nil, type: :chat_input, default_member_permissions: nil, contexts: nil, nsfw: false, integration_types: nil)
|
|
79
|
+
type = ApplicationCommand::TYPES[type] || type
|
|
80
|
+
|
|
81
|
+
contexts = contexts&.map { |context| Interaction::CONTEXTS[context] || context }
|
|
82
|
+
integration_types = integration_types&.map { |type| Interaction::INTEGRATION_TYPES[type] || type }
|
|
83
|
+
default_member_permissions = Permissions.bits(default_member_permissions) if default_member_permissions.is_a?(Array)
|
|
84
|
+
|
|
85
|
+
builder = Interactions::OptionBuilder.new
|
|
86
|
+
permission_builder = Interactions::PermissionBuilder.new
|
|
87
|
+
yield(builder, permission_builder) if block_given?
|
|
88
|
+
|
|
89
|
+
resp = if server_id
|
|
90
|
+
REST::Application.create_guild_command(@token, profile.id, server_id, name, description, builder.to_a, default_permission, type, default_member_permissions&.to_s, contexts, nsfw)
|
|
91
|
+
else
|
|
92
|
+
REST::Application.create_global_command(@token, profile.id, name, description, builder.to_a, default_permission, type, default_member_permissions&.to_s, contexts, nsfw, integration_types)
|
|
93
|
+
end
|
|
94
|
+
cmd = ApplicationCommand.new(JSON.parse(resp), self, server_id)
|
|
95
|
+
|
|
96
|
+
if permission_builder.to_a.any?
|
|
97
|
+
raise ArgumentError, 'Permissions can only be set for guild commands' unless server_id
|
|
98
|
+
|
|
99
|
+
edit_application_command_permissions(cmd.id, server_id, permission_builder.to_a)
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
cmd
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
# @yieldparam [OptionBuilder]
|
|
106
|
+
# @yieldparam [PermissionBuilder]
|
|
107
|
+
def edit_application_command(command_id, server_id: nil, name: nil, description: nil, default_permission: nil, type: :chat_input, default_member_permissions: nil, contexts: nil, nsfw: nil, integration_types: nil)
|
|
108
|
+
type = ApplicationCommand::TYPES[type] || type
|
|
109
|
+
|
|
110
|
+
contexts = contexts&.map { |context| Interaction::CONTEXTS[context] || context }
|
|
111
|
+
integration_types = integration_types&.map { |type| Interaction::INTEGRATION_TYPES[type] || type }
|
|
112
|
+
default_member_permissions = Permissions.bits(default_member_permissions) if default_member_permissions.is_a?(Array)
|
|
113
|
+
|
|
114
|
+
builder = Interactions::OptionBuilder.new
|
|
115
|
+
permission_builder = Interactions::PermissionBuilder.new
|
|
116
|
+
|
|
117
|
+
yield(builder, permission_builder) if block_given?
|
|
118
|
+
|
|
119
|
+
resp = if server_id
|
|
120
|
+
REST::Application.edit_guild_command(@token, profile.id, server_id, command_id, name, description, builder.to_a, default_permission, type, default_member_permissions&.to_s, contexts, nsfw)
|
|
121
|
+
else
|
|
122
|
+
REST::Application.edit_global_command(@token, profile.id, command_id, name, description, builder.to_a, default_permission, type, default_member_permissions&.to_s, contexts, nsfw, integration_types)
|
|
123
|
+
end
|
|
124
|
+
cmd = ApplicationCommand.new(JSON.parse(resp), self, server_id)
|
|
125
|
+
|
|
126
|
+
if permission_builder.to_a.any?
|
|
127
|
+
raise ArgumentError, 'Permissions can only be set for guild commands' unless server_id
|
|
128
|
+
|
|
129
|
+
edit_application_command_permissions(cmd.id, server_id, permission_builder.to_a)
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
cmd
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
# Remove an application command from the commands registered with discord.
|
|
136
|
+
# @param command_id [String, Integer] The ID of the command to remove.
|
|
137
|
+
# @param server_id [String, Integer] The ID of the server to delete this command from, global if `nil`.
|
|
138
|
+
def delete_application_command(command_id, server_id: nil)
|
|
139
|
+
if server_id
|
|
140
|
+
REST::Application.delete_guild_command(@token, profile.id, server_id, command_id)
|
|
141
|
+
else
|
|
142
|
+
REST::Application.delete_global_command(@token, profile.id, command_id)
|
|
143
|
+
end
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
# @param command_id [Integer, String]
|
|
147
|
+
# @param server_id [Integer, String]
|
|
148
|
+
# @param permissions [Array<Hash>] An array of objects formatted as `{ id: ENTITY_ID, type: 1 or 2, permission: true or false }`
|
|
149
|
+
# @param bearer_token [String] A valid bearer token that has permission to manage the server and its roles.
|
|
150
|
+
def edit_application_command_permissions(command_id, server_id, permissions = [], bearer_token = nil)
|
|
151
|
+
builder = Interactions::PermissionBuilder.new
|
|
152
|
+
yield builder if block_given?
|
|
153
|
+
|
|
154
|
+
raise ArgumentError, 'This method requires a valid bearer token to be provided' unless bearer_token
|
|
155
|
+
|
|
156
|
+
permissions += builder.to_a
|
|
157
|
+
bearer_token = "Bearer #{bearer_token.delete_prefix('Bearer ')}"
|
|
158
|
+
REST::Application.edit_guild_command_permissions(bearer_token, profile.id, server_id, command_id, permissions)
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
# Get the permissions for all of the application commands in a specific server.
|
|
162
|
+
# @param server_id [Integer, String, nil] The ID of the server to fetch application command permissions for.
|
|
163
|
+
# @return [Array<ApplicationCommand::Permission>] The permissions for all of the application commands in the given server.
|
|
164
|
+
def application_command_permissions(server_id:)
|
|
165
|
+
response = REST::Application.get_guild_application_command_permissions(@token, profile.id, server_id.resolve_id)
|
|
166
|
+
JSON.parse(response).flat_map { |data| data['permissions'].map { |inner| ApplicationCommand::Permission.new(inner, data, self) } }
|
|
167
|
+
end
|
|
168
|
+
|
|
169
|
+
# Fetches all the application emojis that the bot can use.
|
|
170
|
+
# @return [Array<Emoji>] Returns an array of emoji objects.
|
|
171
|
+
def application_emojis
|
|
172
|
+
response = REST::Application.list_application_emojis(@token, profile.id)
|
|
173
|
+
JSON.parse(response)['items'].map { |emoji| Emoji.new(emoji, self) }
|
|
174
|
+
end
|
|
175
|
+
|
|
176
|
+
# Fetches a single application emoji from its ID.
|
|
177
|
+
# @param emoji_id [Integer, String] ID of the application emoji.
|
|
178
|
+
# @return [Emoji] The application emoji.
|
|
179
|
+
def application_emoji(emoji_id)
|
|
180
|
+
response = REST::Application.get_application_emoji(@token, profile.id, emoji_id.resolve_id)
|
|
181
|
+
Emoji.new(JSON.parse(response), self)
|
|
182
|
+
end
|
|
183
|
+
|
|
184
|
+
# Creates a new custom emoji that can be used by this application.
|
|
185
|
+
# @param name [String] The name of emoji to create.
|
|
186
|
+
# @param image [String, #read] Base64 string with the image data, or an object that responds to #read.
|
|
187
|
+
# @return [Emoji] The emoji that has been created.
|
|
188
|
+
def create_application_emoji(name:, image:)
|
|
189
|
+
image = image.respond_to?(:read) ? OnyxCord.encode64(image) : image
|
|
190
|
+
response = REST::Application.create_application_emoji(@token, profile.id, name, image)
|
|
191
|
+
Emoji.new(JSON.parse(response), self)
|
|
192
|
+
end
|
|
193
|
+
|
|
194
|
+
# Edits an existing application emoji.
|
|
195
|
+
# @param emoji_id [Integer, String, Emoji] ID of the application emoji to edit.
|
|
196
|
+
# @param name [String] The new name of the emoji.
|
|
197
|
+
# @return [Emoji] Returns the updated emoji object on success.
|
|
198
|
+
def edit_application_emoji(emoji_id, name:)
|
|
199
|
+
response = REST::Application.edit_application_emoji(@token, profile.id, emoji_id.resolve_id, name)
|
|
200
|
+
Emoji.new(JSON.parse(response), self)
|
|
201
|
+
end
|
|
202
|
+
|
|
203
|
+
# Deletes an existing application emoji.
|
|
204
|
+
# @param emoji_id [Integer, String, Emoji] ID of the application emoji to delete.
|
|
205
|
+
def delete_application_emoji(emoji_id)
|
|
206
|
+
REST::Application.delete_application_emoji(@token, profile.id, emoji_id.resolve_id)
|
|
207
|
+
end
|
|
208
|
+
end
|
|
209
|
+
end
|
|
210
|
+
end
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module OnyxCord
|
|
4
|
+
class Bot
|
|
5
|
+
module Awaits
|
|
6
|
+
# Add an await the bot should listen to. For information on awaits, see {Await}.
|
|
7
|
+
# @param key [Symbol] The key that uniquely identifies the await for {AwaitEvent}s to listen to (see {#await}).
|
|
8
|
+
# @param type [Class] The event class that should be listened for.
|
|
9
|
+
# @param attributes [Hash] The attributes the event should check for. The block will only be executed if all attributes match.
|
|
10
|
+
# @yield Is executed when the await is triggered.
|
|
11
|
+
# @yieldparam event [Event] The event object that was triggered.
|
|
12
|
+
# @return [Await] The await that was created.
|
|
13
|
+
# @deprecated Will be changed to blocking behavior in v4.0. Use {#add_await!} instead.
|
|
14
|
+
def add_await(key, type, attributes = {}, &block)
|
|
15
|
+
raise "You can't await an AwaitEvent!" if type == OnyxCord::Events::AwaitEvent
|
|
16
|
+
|
|
17
|
+
await = Await.new(self, key, type, attributes, block)
|
|
18
|
+
@awaits ||= {}
|
|
19
|
+
@awaits[key] = await
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
# Awaits an event, blocking the current thread until a response is received.
|
|
23
|
+
# @param type [Class] The event class that should be listened for.
|
|
24
|
+
# @option attributes [Numeric] :timeout the amount of time (in seconds) to wait for a response before returning `nil`. Waits forever if omitted.
|
|
25
|
+
# @yield Executed when a matching event is received.
|
|
26
|
+
# @yieldparam event [Event] The event object that was triggered.
|
|
27
|
+
# @yieldreturn [true, false] Whether the event matches extra await criteria described by the block
|
|
28
|
+
# @return [Event, nil] The event object that was triggered, or `nil` if a `timeout` was set and no event was raised in time.
|
|
29
|
+
# @raise [ArgumentError] if `timeout` is given and is not a positive numeric value
|
|
30
|
+
def add_await!(type, attributes = {})
|
|
31
|
+
raise "You can't await an AwaitEvent!" if type == OnyxCord::Events::AwaitEvent
|
|
32
|
+
|
|
33
|
+
timeout = attributes[:timeout]
|
|
34
|
+
raise ArgumentError, 'Timeout must be a number > 0' if timeout.is_a?(Numeric) && !timeout.positive?
|
|
35
|
+
|
|
36
|
+
mutex = Mutex.new
|
|
37
|
+
cv = ConditionVariable.new
|
|
38
|
+
response = nil
|
|
39
|
+
block = lambda do |event|
|
|
40
|
+
mutex.synchronize do
|
|
41
|
+
response = event
|
|
42
|
+
if block_given?
|
|
43
|
+
result = yield(event)
|
|
44
|
+
cv.signal if result.is_a?(TrueClass)
|
|
45
|
+
else
|
|
46
|
+
cv.signal
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
handler = register_event(type, attributes, block)
|
|
52
|
+
|
|
53
|
+
if timeout
|
|
54
|
+
Thread.new do
|
|
55
|
+
sleep timeout
|
|
56
|
+
mutex.synchronize { cv.signal }
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
mutex.synchronize { cv.wait(mutex) }
|
|
61
|
+
|
|
62
|
+
remove_handler(handler)
|
|
63
|
+
raise 'ConditionVariable was signaled without returning an event!' if response.nil? && timeout.nil?
|
|
64
|
+
|
|
65
|
+
response
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
# Add a user to the list of ignored users. Those users will be ignored in message events at event processing level.
|
|
69
|
+
# @note Ignoring a user only prevents any message events (including mentions, commands etc.) from them! Typing and
|
|
70
|
+
# presence and any other events will still be received.
|
|
71
|
+
# @param user [User, String, Integer] The user, or its ID, to be ignored.
|
|
72
|
+
def ignore_user(user)
|
|
73
|
+
@ignored_ids << user.resolve_id
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
# Remove a user from the ignore list.
|
|
77
|
+
# @param user [User, String, Integer] The user, or its ID, to be unignored.
|
|
78
|
+
def unignore_user(user)
|
|
79
|
+
@ignored_ids.delete(user.resolve_id)
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
# Checks whether a user is being ignored.
|
|
83
|
+
# @param user [User, String, Integer] The user, or its ID, to check.
|
|
84
|
+
# @return [true, false] whether or not the user is ignored.
|
|
85
|
+
def ignored?(user)
|
|
86
|
+
@ignored_ids.include?(user.resolve_id)
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
# @see Logger#debug
|
|
90
|
+
def debug(message)
|
|
91
|
+
LOGGER.debug(message)
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
# @see Logger#log_exception
|
|
95
|
+
def log_exception(e)
|
|
96
|
+
LOGGER.log_exception(e)
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
# Dispatches an event to this bot. Called by the gateway connection handler used internally.
|
|
100
|
+
def dispatch(type, data = nil)
|
|
101
|
+
return dispatch_packet(type) if data.nil? && type.is_a?(Hash)
|
|
102
|
+
|
|
103
|
+
handle_dispatch(type, data)
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
# Raises a heartbeat event. Called by the gateway connection handler used internally.
|
|
107
|
+
def raise_heartbeat_event
|
|
108
|
+
raise_event(HeartbeatEvent.new(self))
|
|
109
|
+
end
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
end
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module OnyxCord
|
|
4
|
+
class Bot
|
|
5
|
+
module Invites
|
|
6
|
+
# Makes the bot join an invite to a server.
|
|
7
|
+
# @param invite [String, Invite] The invite to join. For possible formats see {#resolve_invite_code}.
|
|
8
|
+
def accept_invite(invite)
|
|
9
|
+
resolved = invite(invite).code
|
|
10
|
+
REST::Invite.accept(token, resolved)
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
# Creates an OAuth invite URL that can be used to invite this bot to a particular server.
|
|
14
|
+
# @param server [Server, nil] The server the bot should be invited to, or nil if a general invite should be created.
|
|
15
|
+
# @param permission_bits [String, Integer] Permission bits that should be appended to invite url.
|
|
16
|
+
# @param redirect_uri [String] Redirect URI that should be appended to invite url.
|
|
17
|
+
# @param scopes [Array<String>] Scopes that should be appended to invite url.
|
|
18
|
+
# @return [String] the OAuth invite URL.
|
|
19
|
+
def invite_url(server: nil, permission_bits: nil, redirect_uri: nil, scopes: ['bot'])
|
|
20
|
+
@client_id ||= bot_application.id
|
|
21
|
+
|
|
22
|
+
query = URI.encode_www_form({
|
|
23
|
+
client_id: @client_id,
|
|
24
|
+
guild_id: server&.id,
|
|
25
|
+
permissions: permission_bits,
|
|
26
|
+
redirect_uri: redirect_uri,
|
|
27
|
+
scope: scopes.join(' ')
|
|
28
|
+
}.compact)
|
|
29
|
+
|
|
30
|
+
"https://discord.com/oauth2/authorize?#{query}"
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
# Revokes an invite to a server. Will fail unless you have the *Manage Server* permission.
|
|
34
|
+
# It is recommended that you use {Invite#delete} instead.
|
|
35
|
+
# @param code [String, Invite] The invite to revoke. For possible formats see {#resolve_invite_code}.
|
|
36
|
+
def delete_invite(code)
|
|
37
|
+
invite = resolve_invite_code(code)
|
|
38
|
+
REST::Invite.delete(token, invite)
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module OnyxCord
|
|
4
|
+
class Bot
|
|
5
|
+
module Messaging
|
|
6
|
+
# Sends a text message to a channel given its ID and the message's content.
|
|
7
|
+
# @param channel [Channel, String, Integer] The channel, or its ID, to send something to.
|
|
8
|
+
# @param content [String] The text that should be sent as a message. It is limited to 2000 characters (Discord imposed).
|
|
9
|
+
# @param tts [true, false] Whether or not this message should be sent using Discord text-to-speech.
|
|
10
|
+
# @param embeds [Hash, OnyxCord::Webhooks::Embed, Array<Hash>, Array<OnyxCord::Webhooks::Embed> nil] The rich embed(s) to append to this message.
|
|
11
|
+
# @param allowed_mentions [Hash, OnyxCord::AllowedMentions, false, nil] Mentions that are allowed to ping on this message. `false` disables all pings
|
|
12
|
+
# @param message_reference [Message, String, Integer, Hash, nil] The message, or message ID, to reply to if any.
|
|
13
|
+
# @param components [View, Array<Hash>] Interaction components to associate with this message.
|
|
14
|
+
# @param flags [Integer] Flags for this message. Currently only SUPPRESS_EMBEDS (1 << 2), SUPPRESS_NOTIFICATIONS (1 << 12), and IS_COMPONENTS_V2 (1 << 15) can be set.
|
|
15
|
+
# @param nonce [String, nil] A optional nonce in order to verify that a message was sent. Maximum of twenty-five characters.
|
|
16
|
+
# @param enforce_nonce [true, false] Whether the nonce should be enforced and used for message de-duplication.
|
|
17
|
+
# @param poll [Hash, Poll::Builder, Poll, nil] The poll that should be attached to this message.
|
|
18
|
+
# @return [Message] The message that was sent.
|
|
19
|
+
def send_message(channel, content, tts = false, embeds = nil, attachments = nil, allowed_mentions = nil, message_reference = nil, components = nil, flags = 0, nonce = nil, enforce_nonce = false, poll = nil)
|
|
20
|
+
channel = channel.resolve_id
|
|
21
|
+
debug("Sending message to #{channel} with content '#{content}'")
|
|
22
|
+
allowed_mentions = { parse: [] } if allowed_mentions == false
|
|
23
|
+
message_reference = { message_id: message_reference.resolve_id } if message_reference.respond_to?(:resolve_id)
|
|
24
|
+
embeds = (embeds.instance_of?(Array) ? embeds.map(&:to_hash) : [embeds&.to_hash]).compact
|
|
25
|
+
flags = OnyxCord::MessageComponents.apply_v2_flag(flags, components)
|
|
26
|
+
|
|
27
|
+
response = REST::Channel.create_message(token, channel, content, tts, embeds, nonce, attachments, allowed_mentions&.to_hash, message_reference, components, flags, enforce_nonce, poll&.to_h)
|
|
28
|
+
Message.new(JSON.parse(response), self)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# Sends a text message to a channel given its ID and the message's content,
|
|
32
|
+
# then deletes it after the specified timeout in seconds.
|
|
33
|
+
# @param channel [Channel, String, Integer] The channel, or its ID, to send something to.
|
|
34
|
+
# @param content [String] The text that should be sent as a message. It is limited to 2000 characters (Discord imposed).
|
|
35
|
+
# @param timeout [Float] The amount of time in seconds after which the message sent will be deleted.
|
|
36
|
+
# @param tts [true, false] Whether or not this message should be sent using Discord text-to-speech.
|
|
37
|
+
# @param embeds [Hash, OnyxCord::Webhooks::Embed, Array<Hash>, Array<OnyxCord::Webhooks::Embed> nil] The rich embed(s) to append to this message.
|
|
38
|
+
# @param attachments [Array<File>] Files that can be referenced in embeds via `attachment://file.png`
|
|
39
|
+
# @param allowed_mentions [Hash, OnyxCord::AllowedMentions, false, nil] Mentions that are allowed to ping on this message. `false` disables all pings
|
|
40
|
+
# @param message_reference [Message, String, Integer, nil] The message, or message ID, to reply to if any.
|
|
41
|
+
# @param components [View, Array<Hash>] Interaction components to associate with this message.
|
|
42
|
+
# @param flags [Integer] Flags for this message. Currently only SUPPRESS_EMBEDS (1 << 2), SUPPRESS_NOTIFICATIONS (1 << 12), and IS_COMPONENTS_V2 (1 << 15) can be set.
|
|
43
|
+
# @param nonce [String, nil] A optional nonce in order to verify that a message was sent. Maximum of twenty-five characters.
|
|
44
|
+
# @param enforce_nonce [true, false] Whether the nonce should be enforced and used for message de-duplication.
|
|
45
|
+
# @param poll [Hash, Poll::Builder, Poll, nil] The poll that should be attached to this message.
|
|
46
|
+
def send_temporary_message(channel, content, timeout, tts = false, embeds = nil, attachments = nil, allowed_mentions = nil, message_reference = nil, components = nil, flags = 0, nonce = nil, enforce_nonce = false, poll = nil)
|
|
47
|
+
Internal::AsyncRuntime.async do
|
|
48
|
+
message = send_message(channel, content, tts, embeds, attachments, allowed_mentions, message_reference, components, flags, nonce, enforce_nonce, poll)
|
|
49
|
+
Internal::AsyncRuntime.sleep(timeout)
|
|
50
|
+
message.delete
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
nil
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
# Sends a file to a channel. If it is an image, it will automatically be embedded.
|
|
57
|
+
# @note This executes in a blocking way, so if you're sending long files, be wary of delays.
|
|
58
|
+
# @param channel [Channel, String, Integer] The channel, or its ID, to send something to.
|
|
59
|
+
# @param file [File] The file that should be sent.
|
|
60
|
+
# @param caption [string] The caption for the file.
|
|
61
|
+
# @param tts [true, false] Whether or not this file's caption should be sent using Discord text-to-speech.
|
|
62
|
+
# @param filename [String] Overrides the filename of the uploaded file
|
|
63
|
+
# @param spoiler [true, false] Whether or not this file should appear as a spoiler.
|
|
64
|
+
# @example Send a file from disk
|
|
65
|
+
# bot.send_file(83281822225530880, File.open('rubytaco.png', 'r'))
|
|
66
|
+
def send_file(channel, file, caption: nil, tts: false, filename: nil, spoiler: nil)
|
|
67
|
+
if file.respond_to?(:read)
|
|
68
|
+
if spoiler
|
|
69
|
+
filename ||= File.basename(file.path)
|
|
70
|
+
filename = "SPOILER_#{filename}" unless filename.start_with? 'SPOILER_'
|
|
71
|
+
end
|
|
72
|
+
file.define_singleton_method(:original_filename) { filename } if filename
|
|
73
|
+
file.define_singleton_method(:path) { filename } if filename
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
channel = channel.resolve_id
|
|
77
|
+
response = REST::Channel.upload_file(token, channel, file, caption: caption, tts: tts)
|
|
78
|
+
Message.new(JSON.parse(response), self)
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
# Gets the users, channels, roles and emoji from a string.
|
|
82
|
+
# @param mentions [String] The mentions, which should look like `<@12314873129>`, `<#123456789>`, `<@&123456789>` or `<:name:126328:>`.
|
|
83
|
+
# @param server [Server, nil] The server of the associated mentions. (recommended for role parsing, to speed things up)
|
|
84
|
+
# @return [Array<User, Channel, Role, Emoji>] The array of users, channels, roles and emoji identified by the mentions, or `nil` if none exists.
|
|
85
|
+
def parse_mentions(mentions, server = nil)
|
|
86
|
+
array_to_return = []
|
|
87
|
+
# While possible mentions may be in message
|
|
88
|
+
while mentions.include?('<') && mentions.include?('>')
|
|
89
|
+
# Removing all content before the next possible mention
|
|
90
|
+
mentions = mentions.split('<', 2)[1]
|
|
91
|
+
# Locate the first valid mention enclosed in `<...>`, otherwise advance to the next open `<`
|
|
92
|
+
next unless mentions.split('>', 2).first.length < mentions.split('<', 2).first.length
|
|
93
|
+
|
|
94
|
+
# Store the possible mention value to be validated with RegEx
|
|
95
|
+
mention = mentions.split('>', 2).first
|
|
96
|
+
if /@!?(?<id>\d+)/ =~ mention
|
|
97
|
+
array_to_return << user(id) unless user(id).nil?
|
|
98
|
+
elsif /#(?<id>\d+)/ =~ mention
|
|
99
|
+
array_to_return << channel(id, server) unless channel(id, server).nil?
|
|
100
|
+
elsif /@&(?<id>\d+)/ =~ mention
|
|
101
|
+
if server
|
|
102
|
+
array_to_return << server.role(id) unless server.role(id).nil?
|
|
103
|
+
else
|
|
104
|
+
@servers.each_value do |element|
|
|
105
|
+
array_to_return << element.role(id) unless element.role(id).nil?
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
elsif /(?<animated>^a|^${0}):(?<name>\w+):(?<id>\d+)/ =~ mention
|
|
109
|
+
array_to_return << (emoji(id) || Emoji.new({ 'animated' => animated != '', 'name' => name, 'id' => id }, self, nil))
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
array_to_return
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
# Gets the user, channel, role or emoji from a string.
|
|
116
|
+
# @param mention [String] The mention, which should look like `<@12314873129>`, `<#123456789>`, `<@&123456789>` or `<:name:126328:>`.
|
|
117
|
+
# @param server [Server, nil] The server of the associated mention. (recommended for role parsing, to speed things up)
|
|
118
|
+
# @return [User, Channel, Role, Emoji] The user, channel, role or emoji identified by the mention, or `nil` if none exists.
|
|
119
|
+
def parse_mention(mention, server = nil)
|
|
120
|
+
parse_mentions(mention, server).first
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
# Join a thread
|
|
124
|
+
# @param channel [Channel, Integer, String]
|
|
125
|
+
def join_thread(channel)
|
|
126
|
+
REST::Channel.join_thread(@token, channel.resolve_id)
|
|
127
|
+
nil
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
# Leave a thread
|
|
131
|
+
# @param channel [Channel, Integer, String]
|
|
132
|
+
def leave_thread(channel)
|
|
133
|
+
REST::Channel.leave_thread(@token, channel.resolve_id)
|
|
134
|
+
nil
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
# Add a member to a thread
|
|
138
|
+
# @param channel [Channel, Integer, String]
|
|
139
|
+
# @param member [Member, Integer, String]
|
|
140
|
+
def add_thread_member(channel, member)
|
|
141
|
+
REST::Channel.add_thread_member(@token, channel.resolve_id, member.resolve_id)
|
|
142
|
+
nil
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
# Remove a member from a thread
|
|
146
|
+
# @param channel [Channel, Integer, String]
|
|
147
|
+
# @param member [Member, Integer, String]
|
|
148
|
+
def remove_thread_member(channel, member)
|
|
149
|
+
REST::Channel.remove_thread_member(@token, channel.resolve_id, member.resolve_id)
|
|
150
|
+
nil
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
# Makes the bot leave any groups with no recipients remaining
|
|
154
|
+
def prune_empty_groups
|
|
155
|
+
@channels.each_value do |channel|
|
|
156
|
+
channel.leave_group if channel.group? && channel.recipients.empty?
|
|
157
|
+
end
|
|
158
|
+
end
|
|
159
|
+
end
|
|
160
|
+
end
|
|
161
|
+
end
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module OnyxCord
|
|
4
|
+
class Bot
|
|
5
|
+
module OAuth
|
|
6
|
+
# Creates a new application to do OAuth authorization with. This allows you to use OAuth to authorize users using
|
|
7
|
+
# Discord. For information how to use this, see the docs: https://discord.com/developers/docs/topics/oauth2
|
|
8
|
+
# @param name [String] What your application should be called.
|
|
9
|
+
# @param redirect_uris [Array<String>] URIs that Discord should redirect your users to after authorizing.
|
|
10
|
+
# @return [Array(String, String)] your applications' client ID and client secret to be used in OAuth authorization.
|
|
11
|
+
def create_oauth_application(name, redirect_uris)
|
|
12
|
+
response = JSON.parse(REST.create_oauth_application(@token, name, redirect_uris))
|
|
13
|
+
[response['id'], response['secret']]
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
# Changes information about your OAuth application
|
|
17
|
+
# @param name [String] What your application should be called.
|
|
18
|
+
# @param redirect_uris [Array<String>] URIs that Discord should redirect your users to after authorizing.
|
|
19
|
+
# @param description [String] A string that describes what your application does.
|
|
20
|
+
# @param icon [String, nil] A data URI for your icon image (for example a base 64 encoded image), or nil if no icon
|
|
21
|
+
# should be set or changed.
|
|
22
|
+
def update_oauth_application(name, redirect_uris, description = '', icon = nil)
|
|
23
|
+
REST.update_oauth_application(@token, name, redirect_uris, description, icon)
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module OnyxCord
|
|
4
|
+
class Bot
|
|
5
|
+
module Presence
|
|
6
|
+
# Updates presence status.
|
|
7
|
+
# @param status [String] The status the bot should show up as. Can be `online`, `dnd`, `idle`, or `invisible`
|
|
8
|
+
# @param activity [String, nil] The name of the activity to be played/watched/listened to/stream name on the stream.
|
|
9
|
+
# @param url [String, nil] The Twitch URL to display as a stream. nil for no stream.
|
|
10
|
+
# @param since [Integer] When this status was set.
|
|
11
|
+
# @param afk [true, false] Whether the bot is AFK.
|
|
12
|
+
# @param activity_type [Integer] The type of activity status to display.
|
|
13
|
+
# Can be 0 (Playing), 1 (Streaming), 2 (Listening), 3 (Watching), or 5 (Competing).
|
|
14
|
+
# @see Gateway#send_status_update
|
|
15
|
+
def update_status(status, activity, url, since = 0, afk = false, activity_type = 0)
|
|
16
|
+
gateway_check
|
|
17
|
+
|
|
18
|
+
@activity = activity
|
|
19
|
+
@status = status
|
|
20
|
+
@streamurl = url
|
|
21
|
+
type = url ? 1 : activity_type
|
|
22
|
+
|
|
23
|
+
activity_obj = activity || url ? { 'name' => activity, 'url' => url, 'type' => type } : nil
|
|
24
|
+
@gateway.send_status_update(status, since, activity_obj, afk)
|
|
25
|
+
|
|
26
|
+
# Update the status in the cache
|
|
27
|
+
profile.update_presence('status' => status.to_s, 'activities' => [activity_obj].compact)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# Sets the currently playing game to the specified game.
|
|
31
|
+
# @param name [String] The name of the game to be played.
|
|
32
|
+
# @return [String] The game that is being played now.
|
|
33
|
+
def game=(name)
|
|
34
|
+
gateway_check
|
|
35
|
+
update_status(@status, name, nil)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
alias_method :playing=, :game=
|
|
39
|
+
|
|
40
|
+
# Sets the current listening status to the specified name.
|
|
41
|
+
# @param name [String] The thing to be listened to.
|
|
42
|
+
# @return [String] The thing that is now being listened to.
|
|
43
|
+
def listening=(name)
|
|
44
|
+
gateway_check
|
|
45
|
+
update_status(@status, name, nil, nil, nil, 2)
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# Sets the current watching status to the specified name.
|
|
49
|
+
# @param name [String] The thing to be watched.
|
|
50
|
+
# @return [String] The thing that is now being watched.
|
|
51
|
+
def watching=(name)
|
|
52
|
+
gateway_check
|
|
53
|
+
update_status(@status, name, nil, nil, nil, 3)
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
# Sets the currently online stream to the specified name and Twitch URL.
|
|
57
|
+
# @param name [String] The name of the stream to display.
|
|
58
|
+
# @param url [String] The url of the current Twitch stream.
|
|
59
|
+
# @return [String] The stream name that is being displayed now.
|
|
60
|
+
def stream(name, url)
|
|
61
|
+
gateway_check
|
|
62
|
+
update_status(@status, name, url)
|
|
63
|
+
name
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# Sets the currently competing status to the specified name.
|
|
67
|
+
# @param name [String] The name of the game to be competing in.
|
|
68
|
+
# @return [String] The game that is being competed in now.
|
|
69
|
+
def competing=(name)
|
|
70
|
+
gateway_check
|
|
71
|
+
update_status(@status, name, nil, nil, nil, 5)
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
# Sets status to online.
|
|
75
|
+
def online
|
|
76
|
+
gateway_check
|
|
77
|
+
update_status(:online, @activity, @streamurl)
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
alias_method :on, :online
|
|
81
|
+
|
|
82
|
+
# Sets status to idle.
|
|
83
|
+
def idle
|
|
84
|
+
gateway_check
|
|
85
|
+
update_status(:idle, @activity, nil)
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
alias_method :away, :idle
|
|
89
|
+
|
|
90
|
+
# Sets the bot's status to DnD (red icon).
|
|
91
|
+
def dnd
|
|
92
|
+
gateway_check
|
|
93
|
+
update_status(:dnd, @activity, nil)
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
# Sets the bot's status to invisible (appears offline).
|
|
97
|
+
def invisible
|
|
98
|
+
gateway_check
|
|
99
|
+
update_status(:invisible, @activity, nil)
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
end
|