vrobot4/source/sv_discord.rb

135 lines
3.1 KiB
Ruby

require 'discordrb'
class Sv_Discord < Vrobot4::Server::AudioServer
Vrobot4::Server.add_server_type self, "Discord"
attr_reader :bot
def initialize info
super
if info.key? "admins"; @ops = info["admins"]
else; @ops = []; end
if info.key? "halfop"; @hop = info["halfop"]
else; @hop = []; end
@bot = Discordrb::Bot.new \
token: info["apikey"],
client_id: info["client"]
@bot.message do |evt|
m = Vrobot4::Server::Message.new \
msg: evt.message.content,
user: User.new(evt.user, @ops, @hop),
chan: Channel.new(evt.channel),
serv: self,
reply: -> (text) {evt.respond text},
reply_b: -> (text) {evt.respond "```\n" + text + "```"}
if m.msg.start_with? '.'
argv = m.msg.split
cnam = argv.shift[1..-1]
on_command m, cnam, argv
else
on_message m
end
end
end
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
def connect
@bot.run
end
def type
"Discord"
end
def flags
"AD"
end
protected
def load_permissions pinf
@mprm = {chan: {}, role: {}, glob: {}}
pinf.each do |perm|
mod = Vrobot4::Module.get_module_type(perm["module"])[:type]
if perm.key? "channel"
@mprm[:chan][mod] = ChannelPerms.new unless @mprm[:chan].key? mod
@mprm[:chan][mod][perm["channel"]] = true
elsif perm.key? "roles"
@mprm[:role][mod] = perm["roles"]
else
@mprm[:glob][mod] = perm["enable"]
end
end
end
class ChannelPerms
def initialize
@cprm = {}
end
def [] chan
(@cprm.key? chan.name and @cprm[chan.name]) or
(@cprm.key? chan.real.id and @cprm[chan.real.id])
end
def []= chan, set
@cprm[chan] = set
end
end
class User < Vrobot4::Server::User
attr_reader :real
def initialize user, ops, hop
@real = user
@name = user.name
@roles = "v"
if user.is_a? Discordrb::Member
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
end
end
end
class Channel < Vrobot4::Server::Channel
attr_reader :real
def initialize chan
@real = chan
@name = "#" + chan.name
end
end
end
## EOF