-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rb
91 lines (83 loc) · 2.8 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
require "mongo"
require 'sinatra'
require "repltalk"
require "discordrb"
$rt_client = ReplTalk::Client.new(ENV["sid"])
mongo_client = Mongo::Client.new(ENV["monogurl"], database: "rtbot")
$discord_client = Discordrb::Commands::CommandBot.new(
token: ENV["bottoken"],
prefix: "rt>",
ignore_bots: true,
spaces_allowed: true,
command_doesnt_exist_message: "The command **`%command%`** does not exist"
)
$id_db = mongo_client[:id]
$servers_db = mongo_client[:servers]
def check_posts
loop do
begin
latest = $id_db.find.first[:id]
posts = $rt_client.get_posts(order: "New", count: 10).select { |post| post.id > latest && !post.repl.nil? }
rescue
posts = []
end
posts.each do |post|
embed = Discordrb::Webhooks::Embed.new(
title: post.title,
url: post.url,
description: post.content.length > 150 ? "#{post.preview}..." : post.content,
colour: post.board.nil? ? "0053A6".to_i(16) : post.board.color[1...post.board.color.length].to_i(16),
timestamp: Time.new,
author: Discordrb::Webhooks::EmbedAuthor.new(
name: "#{post.author.username} (#{post.author.cycles})",
url: "https://repl.it/@#{post.author}",
icon_url: post.author.pfp
),
footer: Discordrb::Webhooks::EmbedFooter.new(text: post.board.nil? ? "Community Forum" : "#{post.board.name} Board")
)
$servers_db.find.each do |server|
begin
$discord_client.send_message(
server[:channel_id],
nil,
false,
embed
)
rescue
next
end
end
end
$id_db.update_one( { "id" => $id_db.find.first[:id] }, { "$set" => { "id" => posts[0].id } } ) unless posts.length == 0
sleep 5
end
end
$discord_client.ready do
$discord_client.watching = "repl talk"
Thread.new { check_posts }
end
$discord_client.command :config, description: "Set the channel to send repl talk posts to", usage: "Do `**`rt>config`**` in the channel that you want repl talk posts to be sent to" do |event|
return "You need to be an admin to set a channel" unless event.author.defined_permission?(:administrator)
channel_id = event.channel.id
server_id = event.server.id
if $servers_db.find( { "server_id" => server_id } ).first == nil
$servers_db.insert_one( { "server_id" => server_id, "channel_id" => channel_id } )
else
$servers_db.update_one( { "server_id" => server_id }, { "$set" => { "channel_id" => channel_id } } )
end
"Repl Talk posts will be sent to this channel"
end
$discord_client.command :invite, description: "Sends my invite url so that you can add me to other servers" do |event|
event.bot.invite_url
end
$discord_client.server_delete do |event|
server_id = event.server
unless $servers_db.find( { "server_id" => server_id } ).first == nil
$servers_db.delete_one( { "server_id" => server_id } )
end
end
Thread.new { $discord_client.run }
set :bind, "0.0.0.0"
get "/" do
"Online!"
end