最近discordbotなどの存在を知り、rubyを使い始めた初心者です。 今回、WOTというゲームのトーナメントのページが更新されたらdiscordのテキストチャンネルに通知するというプログラムが組みたくていろいろ調べながらやっていました。 コマンドプロンプトで > ruby discordbot.rb と入力してbotを起動させようとしたところ discordbot.rb:23:in `block in <main>': undefined method `css' for nil:NilClass (NoMethodError) from discordbot.rb:15:in `loop' from discordbot.rb:15:in `<main>' というエラーが出てきました。調べてもよくわからず進展がない状態です。 組んだプログラムは # encoding: utf-8 require 'discordrb' require 'open-uri' require 'nokogiri' require 'certified' bot = Discordrb::Commands::CommandBot.new token: 'NDY4NDIzMjE5MzE4MjI2OTQ0.Di48uA.OEDvrwbK5Yrm_M8o1URofoFgUxY', prefix: '!' bot.message(with_text: 'Ping!') do |event| event.respond 'Pong!' end bot.run :async loop do # 1時間ごとにアナウンスページを確認して、差分があれば内容を通知 # HTML解析 doc = Nokogiri::HTML.parse(open('https://worldoftanks.asia/ja/tournaments/')) # 一番新しいトピックタイトルを取得 node = doc.xpath('//div[@class="news_content no_img"]') now_topic = node[0].css('strong').inner_text # 前回取得したトピックから更新があれば通知 if now_topic != File.read('data/sitecheck/current.txt') then # brタグを"\n"に変換 doc.search('br').each do |br| br.replace("\n") end msg = <<-EOS トーナメントページが更新されました 「#{now_topic}」 doc.at_xpath("//div[@class = 'i_contents']").content https://worldoftanks.asia/ja/tournaments/ EOS bot.send_message([418316166621298688], msg) # 今回取得したトピックを保存 File.open("data/sitecheck/current.txt", "w") do |f| f.print("#{now_topic}") end DEND sleep(3600) DEND DEND end end bot.run です。 最近プログラミングを始めたものなので解説記事からコピペしたものがほとんどで意味が分かっていないものもあります。 どなたかわかる方がいましたらどこをどう言う風に変えたら直るのか教えてください。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。