From bdd094926815196802c7d0bdad089fee9791abf4 Mon Sep 17 00:00:00 2001 From: TrueDoctor Date: Mon, 9 Apr 2018 17:41:52 +0200 Subject: Implemented Audio Memes small sound ques wich can be played from anywhere --- DiscoBot/Auxiliary/Soundeffects.cs | 54 ++++++++++++++++++++++++++++++++++++++ DiscoBot/Auxiliary/SpellCorrect.cs | 2 ++ DiscoBot/Characters/Character.cs | 12 ++++++--- DiscoBot/Commands/Voice.cs | 54 +++++++++++++++++++++++++++++--------- DiscoBot/DiscoBot.csproj | 1 + 5 files changed, 106 insertions(+), 17 deletions(-) create mode 100644 DiscoBot/Auxiliary/Soundeffects.cs (limited to 'DiscoBot') diff --git a/DiscoBot/Auxiliary/Soundeffects.cs b/DiscoBot/Auxiliary/Soundeffects.cs new file mode 100644 index 0000000..bc5bbb3 --- /dev/null +++ b/DiscoBot/Auxiliary/Soundeffects.cs @@ -0,0 +1,54 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DiscoBot.Auxiliary +{ + using DiscoBot.Commands; + + public enum Sound + { + Ding, + Nooo, + Monterkill, + Finish, + Wrong + } + + public static class SoundEffects + { + public static Task Play(Sound s) + { + string url = string.Empty; + int vol = 256; + switch (s) + { + case Sound.Ding: + url = "https://www.myinstants.com/media/sounds/boxing-bell.mp3"; + break; + case Sound.Finish: + url = "https://www.myinstants.com/media/sounds/finishhim.swf.mp3"; + break; + case Sound.Monterkill: + url = "https://www.myinstants.com/media/sounds/announcer_kill_monster_01.mp3"; + break; + case Sound.Nooo: + url = "https://www.myinstants.com/media/sounds/nooo.swf.mp3"; + break; + case Sound.Wrong: + url = "https://www.myinstants.com/media/sounds/stupid_dum_03.mp3"; + vol = 10; + break; + } + + if (url != string.Empty) + { + return Voice.SendAsync(url, vol); + } + + return Dsa.GeneralContext.Channel.SendMessageAsync("Ton Existiert nicht"); + } + } +} diff --git a/DiscoBot/Auxiliary/SpellCorrect.cs b/DiscoBot/Auxiliary/SpellCorrect.cs index 1b740e8..244914b 100644 --- a/DiscoBot/Auxiliary/SpellCorrect.cs +++ b/DiscoBot/Auxiliary/SpellCorrect.cs @@ -6,6 +6,8 @@ public class SpellCorrect : StringComparer { + public const int ErrorThreshold = 94100; + public override int Compare(string x, string y) { if (string.IsNullOrEmpty(x)) diff --git a/DiscoBot/Characters/Character.cs b/DiscoBot/Characters/Character.cs index a78e613..249c6f5 100644 --- a/DiscoBot/Characters/Character.cs +++ b/DiscoBot/Characters/Character.cs @@ -71,8 +71,9 @@ var sc = new SpellCorrect(); var tTalent = this.Talente.OrderBy(x => sc.Compare(talent, x.Name)).First(); - if (sc.Compare(talent, tTalent.Name) > 94100) + if (sc.Compare(talent, tTalent.Name) > SpellCorrect.ErrorThreshold) { + SoundEffects.Play(Sound.Wrong); return $"{this.Name} kann nicht {talent}..."; } @@ -164,8 +165,9 @@ var output = new StringBuilder(); var sc = new SpellCorrect(); var attack = this.Kampftalente.OrderBy(x => sc.Compare(talent, x.Name)).First(); - if (sc.Compare(talent, attack.Name) > 94) + if (sc.Compare(talent, attack.Name) > SpellCorrect.ErrorThreshold) { + SoundEffects.Play(Sound.Wrong); return $"{this.Name} kann nicht mit der Waffenart {talent} umgehen..."; } @@ -187,8 +189,9 @@ var sc = new SpellCorrect(); var attack = this.Kampftalente.OrderBy(x => sc.Compare(talent, x.Name)).First(); - if (sc.Compare(talent, attack.Name) > 94) + if (sc.Compare(talent, attack.Name) > SpellCorrect.ErrorThreshold) { + SoundEffects.Play(Sound.Wrong); return $"{this.Name} kann nicht mit der Waffenart {talent} umgehen..."; } @@ -210,8 +213,9 @@ var sc = new SpellCorrect(); int fk = this.Eigenschaften["fk"]; var attack = this.Talente.OrderBy(x => sc.Compare(talent, x.Name)).First(); - if (sc.Compare(talent, attack.Name) > 94) + if (sc.Compare(talent, attack.Name) > SpellCorrect.ErrorThreshold) { + SoundEffects.Play(Sound.Wrong); return $"{this.Name} kann nicht mit der Waffenart {talent} umgehen..."; } diff --git a/DiscoBot/Commands/Voice.cs b/DiscoBot/Commands/Voice.cs index 772d807..3391452 100644 --- a/DiscoBot/Commands/Voice.cs +++ b/DiscoBot/Commands/Voice.cs @@ -1,8 +1,14 @@ namespace DiscoBot.Commands { + using System; + using System.Collections.Generic; using System.Diagnostics; + using System.Linq; + using System.Media; using System.Threading.Tasks; + using DiscoBot.Auxiliary; + using Discord; using Discord.Audio; using Discord.Commands; @@ -10,6 +16,17 @@ public class Voice : ModuleBase { public static IAudioClient Client { get; set; } + + public static async Task SendAsync(string path, int volume = 256) + { + // Create FFmpeg using the previous example + var ffmpeg = CreateStream(path, volume); + var output = ffmpeg.StandardOutput.BaseStream; + var discord = Client.CreatePCMStream(AudioApplication.Music); + await output.CopyToAsync(discord); + await discord.FlushAsync(); + } + [Command("join", RunMode = RunMode.Async)] public async Task JoinChannelAsync(IVoiceChannel channel = null) { @@ -42,29 +59,40 @@ [Command("play")] public Task PlayAudioAsync(string path) { - return Client == null ? this.Context.Channel.SendMessageAsync("Erst Joinen!") : SendAsync(path); + if (Client == null) + { + return this.Context.Channel.SendMessageAsync("Erst Joinen!"); + } + + var sounds = Enum.GetValues(typeof(Sound)); + var soundList = new List(); + foreach (var sound in sounds) + { + soundList.Add((Sound)sound); + } + + var sc = new SpellCorrect(); + + var tSound = soundList.OrderBy(x => sc.Compare(path, x.ToString())).First(); + + if (sc.Compare(path, tSound.ToString()) > SpellCorrect.ErrorThreshold) + { + return SendAsync(path); + } + + return SoundEffects.Play(tSound); } - private static Process CreateStream(string path) + private static Process CreateStream(string path, int vol = 256) { var ffmpeg = new ProcessStartInfo { FileName = "ffmpeg", - Arguments = $"-i {path} -ac 2 -f s16le -ar 48000 -ab 620000 pipe:1", + Arguments = $"-i {path} -ac 2 -f s16le -ar 48000 -ab 620000 -vol {vol} pipe:1", UseShellExecute = false, RedirectStandardOutput = true, }; return Process.Start(ffmpeg); } - - private static async Task SendAsync(string path) - { - // Create FFmpeg using the previous example - var ffmpeg = CreateStream(path); - var output = ffmpeg.StandardOutput.BaseStream; - var discord = Client.CreatePCMStream(AudioApplication.Music); - await output.CopyToAsync(discord); - await discord.FlushAsync(); - } } } diff --git a/DiscoBot/DiscoBot.csproj b/DiscoBot/DiscoBot.csproj index 3955ef6..29f4208 100644 --- a/DiscoBot/DiscoBot.csproj +++ b/DiscoBot/DiscoBot.csproj @@ -142,6 +142,7 @@ + -- cgit v1.2.3-54-g00ecf