From 36187d706f11f29816fbc60ea975ce50df9152fa Mon Sep 17 00:00:00 2001 From: Nanashi Date: Wed, 16 Aug 2023 12:50:26 +0900 Subject: [PATCH] Fix: Fix type errors (#21) --- Gemfile | 34 ++++++++-------- Steepfile | 2 - lib/discorb/channel/category.rb | 4 +- lib/discorb/channel/forum.rb | 2 +- lib/discorb/extension.rb | 1 + lib/discorb/gateway_requests.rb | 32 +++++++++++---- rbs_collection.lock.yaml | 72 ++++----------------------------- sig/discorb/activity.rbs | 16 ++++++-- sig/discorb/channel/forum.rbs | 6 +-- sig/discorb/user.rbs | 6 +-- 10 files changed, 73 insertions(+), 102 deletions(-) diff --git a/Gemfile b/Gemfile index adf0438b..da3702d5 100644 --- a/Gemfile +++ b/Gemfile @@ -5,32 +5,32 @@ source "https://rubygems.org" # Specify your gem's dependencies in discorb.gemspec gemspec -gem "rake", "~> 13.0" +gem "rake", "~> 13.0", require: false # Rubocop -gem "rubocop", "~> 1.25" -gem "rubocop-rake", "~> 0.6.0" -gem "rubocop-rspec", "~> 2.9" +gem "rubocop", "~> 1.25", require: false +gem "rubocop-rake", "~> 0.6.0", require: false +gem "rubocop-rspec", "~> 2.9", require: false # RSpec -gem "async-rspec", "~> 1.17" -gem "parallel_tests", "~> 4.2" -gem "rspec", "~> 3.12" +gem "async-rspec", "~> 1.17", require: false +gem "parallel_tests", "~> 4.2", require: false +gem "rspec", "~> 3.12", require: false # Typecheck -gem "steep", "~> 1.1" +gem "steep", "~> 1.5", require: false # Other development tools -gem "lefthook", "~> 1.4.3" -gem "sord", "~> 3.0.1" +gem "lefthook", "~> 1.4.3", require: false +gem "sord", "~> 3.0.1", require: false group :docs, optional: true do - gem "crowdin-api", "~> 1.0" - gem "gettext", "~> 3.4.1" - gem "redcarpet" - gem "rubyzip", "~> 2.3" - gem "yard", "~> 0.9.26" + gem "crowdin-api", "~> 1.0", require: false + gem "gettext", "~> 3.4.1", require: false + gem "redcarpet", require: false + gem "rubyzip", "~> 2.3", require: false + gem "yard", "~> 0.9.26", require: false end -gem "syntax_tree", "~> 2.8" -gem "syntax_tree-rbs", "~> 0.5.0" +gem "syntax_tree", "~> 2.8", require: false +gem "syntax_tree-rbs", "~> 0.5.0", require: false diff --git a/Steepfile b/Steepfile index 7f31bd85..9b098c54 100644 --- a/Steepfile +++ b/Steepfile @@ -7,8 +7,6 @@ target :lib do check "lib" - library "net-http", "timeout" - configure_code_diagnostics(D::Ruby.lenient) configure_code_diagnostics do |config| config[D::Ruby::UnsupportedSyntax] = nil diff --git a/lib/discorb/channel/category.rb b/lib/discorb/channel/category.rb index 2eafbc4b..71760924 100644 --- a/lib/discorb/channel/category.rb +++ b/lib/discorb/channel/category.rb @@ -10,7 +10,9 @@ class CategoryChannel < GuildChannel include Discorb::ChannelContainer def channels - @client.channels.values.filter { |channel| channel.parent == self } + @client.channels.values.filter do |channel| + channel.parent == self && channel.is_a?(Discorb::GuildChannel) + end end def create_text_channel(*args, **kwargs) diff --git a/lib/discorb/channel/forum.rb b/lib/discorb/channel/forum.rb index 3b8a3a79..d65b9a4d 100644 --- a/lib/discorb/channel/forum.rb +++ b/lib/discorb/channel/forum.rb @@ -464,7 +464,7 @@ def fetch_webhooks # @param [Integer] rate_limit_per_user The rate limit per user. # @param [Integer] slowmode Alias of `rate_limit_per_user`. # - # @return [Async::Task] The created thread. + # @return [Async::Task] The created thread. # def create_post( title, diff --git a/lib/discorb/extension.rb b/lib/discorb/extension.rb index ffdfa040..3c86849f 100644 --- a/lib/discorb/extension.rb +++ b/lib/discorb/extension.rb @@ -50,6 +50,7 @@ module ClassMethods # @param [Hash] metadata Other metadata. # def event(event_name, id: nil, **metadata, &block) + # @type var event_name: Symbol unless event_name.is_a?(Symbol) raise ArgumentError, "Event name must be a symbol" end diff --git a/lib/discorb/gateway_requests.rb b/lib/discorb/gateway_requests.rb index 06d05a02..1edffe3e 100644 --- a/lib/discorb/gateway_requests.rb +++ b/lib/discorb/gateway_requests.rb @@ -5,6 +5,13 @@ module Discorb # Represents an activity for Gateway Command. # class Activity + # @return [String] The text of the activity. + attr_reader :text + # @return [:playing, :streaming, :listening, :watching, :custom, :competing] The type of the activity. + attr_reader :type + # @return [String] The URL of the activity. + attr_reader :url + # @private # @return [{Symbol => Integer}] The mapping of activity types. TYPES = { @@ -12,20 +19,27 @@ class Activity streaming: 1, listening: 2, watching: 3, + custom: 4, competing: 5 }.freeze # # Initializes a new Activity. # - # @param [String] name The name of the activity. - # @param [:playing, :streaming, :listening, :watching, :competing] type The type of activity. + # @param [String] text The text of the activity. + # @param [:playing, :streaming, :listening, :watching, :custom, :competing] type The type of activity. # @param [String] url The URL of the activity. # - def initialize(name, type = :playing, url = nil) - @name = name - @type = TYPES[type] or - raise ArgumentError, "Invalid activity type: #{type}" + def initialize(text, type = :playing, url: nil) + @text = text + @type = + ( + if TYPES.key?(type) + TYPES[type] + else + raise(ArgumentError, "invalid activity type: #{type}") + end + ) @url = url end @@ -35,7 +49,11 @@ def initialize(name, type = :playing, url = nil) # @return [Hash] A hash representation of the activity. # def to_hash - { name: @name, type: @type, url: @url } + if @type == :custom + { state: @text, type: @type, url: @url } + else + { name: @text, type: @type, url: @url } + end end def inspect diff --git a/rbs_collection.lock.yaml b/rbs_collection.lock.yaml index b4192ee6..d95eae1a 100644 --- a/rbs_collection.lock.yaml +++ b/rbs_collection.lock.yaml @@ -1,76 +1,21 @@ --- sources: -- name: ruby/gem_rbs_collection +- type: git + name: ruby/gem_rbs_collection + revision: 8a678b2ec20e9d594055f53745399814e3a887dc remote: https://github.com/ruby/gem_rbs_collection.git - revision: main repo_dir: gems path: ".gem_rbs_collection" gems: -- name: net-http - version: '0' - source: - type: stdlib - name: logger version: '0' source: type: stdlib -- name: ast - version: '2.4' - source: - type: git - name: ruby/gem_rbs_collection - revision: 347858a6750d478f6cf1285b4ebd6ae945af2609 - remote: https://github.com/ruby/gem_rbs_collection.git - repo_dir: gems -- name: forwardable - version: '0' - source: - type: stdlib -- name: i18n - version: '1.10' - source: - type: git - name: ruby/gem_rbs_collection - revision: 347858a6750d478f6cf1285b4ebd6ae945af2609 - remote: https://github.com/ruby/gem_rbs_collection.git - repo_dir: gems -- name: listen - version: '3.2' - source: - type: git - name: ruby/gem_rbs_collection - revision: 347858a6750d478f6cf1285b4ebd6ae945af2609 - remote: https://github.com/ruby/gem_rbs_collection.git - repo_dir: gems -- name: minitest - version: '0' - source: - type: stdlib -- name: parallel - version: '1.20' - source: - type: git - name: ruby/gem_rbs_collection - revision: 347858a6750d478f6cf1285b4ebd6ae945af2609 - remote: https://github.com/ruby/gem_rbs_collection.git - repo_dir: gems -- name: prime - version: '0' - source: - type: stdlib -- name: rainbow - version: '3.0' - source: - type: git - name: ruby/gem_rbs_collection - revision: 347858a6750d478f6cf1285b4ebd6ae945af2609 - remote: https://github.com/ruby/gem_rbs_collection.git - repo_dir: gems -- name: singleton +- name: monitor version: '0' source: type: stdlib -- name: uri +- name: net-http version: '0' source: type: stdlib @@ -78,11 +23,8 @@ gems: version: '0' source: type: stdlib -- name: monitor - version: '0' - source: - type: stdlib -- name: mutex_m +- name: uri version: '0' source: type: stdlib +gemfile_lock_path: Gemfile.lock diff --git a/sig/discorb/activity.rbs b/sig/discorb/activity.rbs index a29c203f..5a55c71e 100644 --- a/sig/discorb/activity.rbs +++ b/sig/discorb/activity.rbs @@ -4,13 +4,23 @@ module Discorb class Activity TYPES: untyped + type types = :playing | :streaming | :listening | :watching | :custom | :competing + + # @return [String] The text of the activity. + attr_reader text: String + # @return [:playing, :streaming, :listening, :watching, :competing] The type of the activity. + attr_reader type: types + # @return [String] The URL of the activity. + attr_reader url: String? + # # Initializes a new Activity. # - # @param [String] name The name of the activity. - # @param [:playing, :streaming, :listening, :watching, :competing] type The type of activity. + # @param [String] text The text of the activity. + # @param [:playing, :streaming, :listening, :watching, :custom, :competing] type The type of activity. # @param [String] url The URL of the activity. - def initialize: (String name, ?Symbol `type`, ?String? url) -> void + # + def initialize: (String text, types type, ?url: String?) -> void # # Converts the activity to a hash. diff --git a/sig/discorb/channel/forum.rbs b/sig/discorb/channel/forum.rbs index a9617365..15363298 100644 --- a/sig/discorb/channel/forum.rbs +++ b/sig/discorb/channel/forum.rbs @@ -63,7 +63,7 @@ module Discorb String name, Discorb::emoji emoji, ?moderated: bool - ) -> Discorb::ForumChannel::Tag + ) -> void # Returns the tag as a hash. # @@ -292,7 +292,7 @@ module Discorb # @param [Integer] rate_limit_per_user The rate limit per user. # @param [Integer] slowmode Alias of `rate_limit_per_user`. # - # @return [Async::Task] The created thread. + # @return [Async::Task] The created thread. def create_post: ( title: String, ?content: String, @@ -306,7 +306,7 @@ module Discorb ?auto_archive_duration: :hour | :day | :three_days | :week, ?rate_limit_per_user: Integer, ?slowmode: Integer - ) -> Async::Task[Discorb::PublicThread] + ) -> Async::Task[Discorb::ForumChannel::Post] alias start_thread create_post alias create_thread start_thread diff --git a/sig/discorb/user.rbs b/sig/discorb/user.rbs index 90fc783d..737e1809 100644 --- a/sig/discorb/user.rbs +++ b/sig/discorb/user.rbs @@ -35,7 +35,7 @@ module Discorb attr_reader verified: bool # @return [String] The user's username. ("sevenc_nanashi" for new users, "Nanashi." for old users.) - attr_reader :username + attr_reader username: String alias name username # @return [Discorb::Snowflake] The user's ID. @@ -46,10 +46,10 @@ module Discorb # @return [String] The user's discriminator. ("0" for new users, "7740" for old users.) # @deprecated This will be removed in the future because of discord changes. - attr_reader :discriminator + attr_reader discriminator: String # @return [String] The user's global name. ("Nanashi." for new users, old users have no global name.) - attr_reader :global_name + attr_reader global_name: String # @return [Discorb::Asset, Discorb::DefaultAvatar] The user's avatar. attr_reader avatar: Discorb::Asset | Discorb::DefaultAvatar