protected void CommandDefault(User user, Channel ch, string cmd) { if (SoundEffectRepository.Exists(cmd)) { CommandPlayEffect(user, ch, cmd); } else { CommandInvalid(user, ch, cmd); } }
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; } } }