質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Q&A

解決済

2回答

3496閲覧

Array (NoMethodError)

退会済みユーザー

退会済みユーザー

総合スコア0

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

1グッド

1クリップ

投稿2018/09/04 07:01

discordbot製作中にエラーが出たので質問です。
目標はhttps://worldoftanks.asia/ja/tournaments/このリンクのページに定期的に追加されるトーナメントの詳細が書かれているページに飛ぶリンクをdiscordに持ってくることです。「アメジスト」や「サファイヤ」などの題名がわかるだけでもいいのですができればリンクを持ってきたいです。
#エラー内容

ruby

1C:/Ruby23/lib/ruby/gems/2.3.0/gems/discordrb-3.2.1/lib/discordrb/bot.rb:353:in `send_message': undefined method `resolve_id' for [418316166621298688]:Array (NoMethodError) 2 from discordbot1.rb:41:in `block in <main>' 3 from discordbot1.rb:15:in `loop' 4 from discordbot1.rb:15:in `<main>'

#bot.rbのソースコード
1200行ほどあるのでエラーの周辺のみ貼ります。

ruby

1 debug("Got voice channel: #{chan}") 2 3 @should_connect_to_voice[server_id] = chan 4 @gateway.send_voice_state_update(server_id.to_s, chan.id.to_s, false, false) 5 6 debug('Voice channel init packet sent! Now waiting.') 7 8 sleep(0.05) until @voices[server_id] 9 debug('Voice connect succeeded!') 10 @voices[server_id] 11 end 12 13 # Disconnects the client from a specific voice connection given the server ID. Usually it's more convenient to use 14 # {Discordrb::Voice::VoiceBot#destroy} rather than this. 15 # @param server_id [Integer] The ID of the server the voice connection is on. 16 # @param destroy_vws [true, false] Whether or not the VWS should also be destroyed. If you're calling this method 17 # directly, you should leave it as true. 18 def voice_destroy(server_id, destroy_vws = true) 19 @gateway.send_voice_state_update(server_id.to_s, nil, false, false) 20 @voices[server_id].destroy if @voices[server_id] && destroy_vws 21 @voices.delete(server_id) 22 end 23 24 # Revokes an invite to a server. Will fail unless you have the *Manage Server* permission. 25 # It is recommended that you use {Invite#delete} instead. 26 # @param code [String, Invite] The invite to revoke. For possible formats see {#resolve_invite_code}. 27 def delete_invite(code) 28 invite = resolve_invite_code(code) 29 API::Invite.delete(token, invite) 30 end 31 32 # Sends a text message to a channel given its ID and the message's content. 33 # @param channel_id [Integer] The ID that identifies the channel 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 tts [true, false] Whether or not this message should be sent using Discord text-to-speech. 36 # @param embed [Hash, Discordrb::Webhooks::Embed, nil] The rich embed to append to this message. 37 # @return [Message] The message that was sent. 38 def send_message(channel_id, content, tts = false, embed = nil) 39 channel_id = channel_id.resolve_id 40 debug("Sending message to #{channel_id} with content '#{content}'") 41 42 response = API::Channel.create_message(token, channel_id, content, [], tts, embed ? embed.to_hash : nil) 43 Message.new(JSON.parse(response), self) 44 45

#discordbot1.rbのソースコード

ruby

1# encoding: utf-8 2require 'discordrb' 3require 'open-uri' 4require 'nokogiri' 5require 'certified' 6 7bot = Discordrb::Commands::CommandBot.new token: 'NDY4NDIzMjE5MzE4MjI2OTQ0.Di48uA.OEDvrwbK5Yrm_M8o1URofoFgUxY', prefix: '!' 8 9bot.message(with_text: 'Ping!') do |event| 10 event.respond 'Pong!' 11end 12 13bot.run :async 14 15loop do 16 # 1時間ごとにアナウンスページを確認して、差分があれば内容を通知 17 18 # HTML解析 19 doc = Nokogiri::HTML.parse(open('https://worldoftanks.asia/ja/tournaments/')) 20 21 # 一番新しいトピックタイトルを取得 22 now_topic = doc.at_css('.landing-list .landing-list_item .landing-list_name').inner_text 23 24 # 前回取得したトピックから更新があれば通知 25 if now_topic != File.read('C:\Users\やまと\Desktop\新しいフォルダー\プログラミング\current.txt') then 26 27 # brタグを"\n"に変換 28 doc.search('br').each do |br| 29 br.replace("\n") 30 end 31 32 msg = <<-EOS 33トーナメントページが更新されました 34#{now_topic}」 35 36doc.at_xpath("//div[@class = 'i_content']").content 37 38https://worldoftanks.asia/ja/tournaments/ 39EOS 40 41 bot.send_message([418316166621298688], msg) 42 43 # 今回取得したトピックを保存 44 File.open("C:/Users/やまと/Desktop/新しいフォルダー/プログラミング/current.txt", "w") do |f| 45 f.print("#{now_topic}") 46 end 47 DEND 48 49 sleep(3600) 50DEND 51 52DEND 53 54end 55 56end-of-input 57 58bot.run

今までに見たことのないエラーで、調べても英語のページばっかだったりわかりやすい記事が見つからなかったので質問させてもらいました。
解決方法を教えてください。よろしくお願いします。

DrqYuto👍を押しています

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

ベストアンサー

# @param channel_id [Integer] The ID that identifies the channel to send something to.

なので

bot.send_message([418316166621298688], msg)

の[]を取りましょう。

rb

1 bot.send_message(418316166621298688, msg)

投稿2018/09/04 07:18

asm

総合スコア15147

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

今までに見たことのないエラー

それはすごい。

undefined method `resolve_id' for [418316166621298688]:Array

[418316166621298688]というArrayに対して、resolve_idというメソッドが呼ばれたが、そんなメソッドはArrayにはないというエラーです。

send_messageの使い方を間違えているのでしょう。

投稿2018/09/04 07:14

otn

総合スコア84423

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問