func (p *Plugin) apiAudioPlay(call otto.FunctionCall) otto.Value { if p.instance.Audio.IsPlaying() { return otto.FalseValue() } obj := call.Argument(0).Object() if obj == nil { return otto.FalseValue() } filenameValue, _ := obj.Get("filename") callbackValue, _ := obj.Get("callback") if enc, ok := p.instance.Client.AudioEncoder.(*opus.Encoder); ok { enc.SetApplication(gopus.Audio) } p.instance.Audio.Source = gumble_ffmpeg.SourceFile(filenameValue.String()) p.instance.Audio.Play() go func() { p.instance.Audio.Wait() if callbackValue.IsFunction() { p.callValue(callbackValue) } }() return otto.TrueValue() }
func (p *Plugin) apiProcessNew(call otto.FunctionCall) otto.Value { callback := call.Argument(0) command := call.Argument(1).String() args := make([]string, len(call.ArgumentList)-2) for i, arg := range call.ArgumentList[2:] { args[i] = arg.String() } proc := &process{ cmd: exec.Command(command, args...), } go func() { var str string bytes, err := proc.cmd.Output() if err == nil { if bytes != nil { str = string(bytes) } p.callValue(callback, proc.cmd.ProcessState.Success(), str) } else { p.callValue(callback, false, "") } }() ret, _ := p.state.ToValue(proc) return ret }
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) 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 (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) 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 (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) apiTimerNew(call otto.FunctionCall) otto.Value { callback := call.Argument(0) timeout, _ := call.Argument(1).ToInteger() t := &timer{ cancel: make(chan bool), } go func() { defer func() { close(t.cancel) t.cancel = nil }() select { case <-time.After(time.Millisecond * time.Duration(timeout)): p.callValue(callback) case <-t.cancel: } }() ret, _ := p.state.ToValue(t) return ret }
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) 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() }
func (f *file) Write(call otto.FunctionCall) otto.Value { val, _ := f.f.WriteString(call.Argument(0).String()) ret, _ := call.Otto.ToValue(val) return ret }