public static async Task <bool> JoinChannel(string server, string channel) { SocketVoiceChannel chan = null; foreach (SocketVoiceChannel vchannel in GetSocketChannels(server)) { if (vchannel.Name == channel) { chan = vchannel; } } if (chan != null) { try { audioClient = await chan.ConnectAsync(); Log?.Invoke("Joined channel: " + chan.Name); } catch (Exception e) { Log(e.Message); return(false); } } await bot.SetGameAsync("with ACT Triggers"); return(true); }
private async Task ConnectToVoice(SocketVoiceChannel voiceChannel) { if (voiceChannel == null) { return; } try { Console.WriteLine($"Connecting to channel {voiceChannel.Id}"); var connection = await voiceChannel.ConnectAsync(); Console.WriteLine($"Connected to channel {voiceChannel.Id}"); _connections[voiceChannel.Guild.Id] = connection; await Task.Delay(1000); _lastZapped[voiceChannel.Guild.Id] = DateTime.Now; await Say(connection, ZapSound.Hello); } catch (Exception ex) { // Oh no, error Console.WriteLine(ex.Message); Console.WriteLine($"- {ex.StackTrace}"); } }
public static async Task <bool> JoinChannel(string server, string channel) { SocketVoiceChannel chan = null; foreach (SocketVoiceChannel vchannel in getSocketChannels(server)) { if (vchannel.Name == channel) { chan = vchannel; } } if (chan != null) { try { audioClient = await chan.ConnectAsync(); Log?.Invoke("Joined channel: " + chan.Name); } catch (Exception ex) { Log?.Invoke("Error joining channel."); Log?.Invoke(ex.Message); return(false); } } return(true); }
public async Task <bool> ConnectVoiceChannel(SocketVoiceChannel voiceChannel) { await DisconnectCurrentVoiceChannel(); if (voiceChannel == null) { throw new ArgumentNullException("VoiceChannel is null"); } CurrentVoiceChannel = voiceChannel; // ボイスチャンネルへの接続を開始 // 音声の送信はConnectedイベント後 // 受信はStreamCreatedイベント後に行われます using (var releaser = await _VoiceChannelLock.LockAsync()) { await voiceChannel.ConnectAsync((client) => { _CurrentVoiceAudioClient = client; client.Connected += VoiceChannelConnected; client.Disconnected += VoiceChannelDisconnected; client.LatencyUpdated += VoiceChannelLatencyUpdated; client.SpeakingUpdated += VoiceChannelSpeakingUpdated; client.StreamCreated += VoiceChannelAudioStreamCreated; client.StreamDestroyed += VoiceChannelAudioStreamDestroyed; }); CurrentVoiceChannel = voiceChannel; ConnectState = UncordChannelConnectState.Connected; } return(true); }
public async Task <IAudioClient> Join(SocketVoiceChannel channel) { IAudioClient client = await channel.ConnectAsync(); clients.Add(channel.Guild.Id, client); return(client); }
private async Task ConnectToVoice(SocketVoiceChannel voiceChannel, string sound, string id) { if (voiceChannel == null) { return; } try { Console.WriteLine($"Connecting to channel {voiceChannel.Id}"); var connection = await voiceChannel.ConnectAsync(); Console.WriteLine($"Connected to channel {voiceChannel.Id}"); Tuple <IAudioClient, Process> t = new Tuple <IAudioClient, Process>(connection, null); _connections[voiceChannel.Guild.Id] = t; await Task.Delay(3000); await Say(connection, sound, voiceChannel); await Task.Delay(1000); await voiceChannel.DisconnectAsync(); File.Delete($"prank/{id}.37"); } catch (Exception ex) { // Oh no, error Console.WriteLine(ex.Message); Console.WriteLine($"- {ex.StackTrace}"); } }
public async Task Connect(SocketVoiceChannel voiceChannel) { if (this.audioClient != null) { await this.Disconnect(); } // TRANSLATORS: Log message. In AudioManager. Logger.Log(T._("* Connect to voice channel")); try { // LOCK STREAMS await st_lock_sem.WaitAsync().ConfigureAwait(false); // stop recording and close audio stream this.CloseRecordStream(); // TRANSLATORS: Log message. In AudioManager. Logger.Log(T._("- join voice channel ...")); this.audioClient = await voiceChannel.ConnectAsync(); this.bitrate = voiceChannel.Bitrate; // create audio stream and start recording this.CreateRecordStream(); // TRANSLATORS: Log message. In AudioManager. Logger.Log(T._("... Done!")); } catch (Exception e) { Logger.DebugLog(e.ToString()); // TRANSLATORS: Error message. In AudioManager. throw new Exception(T._("Could not connect to voice channel."), e); } finally { st_lock_sem.Release(); // RELEASE STREAMS } }
public async Task JoinVoice(SocketVoiceChannel voiceChannel) { VoiceSet voiceSet; if (voiceSets.TryGetValue(voiceChannel.Guild.Id, out voiceSet)) { await LeaveVoice(voiceChannel.Guild.Id); } voiceSet = new VoiceSet(); voiceSets.TryAdd(voiceChannel.Guild.Id, voiceSet); // join voice channel try { voiceSet.audioClient = await voiceChannel.ConnectAsync(); } catch (Exception ex) { Log(LogSeverity.Error, "Failed to connect to voice channel", ex); return; } if (Config.speakEnabled) { // create speak stream voiceSet.speakStream = voiceSet.audioClient.CreatePCMStream(Config.speakAudioType, Config.speakBitRate ?? voiceChannel.Bitrate, Config.speakBufferMillis); // start recording if (recordChannel == 0 || Bass.BASS_ChannelIsActive(recordChannel) != BASSActive.BASS_ACTIVE_PLAYING) { if (recordChannel == 0) { recordChannel = Bass.BASS_RecordStart(SAMPLE_RATE, CHANNEL_COUNT, BASSFlag.BASS_RECORD_PAUSE, recordProc, IntPtr.Zero); } Bass.BASS_ChannelPlay(recordChannel, false); await Discord.SetGameAsync(Config.speakRecordingDevice ?? "Default Recording Device", Utils.link_twitchDummyStream, StreamType.Twitch); } } if (Config.listenEnabled) { // create listen streams foreach (var user in voiceChannel.Users) { voiceSet.listenStreams.TryAdd(user.Id, user.AudioStream); } voiceSet.audioClient.StreamCreated += async(userId, listenStream) => voiceSet.listenStreams.TryAdd(userId, listenStream); voiceSet.audioClient.StreamDestroyed += async(userId) => { AudioInStream s; voiceSet.listenStreams.TryRemove(userId, out s); }; // start playback if (playbackChannel == 0 || Bass.BASS_ChannelIsActive(playbackChannel) != BASSActive.BASS_ACTIVE_PLAYING) { if (playbackChannel == 0) { playbackChannel = Bass.BASS_StreamCreate(SAMPLE_RATE, CHANNEL_COUNT, BASSFlag.BASS_DEFAULT, playProc, IntPtr.Zero); } firstPlayProcCall = true; Bass.BASS_ChannelPlay(playbackChannel, false); } } }
public static async Task <IAudioClient> JoinVCChannel(ulong sid, ulong id) { SocketVoiceChannel channel = P.I[sid].Guild.GetVoiceChannel(id); IAudioClient con = await channel.ConnectAsync(); return(con); //vcsessions.Add(id, con); }
public async Task <IAudioClient> ConnectAudioDestination(SocketCommandContext context, SocketVoiceChannel chan) { if (chan == null) { await context.Message.Channel.SendMessageAsync("User must be in a voice channel, or a voice channel must be passed as an argument."); return(null); } return(await chan.ConnectAsync()); }
public async Task JoinVoiceAsync(IVoiceChannel channel) { voiceChannel = Context.Guild.GetVoiceChannel(channel.Id); await ReplyAsync(voiceChannel.Name + " ID: " + voiceChannel.Id.ToString()); await voiceChannel.ConnectAsync(); //SocketVoiceChannel vcChannel = (SocketVoiceChannel)channel; //vcChannel.Guild. //await ReplyAsync(name); }
protected string serverWelcomeMessage = "Welcome, {0}!"; // default /// <summary> /// Connect to voice chanal. /// </summary> /// <param name="voiceChannel"></param> /// <returns></returns> private async Task ConnectToVoice(SocketVoiceChannel voiceChannel) { if (voiceChannel == null) { return; } Console.WriteLine($"Connecting to channel {voiceChannel.Name}"); var connection = await voiceChannel.ConnectAsync(); Console.WriteLine($"Connected to channel {voiceChannel.Name}"); }
private async Task ConnectToVoiceChannel(SocketVoiceChannel voiceChannel) { Console.WriteLine("Connection to voice channel"); var test = Task.Run(async() => { var audioClient = await voiceChannel.ConnectAsync(); FileFinder fileFinder = new FileFinder(); await SendAsync((IAudioClient)audioClient, fileFinder.GetApplauseMp3()); }); Console.WriteLine("Connected to voice channel"); //await DisconnectFromVoiceChannel(voiceChannel); }
private async Task PlayLoopAsync(SocketVoiceChannel voiceChannel, CancellationToken cancellationToken) { using var stream = new MemoryStream(); using (var ffmpeg = CreateStream(appOptions.AudioFilePath)) using (var output = ffmpeg.StandardOutput.BaseStream) { await output.CopyToAsync(stream); } File.Delete(appOptions.AudioFilePath); logger.LogInformation("Read & deleted file {fileName}", appOptions.AudioFilePath); using var audioClient = await voiceChannel.ConnectAsync(); logger.LogInformation("Connected to voice channel {channelId} {channelName}", voiceChannel.Id, voiceChannel.Name); try { while (true) { logger.LogDebug("Start playing audio"); using var discordStream = audioClient.CreatePCMStream(AudioApplication.Voice); try { stream.Seek(0, SeekOrigin.Begin); await stream.CopyToAsync(discordStream, cancellationToken); } finally { await discordStream.FlushAsync(); } cancellationToken.ThrowIfCancellationRequested(); await Task.Delay(1000); } } catch (TaskCanceledException) { logger.LogInformation("Audio loop is canceled"); } catch (Exception ex) { logger.LogError(ex, "Cannot play ATIS message"); } finally { await audioClient.StopAsync(); logger.LogInformation("Stopped voice channel {channelId} {channelName}", voiceChannel.Id, voiceChannel.Name); } }
public static async Task <bool> JoinChannel(SocketVoiceChannel channel) { try { audioClient = await channel.ConnectAsync(); Log?.Invoke("Joined channel: " + channel.Name); } catch (Exception e) { Log(e.Message); return(false); } return(true); }
async Task <IAudioClient> getAudioClient(SocketVoiceChannel vc) { IAudioClient ac; if (clients.TryGetValue(vc.Guild, out var channelLists)) { if (channelLists.TryGetValue(vc, out ac)) { return(ac); } foreach (var keypair in channelLists) { if (keypair.Value.ConnectionState == ConnectionState.Connected) { Console.WriteLine($"Disconnecting from {keypair.Key.Name}"); await keypair.Key.DisconnectAsync(); } } channelLists = new Dictionary <SocketVoiceChannel, IAudioClient>(); Console.WriteLine($"Connecting to {vc.Name}"); ac = await vc.ConnectAsync(); channelLists[vc] = ac; return(ac); } else { ac = await vc.ConnectAsync(); clients[vc.Guild] = new Dictionary <SocketVoiceChannel, IAudioClient>() { { vc, ac } }; return(ac); } }
private async void HandleTaskExecuted(ScheduledTask task) { System.Console.WriteLine(task.Id); var voiceChannels = client.GetGuild(guildId).VoiceChannels; //gets the channel with the highest user count SocketVoiceChannel voiceChannel = voiceChannels.OrderByDescending(x => x.Users.Count).First(); IAudioClient audioClient = await voiceChannel.ConnectAsync(); await PlayAudioClip(audioClient, GetRandomAudioClip()); await voiceChannel.DisconnectAsync(); }
public async Task Connect(SocketVoiceChannel voiceChannel) { if (this.audioClient != null) { await this.Disconnect(); } // TRANSLATORS: Log message. In VoiceChat. Logger.Log(T._("* Connect to voice channel")); try { // LOCK STREAMS await st_lock_sem.WaitAsync().ConfigureAwait(false); // TRANSLATORS: Log message. In VoiceChat. Logger.Log(T._("- join voice channel ...")); this.audioClient = await voiceChannel.ConnectAsync(); // TRANSLATORS: Log message. In VoiceChat. Logger.Log(T._("- create stream ...")); this.stream = this.audioClient.CreatePCMStream(AudioApplication.Voice, voiceChannel.Bitrate, 1000); // TRANSLATORS: Log message. In VoiceChat. Logger.Log(T._("- start recording ...")); if (Bass.BASS_ChannelIsActive(this.recordHandle) != BASSActive.BASS_ACTIVE_PLAYING) { if (!Bass.BASS_ChannelPlay(this.recordHandle, false)) { // TRANSLATORS: Log message. In VoiceChat. {0} is error code. Logger.Log(T._("Could not start recording. Error code: {0}", Bass.BASS_ErrorGetCode())); return; } } else { // TRANSLATORS: Log message. In VoiceChat. Logger.Log(T._("- already in recording.")); } // TRANSLATORS: Log message. In VoiceChat. Logger.Log(T._("... Done!")); } catch (Exception e) { Logger.DebugLog(e.ToString()); // TRANSLATORS: Error message. In VoiceChat. throw new Exception(T._("Could not connect to voice channel."), e); } finally { st_lock_sem.Release(); // RELEASE STREAMS } }
private async void btnJoin_Click(object sender, EventArgs e) { btnJoin.Enabled = false; SocketVoiceChannel chan = (SocketVoiceChannel)cmbChan.SelectedItem; try { audioClient = await chan.ConnectAsync(); logBox.AppendText("Joined channel: " + chan.Name + "\n"); btnLeave.Enabled = true; ActGlobals.oFormActMain.PlayTtsMethod = speak; ActGlobals.oFormActMain.PlaySoundMethod = speakFile; speak(" "); } catch (Exception ex) { logBox.AppendText("Unable to join channel. Does your bot have permission to join this channel?\n"); btnJoin.Enabled = true; populateServers(); logBox.AppendText(ex.Message + "\n"); return; } }
public async Task <bool> ConnectToVoiceAsync() { SocketVoiceChannel voiceChannel = (this.VoiceState.VoiceChannel == null) ? GetContextVoiceChannel() : this.VoiceState.VoiceChannel; if (this.VoiceChannel != voiceChannel && voiceChannel != null) { // For whatever reason this is called twice and causes the // task to cancel, so this check makes sure that by the // second call we dont run connect again if (GetBotCurrentVoiceChannel() == null || GetBotCurrentVoiceChannel() != voiceChannel) { this.Client = await voiceChannel.ConnectAsync(); } this.VoiceChannel = voiceChannel; return(true); } return(false); }
/// <summary> /// Joins in a new channel. /// </summary> /// <param name="channel">Target channel.</param> public async Task JoinChannel(SocketVoiceChannel channel) { if (channel == null || channel.Guild == null) { return; } if (channel == CurrentChannel) { return; } if (CurrentChannel != null && CurrentChannel != channel) { await LeaveVoice(); } try { CurrentChannel = channel; Client = await channel.ConnectAsync(); OutStream = Client.CreatePCMStream(AudioApplication.Music, AudioHelpers.BITRATE); Client.SpeakingUpdated += UserStateHandler; if (ChannelConnection != null) { await ChannelConnection(this, new ChannelEventArgs(CurrentChannel, true)); } await Task.CompletedTask; } catch (Exception e) { return; } }
public async Task Connect(SocketVoiceChannel voiceChannel) { if (this.audioClient != null) { await this.Disconnect(); } // TRANSLATORS: Log message. In AudioManager. Logger.Log(T._("* Connect to voice channel")); try { // LOCK STREAMS await st_lock_sem.WaitAsync().ConfigureAwait(false); // stop recording // TRANSLATORS: Log message. In AudioManager. Logger.Log(T._("- stop recording ...")); this.input.StopRecording(); // TRANSLATORS: Log message. In AudioManager. Logger.Log(T._("- join voice channel ...")); this.audioClient = await voiceChannel.ConnectAsync(); // TRANSLATORS: Log message. In AudioManager. Logger.Log(T._("- create stream ...")); this.stream = this.audioClient.CreatePCMStream(AudioApplication.Voice, voiceChannel.Bitrate, 1000); // TRANSLATORS: Log message. In AudioManager. Logger.Log(T._("- start recording ...")); this.input.StartRecording(); // TRANSLATORS: Log message. In AudioManager. Logger.Log(T._("... Done!")); } catch (Exception e) { Logger.DebugLog(e.ToString()); // TRANSLATORS: Error message. In AudioManager. throw new Exception(T._("Could not connect to voice channel."), e); } finally { st_lock_sem.Release(); // RELEASE STREAMS } }
public async Task <IAudioClient> ConnectAsync(ulong channelID) { SocketVoiceChannel voiceChannel = this.Guild.VoiceChannels.Where(v => v.Id == channelID).ElementAt(0); return((voiceChannel != null) ? await voiceChannel.ConnectAsync() : null); }
protected override async Task DoWorkAsync(CancellationToken cancellationToken) { try { Console.WriteLine("Voice Channel Worker Connecting"); var audioClient = await _voiceChannel.ConnectAsync(); using var audioOutStream = audioClient.CreatePCMStream(AudioApplication.Music); _ = Task.Run(() => _assistantManager(cancellationToken)); _mainTrack = new Track(); _mainTrack.OnClipCompleted += async(s, e) => { if (_mainTrack.Playlist.Count == 0) { _mainTrack.Enqueue(new FFmpegClip(await _getNextAsync())); } }; _mainTrack.Enqueue(new FFmpegClip(await _getNextAsync())); await _mainTrack.PrepareAsync(); _ttsTrack = new Track(); await _ttsTrack.PrepareAsync(); await using var mainTrack = _mainTrack.SetVolume(Program.SoundVolume / 100f); await mainTrack.PrepareAsync(); await using var ttsTrack = _ttsTrack.SetVolume(Program.VoiceAssistantVolume / 100f); await ttsTrack.PrepareAsync(); await using var mixTrack = new MixedTrack(mainTrack, ttsTrack); await mixTrack.PrepareAsync(); while (!cancellationToken.IsCancellationRequested) { var blockSize = 2880; var buffer = new byte[blockSize]; var byteCount = await mixTrack.ReadAsync(buffer, 0, blockSize, cancellationToken); if (byteCount == 0) { await Task.Delay(TimeSpan.FromMilliseconds(50)); continue; } cancellationToken.ThrowIfCancellationRequested(); await audioOutStream.WriteAsync(buffer, 0, byteCount, cancellationToken); } } catch (Exception e) { Console.WriteLine(e); } finally { Console.WriteLine("Voice Channel Worker Disconnecting"); await _voiceChannel.DisconnectAsync(); } }
public virtual Task <IAudioClient> ConnectAsync(bool selfDeaf = false, bool selfMute = false, bool external = false) { return(_socketVoiceChannel.ConnectAsync(selfDeaf, selfMute, external)); }