コード例 #1
0
 protected void CommandDefault(User user, Channel ch, string cmd)
 {
     if (SoundEffectRepository.Exists(cmd))
     {
         CommandPlayEffect(user, ch, cmd);
     }
     else
     {
         CommandInvalid(user, ch, cmd);
     }
 }
コード例 #2
0
        protected void OnMessageReceived(object sender, MessageEventArgs e)
        {
            if (e.User.Id == client.CurrentUser.Id)
            {
                return;
            }

            // TODO: handle multiple attachments
            // TODO: handle exceptions

            if (e.Channel.IsPrivate && e.Message.Attachments.Length > 0)
            {
                var attachment = e.Message.Attachments.FirstOrDefault();

                if (attachment != null)
                {
                    var ext = Path.GetExtension(attachment.Filename);

                    if (attachment.Size > Configuration.MaximumSoundEffectSize)
                    {
                        SendMessage(e.Channel, Properties.Resources.MessageInvalidFileSize);
                        return;
                    }

                    if (!SoundEffectRepository.ValidateFilename(attachment.Filename))
                    {
                        SendMessage(e.Channel, Properties.Resources.MessageInvalidFilename);
                        return;
                    }

                    if (!SoundEffectRepository.ValidateFileExtension(ext))
                    {
                        SendMessage(e.Channel, Properties.Resources.MessageUnsupportedFileExtension);
                        return;
                    }

                    var key  = Path.GetFileNameWithoutExtension(attachment.Filename);
                    var name = Path.GetFileName(attachment.Filename);
                    var path = Path.Combine(Configuration.EffectsPath, name);

                    if (SoundEffectRepository.Exists(name))
                    {
                        SendMessage(e.Channel, Properties.Resources.MessageSoundExists);
                        return;
                    }

                    Task.Run(() =>
                    {
                        try
                        {
                            using (var web = new WebClient())
                            {
                                SoundboardLoggingService.Instance.Info(
                                    string.Format("downloading sound <{0}>", name));

                                web.DownloadFile(attachment.Url, path);

                                SoundboardLoggingService.Instance.Info(
                                    string.Format("downloaded <{0}>", name));

                                SoundEffectRepository.Add(new SoundboardEffect(path));
                                SendMessage(e.Channel, string.Format(Properties.Resources.MessageSoundReady, name));

                                SoundboardLoggingService.Instance.Info(
                                    string.Format("sound <{0}> is ready", name));
                            }
                        }
                        catch (Exception ex)
                        {
                            SoundboardLoggingService.Instance.Error("failed to download sound <{0}>", ex);
                            SendMessage(e.Channel, string.Format(Properties.Resources.MessageDownloadFailed, name));
                        }
                    });
                }
            }

            if (e.Message.IsMentioningMe())
            {
                var tokens = e.Message.Text.Split(' ');
                var cmd    = (tokens.Length >= 2) ? tokens[1].ToLowerInvariant() : string.Empty;

                SoundboardLoggingService.Instance.Info(
                    string.Format("[{0}] sent command <{1}>", e.User.Name, cmd));

                switch (cmd)
                {
                case "list":
                    CommandListSounds(e.User, e.Channel, tokens);
                    break;

                case "stats":
                    CommandStatistics(e.User, e.Channel, tokens);
                    break;

                case "random":
                    CommandPlayRandomEffect(e.User, e.Channel);
                    break;

                default:
                    CommandDefault(e.User, e.Channel, cmd);
                    break;
                }
            }
        }
コード例 #3
0
        public void PlaySoundEffect(User user, Channel ch, string name)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                return;
            }

            // Ensure voice channel is connected

            ConnectToVoice();

            // Play the sound effect

            Task.Run(() =>
            {
                try
                {
                    sending.WaitOne();

                    var effect = SoundEffectRepository.FindByName(name);

                    if (audio != null && effect != null)
                    {
                        if (effect.Duration.TotalMilliseconds == 0)
                        {
                            return;
                        }

                        SoundboardLoggingService.Instance.Info(
                            string.Format("[{0}] playing <{1}>", user.Name, name));

                        // Change "playing" to the sound effect name

                        SetStatusMessage(name);

                        // Records play statistics

                        Statistics.Play(user, effect);

                        // Notify users soundbot will begin playing

                        SendMessage(ch, string.Format(Properties.Resources.MessagePlayingSound, name));

                        // Resample and stream sound effect over the configured voice channel

                        var format = new WaveFormat(48000, 16, 2);
                        var length = Convert.ToInt32(format.AverageBytesPerSecond / 60.0 * 1000.0);
                        var buffer = new byte[length];

                        using (var reader = new WaveFileReader(effect.Path))
                            using (var resampler = new WaveFormatConversionStream(format, reader))
                            {
                                int count = 0;
                                while ((count = resampler.Read(buffer, 0, length)) > 0)
                                {
                                    audio.Send(buffer, 0, count);
                                }
                            }

                        audio.Wait();

                        SetStatusMessage(Configuration.Status);
                    }
                }
                catch (Exception ex)
                {
                    SoundboardLoggingService.Instance.Error(
                        string.Format(Properties.Resources.MessagePlayingFailed, name), ex);
                }
                finally
                {
                    sending.Set();
                }
            });
        }