This repository has been archived by the owner on Jul 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.rb
executable file
·94 lines (78 loc) · 3.24 KB
/
main.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
=begin
Trains: A discord bot that scrapes the web to find train photos, and give you details about them in you discord server.
Copyright (C) 2023 cserver45
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
=end
require 'time'
require 'discordrb'
require 'httparty'
require 'nokogiri'
require 'configatron'
require_relative 'config/config'
bot = Discordrb::Commands::CommandBot.new token: configatron.token, client_id: configatron.clientid, intents: :unprivileged
uri = 'https://www.railpictures.net/photo/'
# For registering the commands on startup
bot.register_application_command(:train, 'Fetches a random train image.')
bot.register_application_command(:status, 'Get the status of the bot.')
# To show the licence disclaimer
puts 'Copyright (C) 2023 cserver45'
puts 'License details are in main.rb and LICENSE'
bot.ready do
bot.watching = 'Trains.'
end
bot.application_command(:train, description: 'Fetches a random train image.') do |event|
img_number = rand 1..836_514
is_img = false
response = HTTParty.get(uri + img_number.to_s)
doc = Nokogiri::HTML(response.body)
while is_img == false do
begin
img = doc.at_xpath('//meta[@property="og:image"]')['content']
desc = doc.at_xpath('//meta[@name="description"]')['content']
photographer = doc.at_xpath('/html/body/center/center/table[2]/tr[last()]/td/center/font[last()]')
photographer_extracted = photographer.to_s.split('<a').first.split('>').last
rescue NoMethodError
# need to get a new number, else its an infinite loop
img_number = rand 1..836_514
response = HTTParty.get(uri + img_number.to_s)
doc = Nokogiri::HTML(response.body)
else
is_img = true
end
end
builder = Discordrb::Webhooks::Builder.new
builder.add_embed do |embed|
embed.description = desc
embed.color = '#57F287'
embed.timestamp = Time.now
embed.image = Discordrb::Webhooks::EmbedImage.new(url: img)
embed.url = uri + img_number.to_s
embed.footer = Discordrb::Webhooks::EmbedFooter.new(text: photographer_extracted)
end
event.respond(embeds: builder.embeds.map(&:to_hash))
end
bot.application_command(:status, description: 'Get the status of the bot.') do |event|
builder = Discordrb::Webhooks::Builder.new
builder.add_embed do |embed|
embed.description = 'Status of the Trains Discord Bot:'
embed.color = '#57F287'
embed.timestamp = Time.now
embed.fields = [
{name: ':writing_hand: Author:', value: 'cserver#3402'},
{name: ':robot: Bot Version', value: configatron.version},
{name: 'Ruby info:', value: RUBY_DESCRIPTION},
{name: 'Discordrb version:', value: Discordrb::VERSION},
]
end
event.respond(embeds: builder.embeds.map(&:to_hash))
end
bot.run