diff options
Diffstat (limited to 'DiscoBot/Audio')
-rw-r--r-- | DiscoBot/Audio/AudioModule.cs | 28 | ||||
-rw-r--r-- | DiscoBot/Audio/AudioService.cs | 70 | ||||
-rw-r--r-- | DiscoBot/Audio/Sound.cs | 16 | ||||
-rw-r--r-- | DiscoBot/Audio/Soundeffects.cs | 93 | ||||
-rw-r--r-- | DiscoBot/Audio/Voice.cs | 69 |
5 files changed, 68 insertions, 208 deletions
diff --git a/DiscoBot/Audio/AudioModule.cs b/DiscoBot/Audio/AudioModule.cs index f8834f3..add4bf0 100644 --- a/DiscoBot/Audio/AudioModule.cs +++ b/DiscoBot/Audio/AudioModule.cs @@ -1,33 +1,21 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Security.Cryptography; -using System.Threading.Tasks; - -using DiscoBot; -using DiscoBot.Audio; -using DiscoBot.Auxiliary; -using DiscoBot.Commands; - +using System.Threading.Tasks; using Discord; using Discord.Commands; namespace DiscoBot.Audio { - using DiscoBot.DSA_Game; - public class AudioModule : ModuleBase { // Scroll down further for the AudioService. // Like, way down - private readonly AudioService _service; + private readonly AudioService service; // Remember to add an instance of the AudioService // to your IServiceCollection when you initialize your bot public AudioModule(AudioService service) { - _service = service; - Dsa.Service = service; + this.service = service; + //Dsa.Service = service; } // You *MUST* mark these commands with 'RunMode.Async' @@ -35,7 +23,7 @@ namespace DiscoBot.Audio [Command("_join", RunMode = RunMode.Async)] public async Task JoinCmd() { - await _service.JoinAudio(Context.Guild, (Context.User as IVoiceState).VoiceChannel); + await service.JoinAudio(Context.Guild, (Context.User as IVoiceState).VoiceChannel); } // Remember to add preconditions to your commands, @@ -44,13 +32,13 @@ namespace DiscoBot.Audio [Command("_leave", RunMode = RunMode.Async)] public async Task LeaveCmd() { - await _service.LeaveAudio(Context.Guild); + await service.LeaveAudio(Context.Guild); } [Command("_play", RunMode = RunMode.Async)] public async Task PlayCmd([Remainder] string song) { - if (Dsa.GeneralContext == null) + /*if (Dsa.GeneralContext == null) { Dsa.GeneralContext = this.Context; } @@ -71,7 +59,7 @@ namespace DiscoBot.Audio await _service.SendAudioAsync(Context.Guild, Context.Channel, song); } - SoundEffects.Play(song); + SoundEffects.Play(song);*/ } } }
\ No newline at end of file diff --git a/DiscoBot/Audio/AudioService.cs b/DiscoBot/Audio/AudioService.cs index 6e01980..a198eb2 100644 --- a/DiscoBot/Audio/AudioService.cs +++ b/DiscoBot/Audio/AudioService.cs @@ -1,34 +1,27 @@ -namespace DiscoBot.Audio -{ - using System.Collections.Concurrent; - using System.Diagnostics; - using System.IO; - using System.Threading.Tasks; - - using DiscoBot.DSA_Game; - - using Discord; - using Discord.Audio; +using System; +using System.Collections.Concurrent; +using System.Diagnostics; +using System.IO; +using System.Threading.Tasks; +using Discord; +using Discord.Audio; +namespace DiscoBot.Audio +{ public class AudioService { - private readonly ConcurrentDictionary<ulong, IAudioClient> connectedChannels = new ConcurrentDictionary<ulong, IAudioClient>(); + private readonly ConcurrentDictionary<ulong, IAudioClient> connectedChannels = + new ConcurrentDictionary<ulong, IAudioClient>(); public async Task JoinAudio(IGuild guild, IVoiceChannel target) { - if (this.connectedChannels.TryGetValue(guild.Id, out var client)) - { - return; - } + if (connectedChannels.TryGetValue(guild.Id, out var client)) return; - if (target.Guild.Id != guild.Id) - { - return; - } + if (target.Guild.Id != guild.Id) return; var audioClient = await target.ConnectAsync(); - if (this.connectedChannels.TryAdd(guild.Id, audioClient)) + if (connectedChannels.TryAdd(guild.Id, audioClient)) { // If you add a method to log happenings from this service, // you can uncomment these commented lines to make use of that. @@ -38,11 +31,9 @@ public async Task LeaveAudio(IGuild guild) { - if (this.connectedChannels.TryRemove(guild.Id, out var client)) - { + if (connectedChannels.TryRemove(guild.Id, out var client)) await client.StopAsync(); - //await Log(LogSeverity.Info, $"Disconnected from voice on {guild.Name}."); - } + //await Log(LogSeverity.Info, $"Disconnected from voice on {guild.Name}."); } public async Task SendAudioAsync(IGuild guild, IMessageChannel channel, string path) @@ -54,28 +45,31 @@ return; } - if (this.connectedChannels.TryGetValue(guild.Id, out var client)) - { + if (connectedChannels.TryGetValue(guild.Id, out var client)) //await Log(LogSeverity.Debug, $"Starting playback of {path} in {guild.Name}"); - using (var ffmpeg = this.CreateStream(path)) + using (var ffmpeg = CreateStream(path)) using (var stream = client.CreatePCMStream(AudioApplication.Music)) { - try { await ffmpeg.StandardOutput.BaseStream.CopyToAsync(stream); } - finally { await stream.FlushAsync(); } + try + { + await ffmpeg.StandardOutput.BaseStream.CopyToAsync(stream); + } + finally + { + await stream.FlushAsync(); + } } - } } - public async Task SendAudioAsync(string path, int Volume) + public async Task SendAudioAsync(string path, int volume) { // Your task: Get a full path to the file if the value of 'path' is only a filename. if (!File.Exists(path) && false) - { //await channel.SendMessageAsync("File does not exist."); return; - } - if (this.connectedChannels.TryGetValue(Dsa.GeneralContext.Guild.Id, out var client)) + throw new NotImplementedException("get channel data from server"); + /*if (this.connectedChannels.TryGetValue()) { //await Log(LogSeverity.Debug, $"Starting playback of {path} in {guild.Name}"); using (var ffmpeg = this.CreateStream(path)) @@ -84,10 +78,10 @@ try { await ffmpeg.StandardOutput.BaseStream.CopyToAsync(stream); } finally { await stream.FlushAsync(); } } - } + }*/ } - private Process CreateStream(string path) + private static Process CreateStream(string path) { return Process.Start(new ProcessStartInfo { @@ -98,4 +92,4 @@ }); } } -} +}
\ No newline at end of file diff --git a/DiscoBot/Audio/Sound.cs b/DiscoBot/Audio/Sound.cs index a9a2768..85023c8 100644 --- a/DiscoBot/Audio/Sound.cs +++ b/DiscoBot/Audio/Sound.cs @@ -1,18 +1,12 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace DiscoBot.Audio +namespace DiscoBot.Audio { public class Sound { public Sound(string name, string url, int volume) { - this.Name = name; - this.Url = url; - this.Volume = volume; + Name = name; + Url = url; + Volume = volume; } public string Name { get; } @@ -21,4 +15,4 @@ namespace DiscoBot.Audio public int Volume { get; } } -} +}
\ No newline at end of file diff --git a/DiscoBot/Audio/Soundeffects.cs b/DiscoBot/Audio/Soundeffects.cs deleted file mode 100644 index f3a014c..0000000 --- a/DiscoBot/Audio/Soundeffects.cs +++ /dev/null @@ -1,93 +0,0 @@ -namespace DiscoBot.Audio -{ - using System; - using System.Linq; - using System.Threading.Tasks; - - using DiscoBot.Auxiliary; - using DiscoBot.Commands; - using DiscoBot.DSA_Game; - - /*public enum Sound - { - Bell, - Ding, - Nooo, - Monsterkill, - Finish, - Wrong, - Magic, - Stupid, - Police, - Roblox - }*/ - - public static class SoundEffects - { - public static int MaxVolume { get; set; } = 50; - - public static void Play(string s) - { - string url = string.Empty; - int volume = 255; - - var tSound = DSA_Game.Save.Properties.Sounds.OrderBy(x => SpellCorrect.CompareEasy(s, x.Name)).First(); - - url = s; - - switch (s) - { - case "Bell": - case "Ding": - url = "https://www.myinstants.com/media/sounds/boxing-bell.mp3"; - break; - case "Finish": - url = "https://www.myinstants.com/media/sounds/finishhim.swf.mp3"; - break; - case "Magic": - url = "https://www.myinstants.com/media/sounds/dream-harp-sound-effect.mp3"; - break; - case "Monsterkill": - url = "https://www.myinstants.com/media/sounds/announcer_kill_monster_01.mp3"; - break; - case "Nooo": - url = "https://www.myinstants.com/media/sounds/nooo.swf.mp3"; - break; - case "Roblox": - url = "https://www.myinstants.com/media/sounds/roblox-death-sound_ytkBL7X.mp3"; - break; - case "Stupid": - url = "https://www.myinstants.com/media/sounds/stupid_dum_03.mp3"; - volume = 10; - break; - case "Police": - url = "https://www.myinstants.com/media/sounds/sound-of-the-police.mp3"; - break; - case "Wrong": - url = "https://www.myinstants.com/media/sounds/wrong-answer-sound-effect.mp3"; - volume = 50; - break; - } - - - - if (SpellCorrect.CompareEasy(s, tSound.Name) < SpellCorrect.ErrorThreshold) - { - url = tSound.Url; - volume = tSound.Volume; - } - - volume = (int)(volume * (MaxVolume / 100.0)); - - - if (url != string.Empty) - { - // await Dsa.Service.SendAudioAsync(url, vol); - Voice.Send(url, volume); - return; - } - - throw new Exception("Ton Existiert nicht"); - } - } -} diff --git a/DiscoBot/Audio/Voice.cs b/DiscoBot/Audio/Voice.cs index 379f65b..c2a3097 100644 --- a/DiscoBot/Audio/Voice.cs +++ b/DiscoBot/Audio/Voice.cs @@ -1,42 +1,34 @@ -namespace DiscoBot.Audio +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Diagnostics; +using System.Threading.Tasks; +using DiscoBot.Auxiliary; +using Discord; +using Discord.Audio; +using Discord.Commands; + +namespace DiscoBot.Audio { - using System; - using System.Collections.Generic; - using System.ComponentModel; - using System.Diagnostics; - using System.Linq; - using System.Threading.Tasks; - - using DiscoBot.Auxiliary; - using DiscoBot.DSA_Game; - - using Discord; - using Discord.Audio; - using Discord.Commands; - using Discord.WebSocket; - public class Voice : ModuleBase { public static IAudioClient Client { get; set; } public static void Send(string path, int volume = 256) { - if (Client == null) - { - throw new NullReferenceException("Bot befindet sich nicht in einem Sprachchannel"); - } + if (Client == null) throw new NullReferenceException("Bot befindet sich nicht in einem Sprachchannel"); // Create FFmpeg using the previous example var ffmpeg = CreateStream(path, volume); var output = ffmpeg.StandardOutput.BaseStream; var barInvoker = new BackgroundWorker(); barInvoker.DoWork += delegate - { - var discord = Client.CreatePCMStream(AudioApplication.Music); - output.CopyToAsync(discord); + { + var discord = Client.CreatePCMStream(AudioApplication.Music); + output.CopyToAsync(discord); - discord.FlushAsync(); - }; + discord.FlushAsync(); + }; barInvoker.RunWorkerAsync(); } @@ -44,7 +36,7 @@ [Command("join", RunMode = RunMode.Async)] public async Task JoinChannelAsync(IVoiceChannel channel = null) { - var msg = this.Context.Message; + var msg = Context.Message; // Get the audio channel channel = channel ?? (msg.Author as IGuildUser)?.VoiceChannel; @@ -67,37 +59,22 @@ if (Client != null) { - SoundEffects.Play("Nooo"); await Client.StopAsync(); Client = null; } } - [Command("volume")] - public void SetVolume(int volume) - { - if (volume <= 100 && volume >= 0) - { - SoundEffects.MaxVolume = volume; - } - } [Command("play", RunMode = RunMode.Async)] public async Task PlayAudioAsync(string path) { - if (Client == null) - { - await this.Context.Channel.SendMessageAsync("Erst Joinen!"); - } + if (Client == null) await Context.Channel.SendMessageAsync("Erst Joinen!"); - SoundEffects.Play(path); + //SoundEffects.Play(path); var sounds = Enum.GetValues(typeof(Sound)); var soundList = new List<Sound>(); - foreach (var sound in sounds) - { - soundList.Add((Sound)sound); - } + foreach (var sound in sounds) soundList.Add((Sound) sound); var sc = new SpellCorrect(); } @@ -109,9 +86,9 @@ FileName = "ffmpeg", Arguments = $"-i {path} -ac 2 -f s16le -ar 48000 -ab 620000 -vol {vol} pipe:1", UseShellExecute = false, - RedirectStandardOutput = true, + RedirectStandardOutput = true }; return Process.Start(ffmpeg); } } -} +}
\ No newline at end of file |