2017-08-06 16:30:37 -07:00
|
|
|
require 'discordrb'
|
|
|
|
|
2017-08-08 21:00:28 -07:00
|
|
|
class User < Vrobot4::Server::User
|
|
|
|
attr_reader :real
|
2017-08-08 01:35:20 -07:00
|
|
|
|
2017-08-09 02:57:56 -07:00
|
|
|
def initialize user, ops, hop
|
2017-08-08 21:00:28 -07:00
|
|
|
@real = user
|
|
|
|
@name = user.name
|
|
|
|
@roles = "v"
|
|
|
|
if user.is_a? Discordrb::Member
|
2017-08-09 02:57:56 -07:00
|
|
|
if user.owner?
|
|
|
|
@roles += "Ooh"
|
|
|
|
elsif ops and ops.select {|role| user.role? role}.any?
|
|
|
|
@roles += "oh"
|
|
|
|
elsif hop and hop.select {|role| user.role? role}.any?
|
|
|
|
@roles += "h"
|
|
|
|
end
|
2017-08-08 01:35:20 -07:00
|
|
|
end
|
|
|
|
end
|
2017-08-08 21:00:28 -07:00
|
|
|
end
|
2017-08-08 01:35:20 -07:00
|
|
|
|
2017-08-08 21:00:28 -07:00
|
|
|
class Channel < Vrobot4::Server::Channel
|
|
|
|
attr_reader :real
|
2017-08-08 01:35:20 -07:00
|
|
|
|
2017-08-08 21:00:28 -07:00
|
|
|
def initialize chan
|
|
|
|
@real = chan
|
|
|
|
@name = "#" + chan.name
|
2017-08-08 01:35:20 -07:00
|
|
|
end
|
2017-08-08 21:00:28 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
class Sv_Discord < Vrobot4::Server::AudioServer
|
|
|
|
Vrobot4::Server.add_server_type self, "Discord"
|
|
|
|
|
|
|
|
attr_reader :bot
|
2017-08-08 01:35:20 -07:00
|
|
|
|
2017-08-06 05:36:15 -07:00
|
|
|
def initialize info
|
2017-08-08 01:35:20 -07:00
|
|
|
super
|
|
|
|
|
2017-08-09 02:57:56 -07:00
|
|
|
if info.key? "admins"; @ops = info["admins"]
|
|
|
|
else; @ops = []; end
|
|
|
|
|
|
|
|
if info.key? "halfop"; @hop = info["halfop"]
|
|
|
|
else; @hop = []; end
|
2017-08-08 01:35:20 -07:00
|
|
|
|
2017-08-06 16:30:37 -07:00
|
|
|
@bot = Discordrb::Bot.new \
|
|
|
|
token: info["apikey"],
|
|
|
|
client_id: info["client"]
|
|
|
|
|
|
|
|
@bot.message do |evt|
|
2017-08-08 01:35:20 -07:00
|
|
|
m = Vrobot4::Server::Message.new \
|
|
|
|
msg: evt.message.content,
|
2017-08-09 02:57:56 -07:00
|
|
|
user: User.new(evt.user, @ops, @hop),
|
2017-08-08 01:35:20 -07:00
|
|
|
chan: Channel.new(evt.channel),
|
|
|
|
serv: self,
|
2017-08-08 04:33:30 -07:00
|
|
|
reply: -> (text) {evt.respond text},
|
|
|
|
reply_b: -> (text) {evt.respond "```\n" + text + "```"}
|
2017-08-08 01:35:20 -07:00
|
|
|
|
2017-08-06 16:30:37 -07:00
|
|
|
if m.msg.start_with? '.'
|
|
|
|
argv = m.msg.split
|
|
|
|
cnam = argv.shift[1..-1]
|
|
|
|
onCommand m, cnam, argv
|
|
|
|
else
|
|
|
|
onMessage m
|
|
|
|
end
|
|
|
|
end
|
2017-08-06 05:36:15 -07:00
|
|
|
end
|
|
|
|
|
2017-08-08 21:03:18 -07:00
|
|
|
def voice_join m
|
|
|
|
chan = m.user.real.voice_channel
|
|
|
|
raise RuntimeError, "You're not in a voice channel" unless chan
|
|
|
|
@bot.voice_connect chan
|
|
|
|
end
|
|
|
|
|
|
|
|
def voice_quit m
|
|
|
|
@bot.voice_destroy m.chan.real.server.id
|
|
|
|
end
|
|
|
|
|
|
|
|
def play m, io
|
|
|
|
v = @bot.voice(m.chan.real)
|
|
|
|
raise ArgumentError, "Invalid i/o stream" unless io
|
|
|
|
raise RuntimeError, "I'm not in a voice channel" unless v
|
|
|
|
v.play_stream io
|
|
|
|
end
|
|
|
|
|
|
|
|
def is_playing? m
|
|
|
|
v = @bot.voice(m.chan.real)
|
|
|
|
v != nil and v.playing?
|
|
|
|
end
|
|
|
|
|
2017-08-06 05:36:15 -07:00
|
|
|
def connect
|
2017-08-06 16:30:37 -07:00
|
|
|
@bot.run
|
2017-08-06 05:36:15 -07:00
|
|
|
end
|
2017-08-08 01:35:20 -07:00
|
|
|
|
|
|
|
def type
|
|
|
|
"Discord"
|
|
|
|
end
|
2017-08-08 04:32:45 -07:00
|
|
|
|
|
|
|
def flags
|
|
|
|
"AD"
|
|
|
|
end
|
2017-08-06 05:36:15 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
## EOF
|