A beginner who recently learned about discordbot and started using ruby.
This time, I wanted to build a program to notify the discord text channel when the WOT game tournament page was updated.
At the command prompt
>ruby ​​discordbot.rb
I tried to start bot by typing
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>'
I got an error. There is no progress without knowing well.
The assembled program is
# 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!'
bot.run: async
loop do
  # Check the announcement page every hour and notify if there is a difference
  # HTML analysis
  doc = Nokogiri :: HTML.parse (open ('https://worldoftanks.asia/en/tournaments/'))
  # Get new topic title
  node = doc.xpath ('// div [@ class = "news_content no_img"]')
  now_topic = node [0] .css ('strong'). inner_text
  # Notify if there is an update from the last topic
  if now_topic! = File.read ('data/sitecheck/current.txt') then
    # convert br tag to "\ n"
    doc.search ('br'). each do | br |
      br.replace ("\ n")
    msg =<<-EOS
The tournament page has been updated
"# {Now_topic}"
doc.at_xpath ("// div [@class = 'i_contents']"). content
    bot.send_message ([418316166621298688], msg)
    # Save this topic
    File.open ("data/sitecheck/current.txt", "w") do | f |
      f.print ("# {now_topic}")
  sleep (3600)
Since I have started programming recently, most of them have been copied and copied from commentary articles.
If there is a person who knows who is, please tell me where to change it and how to change it.
  • Answer # 1

    undefined method'css'for nil: NilClass (NoMethodError)

    This is because the methodcssis not defined inNilClass~! It is an error.
    If you play with Ruby in the future, you will probably see this error.

    Is the corresponding code around here?

    node = doc.xpath ('// div [@ class = "news_content no_img"]')
    now_topic = node [0] .css ('strong'). inner_text

    It looks likenode [0]isnil.
    Is it because I can't find a div withclass ="news_content no_img"?

    I felt the DOM search conditions were a bit harsh, so
    Should I replace it with the following code instead? I thought.

    doc.at_css ('div.news_content.no_img>strong'). inner_text

    at_cssis a method that searches using a CSS query and returns the first element found.

    Crawlers using Nokogiri (Thank you Kenya, good article)

    But I couldn't find such a DOM in the link where ot_amay was pasted. .
    I don't know what ot_amay is asking for, but I think you can get a string like that with the following code.

    now_topic = doc.at_css ('. landing-list .landing-list_item .landing-list_name'). inner_text

    I don't know if it works because it isn't actually running. I'm sorry if it doesn't move!

  • Answer # 2

    I have nothing to do with the subject, but a word about how to create a question

    It is easy to understand when viewed on a PC,
    The question editing screen has input on the left and output on the right.
    Input is in markdawn format, and you can see how the respondent looks in the preview on the right

    In "markdawn", "#" is a character representing a headline, so "#" indicating a comment is displayed as a headline. So, please enclose the source code part in the code block as follows

    Not only indentation is preserved, but syntax highlighting enhances readability.
    It also makes it easier to copy and paste