vrobot4/source/servers/sv_discord.rb

135 lines
3.7 KiB
Ruby
Raw Normal View History

2017-08-06 16:30:37 -07:00
require 'discordrb'
2017-08-11 19:40:43 -07:00
# A server implementation for Discord using discordrb.
2017-08-08 21:00:28 -07:00
class Sv_Discord < Vrobot4::Server::AudioServer
2017-08-11 19:40:43 -07:00
# The server type name.
def self.type() "Discord" end
2017-08-11 18:50:43 -07:00
Vrobot4::Server.add_server_type self
2017-08-08 21:00:28 -07:00
2017-08-11 19:40:43 -07:00
attr_reader :bot # The Discordrb::Bot instance.
2017-08-11 19:40:43 -07:00
# (see Vrobot4::Server::Server#initialize)
def initialize info
super
@ops = info["admins"] || []
@hop = info["halfop"] || []
2017-08-06 16:30:37 -07:00
@bot = Discordrb::Bot.new \
token: info["apikey"],
client_id: info["client"]
@bot.message do |evt|
m = Vrobot4::Server::Message.new \
msg: evt.message.content,
2017-08-09 02:57:56 -07:00
user: User.new(evt.user, @ops, @hop),
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-13 03:48:00 -07:00
handle_text_cmd m
2017-08-06 16:30:37 -07:00
end
end
2017-08-11 19:40:43 -07:00
# (see Vrobot4::Server::AudioServer#voice_join)
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
2017-08-11 19:40:43 -07:00
# (see Vrobot4::Server::AudioServer#voice_join)
2017-08-08 21:03:18 -07:00
def voice_quit m
@bot.voice_destroy m.chan.real.server.id
end
# (see Vrobot4::Server::AudioServer#voice_play)
def voice_play m, io
2017-08-08 21:03:18 -07:00
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
2017-08-11 19:40:43 -07:00
# (see Vrobot4::Server::AudioServer#is_playing?)
2017-08-08 21:03:18 -07:00
def is_playing? m
v = @bot.voice(m.chan.real)
v != nil and v.playing?
end
2017-08-11 19:40:43 -07:00
# (see Vrobot4::Server::Server#connect)
def connect
2017-08-06 16:30:37 -07:00
@bot.run
end
2017-08-11 19:40:43 -07:00
# (see Vrobot4::Server::Server#flags)
2017-08-08 04:32:45 -07:00
def flags
"A"
2017-08-08 04:32:45 -07:00
end
protected
2017-08-11 19:40:43 -07:00
# (see Vrobot4::Server::Server#load_permissions)
def load_permissions pinf
@mprm = {chan: {}, role: {}, glob: {}}
2017-08-13 03:48:00 -07:00
pinf.each do |pr|
mod = Vrobot4::Module.get_module_type(pr["module"])[:type]
if pr.key? "channel"
@mprm[:chan][mod] = ChannelPerms.new unless @mprm[:chan].key? mod
2017-08-13 03:48:00 -07:00
@mprm[:chan][mod][pr["channel"]] = true
elsif pr.key? "roles"
@mprm[:role][mod] = pr["roles"]
elsif pr.key? "enable"
@mprm[:glob][mod] = pr["enable"]
end
2017-08-13 03:48:00 -07:00
end if pinf
end
class ChannelPerms
2017-08-13 03:48:00 -07:00
def initialize() @cprm = {} end
2017-08-11 19:40:43 -07:00
# Returns true if the channel is enabled, false otherwise.
2017-08-13 03:48:00 -07:00
def [](chan) @cprm[chan.name] or @cprm[chan.real.id] end
2017-08-11 19:40:43 -07:00
# Sets a channel's permission on/off.
2017-08-13 03:48:00 -07:00
def []=(chan, set) @cprm[chan] = set end
end
2017-08-11 19:40:43 -07:00
private_constant :ChannelPerms
2017-08-11 19:40:43 -07:00
# A Discord user.
class User < Vrobot4::Server::User
2017-08-11 19:40:43 -07:00
attr_reader :real # The Discordrb::User instance.
2017-08-11 19:40:43 -07:00
# @param user [Discordrb::User] the discord user
# @param ops [Array] list of operator role IDs
# @param hop [Array] list of half-operator role IDs
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 && ops.select {|role| user.role? role}.any?
@roles += "oh"
elsif hop && hop.select {|role| user.role? role}.any?
@roles += "h"
end
end
end
end
2017-08-11 19:40:43 -07:00
# A Discord channel.
class Channel < Vrobot4::Server::Channel
2017-08-11 19:40:43 -07:00
attr_reader :real # The Discordrb::Channel instance.
2017-08-11 19:40:43 -07:00
# @param chan [Discordrb::Channel] the discord channel
def initialize chan
@real = chan
@name = "#" + chan.name
end
end
end
## EOF