func (p *Plugin) apiAudioSetVolume(call otto.FunctionCall) otto.Value { volume, err := call.Argument(0).ToFloat() if err != nil { return otto.UndefinedValue() } p.instance.Audio.Volume = float32(volume) return otto.UndefinedValue() }
func (p *Plugin) apiAudioSetBitrate(call otto.FunctionCall) otto.Value { bitrate, err := call.Argument(0).ToInteger() if err != nil { return otto.UndefinedValue() } if enc, ok := p.instance.Client.AudioEncoder.(*opus.Encoder); ok { enc.SetBitrate(int(bitrate)) } return otto.UndefinedValue() }
func (p *Plugin) apiAudioBitrate(call otto.FunctionCall) otto.Value { if enc, ok := p.instance.Client.AudioEncoder.(*opus.Encoder); ok { value, _ := p.state.ToValue(enc.Bitrate()) return value } return otto.UndefinedValue() }
func (p *Plugin) apiFileOpen(call otto.FunctionCall) otto.Value { var filename, mode string switch len(call.ArgumentList) { case 1: filename = call.Argument(0).String() mode = "r" case 2: filename = call.Argument(0).String() mode = call.Argument(1).String() default: return otto.UndefinedValue() } var iMode int switch mode { case "r": iMode = os.O_RDONLY case "r+": iMode = os.O_RDWR case "w": iMode = os.O_WRONLY | os.O_CREATE case "w+": iMode = os.O_RDWR | os.O_CREATE | os.O_TRUNC case "a": iMode = os.O_WRONLY | os.O_CREATE | os.O_APPEND case "a+": iMode = os.O_RDWR | os.O_CREATE | os.O_APPEND default: return otto.UndefinedValue() } osFile, err := os.OpenFile(filename, iMode, 0644) if err != nil { return otto.UndefinedValue() } f := &file{ f: osFile, } ret, _ := p.state.ToValue(f) return ret }
func (p *Plugin) apiAudioNewTarget(call otto.FunctionCall) otto.Value { id, err := call.Argument(0).ToInteger() if err != nil { return otto.UndefinedValue() } target := &gumble.VoiceTarget{} target.ID = uint32(id) value, _ := p.state.ToValue(target) return value }
func (f *file) Read(call otto.FunctionCall) otto.Value { bytes, err := call.Argument(0).ToInteger() var data []byte if bytes <= 0 { data, err = ioutil.ReadAll(f.f) if err != nil { return otto.UndefinedValue() } } else { data = make([]byte, bytes) _, err = io.ReadFull(f.f, data) if err != nil { panic(err) return otto.UndefinedValue() } } ret, _ := call.Otto.ToValue(string(data)) return ret }
func (p *Plugin) apiAudioSetTarget(call otto.FunctionCall) otto.Value { if len(call.ArgumentList) == 0 { p.instance.Client.VoiceTarget = nil return otto.TrueValue() } target, err := call.Argument(0).Export() if err != nil { return otto.UndefinedValue() } voiceTarget := target.(*gumble.VoiceTarget) p.instance.Client.Send(voiceTarget) p.instance.Client.VoiceTarget = voiceTarget return otto.TrueValue() }
func (f *file) Seek(call otto.FunctionCall) otto.Value { offset, _ := call.Argument(0).ToInteger() whence := call.Argument(1).String() if whence == "" { whence = "cur" } var iWhence int switch whence { case "set": iWhence = 0 case "cur": iWhence = 1 case "end": iWhence = 2 default: return otto.UndefinedValue() } val, _ := f.f.Seek(offset, iWhence) ret, _ := call.Otto.ToValue(val) return ret }
func (p *Plugin) apiAudioStop(call otto.FunctionCall) otto.Value { p.instance.Audio.Stop() return otto.UndefinedValue() }
func (p *Plugin) apiOn(call otto.FunctionCall) otto.Value { event := strings.ToLower(call.Argument(0).String()) function := call.Argument(1) p.listeners[event] = append(p.listeners[event], function) return otto.UndefinedValue() }